I am iterating a list with size approx. 500. On using For each loop it took approx 1ms to reach last record where as Stream API took approx more than 50ms to filter particular record.
Why should I use stream API and where its beneficial ?
I am iterating a list with size approx. 500. On using For each loop it took approx 1ms to reach last record where as Stream API took approx more than 50ms to filter particular record.
Why should I use stream API and where its beneficial ?
Streams should be used if it can make your code shorter and more readable. So your use case there is not a good example by itself. However if you are going to use the map.forEach method itself, then there is some point in using lambdas, but then again, you're not using streams anymore at that point.
The Streams API makes parallelism much easier to accomplish (although you'll only see the benefit with a large sized collection). If you had to implement parallelism on your first example then there would be a sizeable difference in the amount of code (as opposed to adding .parallelStream() to the second example)
http://docs.oracle.com/javase/tutorial/collections/streams/parallelism.html