Saturday, June 13, 2015

Java 8: Gone are the days of For Loops: Stream API

Java 8 seems to have skipped a generation. So much changes bundled together and so much to explore.

java.util.stream adds support for functional-style operations on streams of elements. This opens up huge possibilities. A major outcome is that traditional for-loops might not exist as we know them. Gone are their days. So what come as replacement?

There you go! Right question. Already on the mark.

Let's see some examples.



Above piece of code is a traditional for loop, which returns the Opening Batsmen.. Same code can now be written as below without the for loop.




Nice! Used filter to filter out the Opening Batsmen and return the first one of all.

There are two Opening Batsmen. How to get both of them.

Below is the code both in traditional for-loop and the new Streams



There can be innumerable example of replacing for-loops. Explore them and start using the power of Java 8.





No comments:

Post a Comment