https://aleftavtraining.siterubix.com/
Lecture 14: Stream() Method Solution
The stream() method problem statement
Implement the stream() method to print the names of the BRICS countries.
The stream() method solution
brics.stream().forEach(c -> System.out.print(c + " "));
The stream() method output
Brazil Russia India China South Africa

Hi there, My name is Marius from AlefTav Coding. In this lecture you will learn the solution to the stream() 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 countries.
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 + " "));
}
}
My solution starts with a collection of String text, named brics (b, r, i, c, s), all small letters. I turn this 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 each of the words. Also, forEach() is one of those methods that can call a lambda.
Note that for improved readability I place the forEach() method on a new line . I read this lambda as follows: Given a value, c, short for country, return that same value by printing it. After each country 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 ususal 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 (import java.util.List;).
My program is now completed. So, when I run it the output displayed 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 list.
In this lecture you learnt the solution to the stream() coding exercise. The next lectures will be the last in this course.
This is Marius, from AlefTav Coding. Till the next time, KEEP CODING.
Please feel free to upvote, subscribe, like and share. You can find me at the following links.
https://www.udemy.com/course/1435298/manage/basics/
https://steemit.com/@mariusclaassen