LogIn
I don't have account.

How can I iterate over a HashMap in Java?

I want to iterate a HashMap over keys and values.
How can i do it effectively?

Please pay attention to update your answer.

Update Your Answer care fully. After saving you can not recover your old answer

Carefully Update Your Answer!!

There are multiple ways to iterate over a hash map in Java. Let's take a HashMap 

HashMap<String, Integer> map = new HashMap<>();

  1. Iterating over key-value pairs

    for (Map.Entry<String, Integer> entry : map.entrySet()) {
       String key = entry.getKey();
       Integer value = entry.getValue();
       System.out.println("Key: " + key + ", Value: " + value);
    }

     

  2. Looping over Keys

    for(String key : map.keySet()) {
       System.out.println("Key: " + key);
     }

     

  3. Looping over Values

    for (Integer value : map.values()) {
       System.out.println("Value: " + value);
     }

     

  4. Java 8's forEach Method

    map.forEach((key, value) -> {
     System.out.println("Key: " + key + ", Value: " + value);
    });

Please pay attention to update your answer.

Update Your Answer care fully. After saving you can not recover your old answer

Carefully Update Your Answer!!

In Java, there are several ways to iterate over a HashMap, depending on whether you want to access keys, values or both (key–value pairs).

Let’s say you have this map:

Map<String, Integer> map = new HashMap<>();
map.put("A", 10);
map.put("B", 20);
map.put("C", 30);

 

 1. Iterate using entrySet() (Best and most efficient way)

for (Map.Entry<String, Integer> entry : map.entrySet()) {
    System.out.println(entry.getKey() + " = " + entry.getValue());
}
  • Efficient because it gives direct access to both key and value.
  • Preferred in most real-world scenarios.

2. Iterate using keySet()

for (String key : map.keySet()) {
    System.out.println(key + " = " + map.get(key));
}
  • Simple, but slightly less efficient because map.get(key) looks up the value each time.

 3. Iterate using values()

for (Integer value : map.values()) {
    System.out.println(value);
}
  • Use this if you only need the values.

4. Using Java 8 forEach() and Lambda

map.forEach((key, value) -> {
    System.out.println(key + " = " + value);
});
  • Clean and concise, ideal for modern Java (8+).

5. Using an Iterator (if you need to modify entries while looping)

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());
}
  • Use this when you need to remove elements safely during iteration.

 Best Practice

For readability and efficiency, prefer
map.entrySet() or map.forEach() (Java 8+).

Both provide direct access to key-value pairs without extra lookups.

Example (Recommended Modern Way):

map.forEach((key, value) -> System.out.println(key + " -> " + value));

Please pay attention to update your answer.

Update Your Answer care fully. After saving you can not recover your old answer

Carefully Update Your Answer!!

The best and most efficient way to iterate over a HashMap in Java is using the entrySet() or forEach() (Java 8+) method.

Here’s how 

 1. Using entrySet() (most efficient before Java 8)

for (Map.Entry<String, Integer> entry : map.entrySet()) {
    System.out.println(entry.getKey() + " = " + entry.getValue());
}
  • Directly accesses both key and value.
  • Avoids extra lookups (unlike using keySet() + get()).

2. Using forEach() (best for Java 8 and later)

map.forEach((key, value) -> System.out.println(key + " = " + value));
  • Clean, modern and concise.
  • Internally optimized for performance.

Conclusion:

Use map.forEach() in Java 8+ or map.entrySet() for earlier versions,  both are equally efficient and the best ways to iterate a HashMap.