A Stream in the new Java 8 is a typed interface, where we can use a Stream of T elements, being T an integer, a String, an Employer object, etc.
With Stream we can handle data the same way we would work in a Big Data environment, applying map-filter-reduce operations, for example.
But aware you must! a Stream may look like a Collection at first sight, but it is NOT a collection. I repeat. It is not a collection. Collections and Streams work in a total different way. In fact, a Stream object does not store any info, unlike a Collection.
If we execute this code, we would get the following console output.
One of the most interesting features of this Stream interface is the fact that it will work in parallel, meaning, will use all processors we may have in our machine, avoiding the programmer to handle this work for him, in a total transparent way.
What we have done in the last piece of code was a nice filter. Filtering data with Streams is fairly simple. In the following example, we want to get all the employees that contains the word 'Attitude'
In this case we would get what we were searching for:
There is something very important to remember when we work with Streams. Once we have used a Stream, it cannot be reused, or we will have a nice exception in our console. Hence, if we try to apply the forEach method again:
We will get this message in our console:
This is it for the first part of the new features in Java8. New posts arriving soon!!