Java 8 single line forEach for looping through Collection

Java 8 introduced a new forEach method for Iterables ( such as List ) as well as for Map.
Lets see an example of looping through List and Map using forEach ( which uses Lambda Expression ).

Example : forEach + Lambda Expression = Loop through Collection

Employee.java

OUTPUT

Looping result for List :
Employee Name : Naveen , Salary : 1000
Employee Name : Hari , Salary : 2000
Employee Name : Raju , Salary : 9000

Looping result for Map :
Name : Hari , Salary : 2000
Name : Naveen , Salary : 1000
Name : Raju , Salary : 9000

 

 

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

www.000webhost.com