ULOG: Java Programming Basics 14 - Ternary Operator SolutionsteemCreated with Sketch.

in #ulog6 years ago (edited)

Lecture 14: The stream() Method Solution


The stream() method coding exercise

import java.util.List;
public class Lecture14 {
    public static void main(String[] args) {
        List<String> brics = List.of("Brazil", "Russia", "India", "China", "South Africa");
        // TODO: Implement the 'stream()' method to print the names of the 'BRICS' countries
    }
}

The stream() method solution code
brics.stream().forEach(c -> System.out.print(c + " "));

The stream() method output
Brazil Russia India China South Africa

L14.png

Hi there, My name is Marius from AlefTav Coding. In this lecture you will learn the solution to the stream() method coding exercise. Your homework was, 'Implement the stream() method to print the names of the BRICS countries'. BRICS is an abbreviation of the first letters of the names of five newly industrialized nations.

brics.stream().forEach(c -> System.out.print(c + " ") );

This solution code above starts with a collection of String text, named brics, all small letters. I turn a collection of words into a stream of text, with a call to the stream() method. Now that I have a stream, I can call a method such as forEach(), to print the name of each individual country. Also, forEach() is one of those methods that can call a lambda expression.

Complete solution

import java.util.List;
public class Lecture14 {
    public static void main(String[] args) {
        List<String> brics = List.of("Brazil", "Russia", "India", "China", "South Africa");
        brics.stream()
             .forEach(c -> System.out.print(c + " ") );
    }
}

Note that for improved readability I align the dots in my solution vertically. The method, forEach() is placed vertically below the stream() method. We read this lambda as follows: Given a value, c, short for country, return that same value by printing it. After each country's name is printed, add one space, + " ". The lambda operator, ->, separates the two parts of the lambda expression.

Next, I have a class, Lecture14 with the usual main() method. I then create a collection of values by declaring a List of String. Why String? Because I am going to work with words of text. The name of my list is brics. Then I add the five names to my collection with the expression, List.of().

The next step is to do an import for List in the statement, import java.util.List;.

My program is now complete. So, when I run my code the output displayed is as follows:

Brazil Russia India China South Africa

This is the word, Brazil, followed by one space, then the word, Russia, again a space and so on until the forEach() method finds there are no more words left to print in the collection.


In this lecture you learnt the solution to the stream() coding exercise. The next lecture will be the last in this series of fifteen tutorials.

This is Marius, from AlefTav Coding. Till the next time, KEEP CODING.

Join me on Trybe.one (tokenized information-sharing platform) to earn 100 tokens for signing up and 10 tokens for logging in daily:

http://trybe.one/ref/2859/

This post was made from https://ulogs.org

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63498.69
ETH 2645.91
USDT 1.00
SBD 2.80