java hashmap for each code example
Example 1: how to iterate hashmap in java
for (Map.Entry<String, String> entry : yourHashMap.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
map.forEach((k, v) -> {
System.out.format("key: %s, value: %d%n", k, v);
});
Example 2: how to iterate hashmap java
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
Example 3: java loop hashmap
map.forEach((k, v) -> {
System.out.format("key: %s, value: %d%n", k, v);
});
Example 4: foreach hashmap java
hashmap.forEach(BiConsumer<K, V> action)