From Java 8 onward, you can iterate over a List or any Collection without using any loop in Java. The new Stream class provides a forEach() method, which can be used to loop over all or selected elements of list and map. forEach() method provides several advantage over traditional for loop e.g. you can execute it in parallel by just using a parallel Stream instead of regular stream. Since you are operating on stream, it also allows you to filter and map elements. Once you are done with filtering and mapping, you can use forEach() to operate over them. You can even use method reference and lambda expression inside forEach() method, resulting in more clear and concise code. If you not started with Java 8 yet than you should make it one of your new year resolution for 2016. Coming years will see much more adoption of Java 8. If you are looking for a good book to learn Java 8, than you can use Java 8 in Action, one of the best book about lambda expression, stream and other functional aspect of Java 8. For now, let's see a couple of examples of forEach() in Java 8.
Read more »
