In Java 8, they introduced forEach. Using it List, Maps can be looped.
Loop a List using for each
List<String> someList = new ArrayList<String>();someList.add("A");someList.add("B");someList.add("C");someList.forEach(listItem -> System.out.println(listItem))
or
someList.forEach(listItem-> { System.out.println(listItem); });
Loop a Map using for each
Map<String, String> mapList = new HashMap<>(); mapList.put("Key1", "Value1"); mapList.put("Key2", "Value2"); mapList.put("Key3", "Value3");mapList.forEach((key,value)->System.out.println("Key: "+ key +" Value : "+ value));
or
mapList.forEach((key,value)->{ System.out.println("Key : "+ key +" Value : "+ value);});