Using older Java versions, including Java 7Java 7, you can use a 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);
}
FollowingThe following is the very latest way of using a foreachfor each loop in Java 8
Java 8 (loop a List with forEach + lambda expression or method reference).
Lambda
//lambda
//Output : A,B,C,D,E
items.forEach(item->System.out.println(item));
Method reference
//method reference
//Output : A,B,C,D,E
items.forEach(System.out::println);
For more infoinformation, refer this linkto "Java 8 forEach examples".