Using older Java versions including Java 7
you can use foreach
loop as follows.
List<String> items = new ArrayList<>();
items.add("A");
items.add("B");
items.add("C");
items.add("D");
items.add("E");
for(String item : items){
System.out.println(item);
}
Following is the very latest way of using foreach
loop in Java 8
(loop a List with forEach
+ lambda expression or method reference)
//lambda
//Output : A,B,C,D,E
items.forEach(item->System.out.println(item));
//method reference
//Output : A,B,C,D,E
items.forEach(System.out::println);
For more info refer this link.