With many new features, Java 8 has has an addition of forEach as one of the most easy one to use. It reduces the coding time and speeds up the process of iteration as well.
Below example shows how to iterate a List in Java 8 using this new feature. We also need to note that Lambda expression gets used while using it. So if you have not understood the Lambda expressions yet, have a quick go through here.
Hello World forEach Iteration
The output comes as :
Printing Using Java 7 way :
Hello World forEach Code Sample Now Printing Using Java 8 forEach way : Hello World forEach Code Sample
This is fairly easy and common to use feature. This can be used to iterate the map’s content as well. So have you started using it yet? If not, what are you waiting for 🙂