Categories
hashmap iteration java loops

Iterate through a HashMap [duplicate]

3681

What’s the best way to iterate over the items in a HashMap?

2

3437

Iterate through the entrySet() like so:

public static void printMap(Map mp) {
    Iterator it = mp.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
        System.out.println(pair.getKey() + " = " + pair.getValue());
        it.remove(); // avoids a ConcurrentModificationException
    }
}

Read more about Map.

28

  • 40

    Though old style, this will help avoid ConcurrentModificationExceptions over the new foreach style in the answers below. You can for instance remove via the seperate iterator.

    Dec 16, 2010 at 9:22

  • 469

    @karim79 what do you think about the following way: 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()); }

    – fresh_dev

    Oct 25, 2011 at 8:56

  • 15

    by calling ‘it.remove(); ‘ you are emptying the map making it not reusable if this map was a class variable. Do you have any solution to that?

    – vim

    Jan 10, 2012 at 9:47

  • 28

    @vimukthi what do you mean a solution to that? Just remove the it.remove(); line.

    – Danny

    Jan 26, 2012 at 19:07

  • 114

    The for (Map.Entry<String, Object> cursor : map.entrySet()) {...} syntax is much better.

    Jan 28, 2012 at 17:29


3437

Iterate through the entrySet() like so:

public static void printMap(Map mp) {
    Iterator it = mp.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
        System.out.println(pair.getKey() + " = " + pair.getValue());
        it.remove(); // avoids a ConcurrentModificationException
    }
}

Read more about Map.

28

  • 40

    Though old style, this will help avoid ConcurrentModificationExceptions over the new foreach style in the answers below. You can for instance remove via the seperate iterator.

    Dec 16, 2010 at 9:22

  • 469

    @karim79 what do you think about the following way: 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()); }

    – fresh_dev

    Oct 25, 2011 at 8:56

  • 15

    by calling ‘it.remove(); ‘ you are emptying the map making it not reusable if this map was a class variable. Do you have any solution to that?

    – vim

    Jan 10, 2012 at 9:47

  • 28

    @vimukthi what do you mean a solution to that? Just remove the it.remove(); line.

    – Danny

    Jan 26, 2012 at 19:07

  • 114

    The for (Map.Entry<String, Object> cursor : map.entrySet()) {...} syntax is much better.

    Jan 28, 2012 at 17:29


869

Extracted from the reference How to Iterate Over a Map in Java:

There are several ways of iterating over a Map in Java. Let’s go over the most common methods and review their advantages and disadvantages. Since all maps in Java implement the Map interface, the following techniques will work for any map implementation (HashMap, TreeMap, LinkedHashMap, Hashtable, etc.)

Method #1: Iterating over entries using a For-Each loop.

This is the most common method and is preferable in most cases. It should be used if you need both map keys and values in the loop.

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());
}

Note that the For-Each loop was introduced in Java 5, so this method is working only in newer versions of the language. Also a For-Each loop will throw NullPointerException if you try to iterate over a map that is null, so before iterating you should always check for null references.

Method #2: Iterating over keys or values using a For-Each loop.

If you need only keys or values from the map, you can iterate over keySet or values instead of entrySet.

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

// Iterating over keys only
for (Integer key : map.keySet()) {
    System.out.println("Key = " + key);
}

// Iterating over values only
for (Integer value : map.values()) {
    System.out.println("Value = " + value);
}

This method gives a slight performance advantage over entrySet iteration (about 10% faster) and is more clean.

Method #3: Iterating using Iterator.

Using Generics:

Map<Integer, Integer> map = new HashMap<Integer, Integer>();
Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
    Map.Entry<Integer, Integer> entry = entries.next();
    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}

Without Generics:

Map map = new HashMap();
Iterator entries = map.entrySet().iterator();
while (entries.hasNext()) {
    Map.Entry entry = (Map.Entry) entries.next();
    Integer key = (Integer)entry.getKey();
    Integer value = (Integer)entry.getValue();
    System.out.println("Key = " + key + ", Value = " + value);
}

You can also use same technique to iterate over keySet or values.

This method might look redundant, but it has its own advantages. First of all, it is the only way to iterate over a map in older versions of Java. The other important feature is that it is the only method that allows you to remove entries from the map during iteration by calling iterator.remove(). If you try to do this during For-Each iteration you will get “unpredictable results” according to Javadoc.

From a performance point of view this method is equal to a For-Each iteration.

Method #4: Iterating over keys and searching for values (inefficient).

Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Integer key : map.keySet()) {
    Integer value = map.get(key);
    System.out.println("Key = " + key + ", Value = " + value);
}

This might look like a cleaner alternative for method #1, but in practice it is pretty slow and inefficient as getting values by a key might be time-consuming (this method in different Map implementations is 20%-200% slower than method #1). If you have FindBugs installed, it will detect this and warn you about inefficient iteration. This method should be avoided.

Conclusion:

If you need only keys or values from the map, use method #2. If you are stuck with older version of Java (less than 5) or planning to remove entries during iteration, you have to use method #3. Otherwise use method #1.

3

  • 1

    Lets add the small caevet, that in case of ConcurrentMaps, iteration on keySet() will crash in general (there is no guarantee the values exist for earlier-gathered keys). On the other hand using iterators or entries is safe (they always refer to existing objects).

    – P Marecki

    Feb 29, 2016 at 12:34

  • 2

    @arvind How would method #4 ever be inefficient? By definition, calling get() is always O(1) for a HashMap. That is the definition of a HashMap and the user asked for a HashMap. I do not get why this is so highly upvoted. If you are going to reference someone else’s link, make sure it actually makes sense for the question asked.

    – ohbrobig

    Jun 3, 2018 at 13:09


  • 1

    @ohbrobig yet it’s O(1) but that’s the runtime, that’s how it scale. It doesn’t mean it will necessarily get the value in the first cycle.Method#4 will definitely be slower then Method#1

    Apr 13, 2019 at 8:45