how to iterate through keys in hashmap java code example

Example 1: java iterate through hashmap

for (Map.Entry<String, String> entry : yourHashMap.entrySet()) {
	System.out.println(entry.getKey() + " = " + entry.getValue());
}

Example 2: iterating over a hashmap

for(Map.Entry<String, Integer> entry : hashMap.entrySet()) {
    String key = entry.getKey();
    Integer value =  entry.getValue();
    //do something with the key and value
}

Example 3: java loop hashmap

map.forEach((k, v) -> {
    System.out.format("key: %s, value: %d%n", k, v);
});

Tags:

Java Example