How to iterate or loop over HashMap (Map) in Java with Example

I have already shared how HashMap works internally in java. In this tutorial we will learn about the four different ways of looping or iterating over Map in java.

1. for-each loop
2. keyset() iterator
3. entrySet() and for loop
4. entrySet() and java iterator


1. Iterating or looping Map Using keySet() and foreach loop

In this we will use foreach loop to iterate over any map in java and using KeySet of the map to get keys. This will display keys and values together.


import java.util.*;

 public class HashMapLoopExample {
    public static void main(String args[]) {
        

    // Creating a HashMap of String keys and String values
    HashMap<String, String> hashmap = new HashMap<String, String>();
    hashmap.put("Key1", "Value1");
    hashmap.put("Key2", "Value2");
    System.out.println("Iterating or looping map using foreach loop");
    // Iterating or looping using keySet() method
    for (String key : hashmap.keySet()) {
        System.out.println("key: " + key + " value: " + hashmap.get(key));
    }
  }
}


Output
Iterating or looping map using foreach loop
key: Key2 value: Value2
key: Key1 value: Value1



2. Iterating or looping Map Using keySet() and iterator

In this example of looping HashMap in java we have used java iterator instead of for each loop.

import java.util.*;

 public class HashMapLoopExample {
    public static void main(String args[]) {
        
    // Creating a HashMap of String keys and String values
    HashMap<String, String> hashmap = new HashMap<String, String>();
    hashmap.put("Key1", "Value1");
    hashmap.put("Key2", "Value2");
    System.out.println("Iterating or looping map using keySet Iterator");
    // Iterating or looping using keySet() method
    Set<String> keySet = hashmap.keySet();
    Iterator<String> keySetIterator = keySet.iterator();
    while (keySetIterator.hasNext()) {
        String key = keySetIterator.next();
        System.out.println("key: " + key + " value: " + hashmap.get(key));
    }
  }
}


Output
Iterating or looping map using keySet iterator
key: Key2 value: Value2
key: Key1 value: Value1

3. Iterating or Looping Map in Java using entrySet() and for each Loop



import java.util.*;

 public class HashMapLoopExample {
    public static void main(String args[]) {
        
    // Creating a HashMap of String keys and String values
    HashMap<String, String> hashmap = new HashMap<String, String>();
    hashmap.put("Key1", "Value1");
    hashmap.put("Key2", "Value2");
    System.out.println("Iterating or looping map using entrySet and foreach loop");
    // Iterating or looping using entrySet() method
    Set<Map.Entry<String, String>> entrySet = hashmap.entrySet();
    for (Map.Entry entry : entrySet) {
        System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
    }
  }
}


Output
Iterating or looping map using entrySet and foreach loop
key: Key2 value: Value2
key: Key1 value: Value1

4. Iterate or Loop HashMap in Java Using entrySet() and java iterator

In this example we have used combination of Iterator and entrySet() to show all keys and values of a Java Map.


import java.util.*;

 public class HashMapLoopExample {
    public static void main(String args[]) {
        
    // Creating a HashMap of String keys and String values
    HashMap<String, String> hashmap = new HashMap<String, String>();
    hashmap.put("Key1", "Value1");
    hashmap.put("Key2", "Value2");
    System.out.println("Iterating or looping map using entrySet and Iterator");
    // Iterating or looping using entrySet() method
    Set<Map.Entry<String, String>> entrySet1 = hashmap.entrySet();
    Iterator<Map.Entry<String, String>> entrySetIterator = entrySet1.iterator();
    while (entrySetIterator.hasNext()) {
        Map.Entry entry = entrySetIterator.next();
        System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
    }
  }
}


Output
Iterating or looping map using entrySet and Iterator
key: Key2 value: Value2
key: Key1 value: Value1

I have shown all the ways through which looping of Map in java is possible. Please mention in comments if you know any other way of iterating or looping of Map in java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry