Ways to iterate over entries in a Map

Effectily way to iterate over entries in Java map

Let say we have a map with these values:

Map<Integer, String> map = new HashMap<>();
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");
map.put(4, "Four");
map.put(5, "Five");

All you want is iterate over elements in this map. There are some ways to do that:

Java 8

If you are using Java 8 or later, you can use one of those approachs:

Using forEach

This is simple and really readable way

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

// Output:
// 1: One
// 2: Two
// 3: Three
// 4: Four
// 5: Five
//

Using the Stream API

map.entrySet()
    .stream()
    .forEach(entry ->  System.out.println("" + entry.getKey() + ": " + entry.getValue()));

// Output same as above

The stream().forEach() chain can be replaced with forEach()

map.entrySet()
   .forEach(entry ->  System.out.println("" + entry.getKey() + ": " + entry.getValue()));

// Output same as above

Using Stream parallel API

map.entrySet()
   .stream()
   .parallel()
   .forEach(entry -> System.out.println("" + entry.getKey() + ": " + entry.getValue()));

// Output same as above

Using foreach and entrySet()

This is the most common method before Java 8 and is preferable in most cases.

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

// Output:
// 1: One
// 2: Two
// 3: Three
// 4: Four
// 5: Five
//
Note
For-Each loop will throw NullPointerException if map is null, so make sure you check null before iterating a map

Using iterator and entrySet()

Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
    final Map.Entry<Integer, String> entry = iterator.next();
    System.out.println("" + entry.getKey() + ": " + entry.getValue());
}

// Output same as above

Using keySet() and foreach

for (final Integer key : map.keySet()) {
    System.out.println("" + key + ": " + map.get(key));
}

Using keySet() and iterator

This way, similar with Using keySet() and foreach we iterate over keys set and search for values using Map#get(key).

Note
This way is inefficient, compare to Java 8 forEach approach
. It is slow and inefficient since getting values by a key might be time-consuming.

Iterator<Integer> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
    final Integer key = iterator.next();
    System.out.println("" + key + ": " + map.get(key));
}

Using values() and foreach

If you only need the values and don’t care about the keys, you can you this way:

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

// Output
// Value: One
// Value: Two
// Value: Three
// Value: Four
// Value: Five
//

Conclusion

If you are using Java 8 or later, you can use Java 8 forEach. If you are on the erlier of Java, you can use the most common way foreach and entrySet(). If you need only keys or values from the map, you can use keySet() or values().

Last modified October 4, 2020