how to loop map in java 8 code example
Example 1: java 8 map foreach
public void iterateUsingIteratorAndEntry(Map<String, Integer> map) {
Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Integer> entry = iterator.next();
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
Example 2: java 8 map foreach
public void iterateUsingStreamAPI(Map<String, Integer> map) {
map.entrySet().stream()
.forEach(e -> System.out.println(e.getKey() + ":" + e.getValue()));
}