Posts

Showing posts from 2018

Using foreach method with list in Java 8

This blog post is about using foreach method with list in Java 8. Foreach method can be used to easily iterate a list. Check my previous post to see how to iterate foreach method with map in Java 8 . The program to iterate list using foreach method is as follows: import java.util.ArrayList; import java.util.List; /* * Java program to demonstrate the use of forEach method in List */ public class ForEachList { public static void main(String[] args) { List<String> usStates = new ArrayList<>(); usStates.add( "California" ); usStates.add( "New York" ); usStates.add( "New Mexico" ); usStates.add( "Pennsylvania" ); usStates.add( "New Jersey" ); // iterating the List through normal method System.out.println( "***Iterating the list through normal method***\n" ); for (String states:usStates) {