/** * @author lautturi.com * Java example: Java hashmap map forEach iterate through a hashmap */ import java.util.*; import java.util.Map.Entry; public class Lautturi { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("Apple", 21); map.put("Banana", 12); map.put("Cherry", 15); map.put("Orange", 35); map.forEach((k, v) -> { System.out.format("key: %s, value: %d%n", k, v); }); } }
output:
key: Apple, value: 21 key: Cherry, value: 15 key: Orange, value: 35 key: Banana, value: 12