For Each and Map/Reduce
Learn about the `forEach` and map/reduce method in Java 8.
We'll cover the following...
We'll cover the following...
For each
The most basic thing we can do with a stream is loop through it using the forEach method.
For example, to print out all of the files in the current directory, we could do the following:
For the most part, this replaces the for loop. It is more concise and more object-oriented since we are delegating the implementation of the actual loop.
Map, filter, and reduce
Lambda expressions and default methods allow us to implement map/filter/reduce in Java 8. Actually, it is already implemented for us in the standard library.
Let’s see how these methods can be implemented:
The above program creates a map with ...