Iterate Map in Java using entrySet() method
This post will discuss various methods to iterate map in Java using the entrySet() method.
We know that Map.entrySet() returns a set of key-value mappings contained in the map. So, we can iterate a map using Map.entrySet(), which contains both key-value pairs. There are several ways to do that:
1. Using Iterator
Since the map doesn’t extend the Collection interface, it doesn’t have its own iterator. But Map.entrySet() returns a set of key-value mappings, and since Set extends the Collection interface, we can get an iterator to it. Now we can easily process each key-value pair by using a simple while loop, as shown below:
|
1 2 3 4 5 |
Iterator<Map.Entry<K, V>> itr = map.entrySet().iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } |
2. Using for-each loop (Enhanced for statement)
The for-loop also has another variation designed for iteration through collections and arrays. It is called a for-each loop and is available to any object implementing the Iterable interface. As Set extends Collection interface and Collection extends Iterable interface, we can use a for-each loop to loop through the entrySet. Please note that this approach will also invoke the iterator() method behind the scenes.
|
1 2 3 4 5 6 7 |
for (Map.Entry<K, V> entry: map.entrySet()) { K key = (Integer)entry.getKey(); V value = entry.getValue(); System.out.println(key + "=" + value); } |
3. Using Iterator.forEachRemaining() method
We can also use the forEachRemaining() method that is the latest addition to the Iterator interface in Java 8 and above. It performs the given action for each remaining element until all elements have been processed.
As seen before, we can easily get an iterator to the set of Map.Entry<K,V>. Once we have the iterator, we can pass the method reference System.out::println or corresponding lambda expression E -> System.out.println(E) to forEachRemaining() method, as shown below:
|
1 2 3 4 5 6 7 8 9 |
// using method reference map.entrySet() .iterator() .forEachRemaining(System.out::println); // or using a lambda expression map.entrySet() .iterator() .forEachRemaining(E -> System.out.println(E)); |
4. Using Stream.forEach() method
We can use streams in Java 8 and above to iterate a map by passing method reference or lambda expression to forEach() method of Stream interface that performs an action for each element of this stream.
|
1 2 3 |
map.entrySet() .stream() .forEach(System.out::println); |
We can also use Stream.of to get stream of Objects:
|
1 2 |
Stream.of(map.entrySet().toArray()) .forEach(System.out::println); |
5. Using toString() method
If we just want to display key-value pairs present on the map, we can simply print the string representation of map.entrySet() that returns a set of key-value pairs. This approach will even work directly on a map, as shown below:
|
1 2 3 |
System.out.println(map.entrySet().toString()); System.out.println(map.toString()); |
Or in Java 8 and above using streams:
|
1 2 3 4 5 |
Stream.of(map.entrySet().toString()) .forEach(System.out::println); Stream.of(map.toString()) .forEach(System.out::println); |
Following is a simple Java program that covers all approaches discussed above:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.stream.Stream; class Main { // Program to iterate map in Java using `entrySet()` public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); map.put(1, "One"); map.put(2, "Two"); // 1. Using an iterator Iterator<Map.Entry<Integer, String>> itr = map.entrySet().iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } // 2. For-each loop for (Map.Entry<Integer, String> entry: map.entrySet()) { Integer key = (Integer)entry.getKey(); String value = entry.getValue(); System.out.println(key + "=" + value); } // 3. Java 8 – Using `Iterator.forEachRemaining()` map.entrySet() .iterator() .forEachRemaining(System.out::println); // 4. Java 8 – Using `Stream.forEach()` map.entrySet() .stream() .forEach(System.out::println); // Java 8 – Using `Stream.of()` to get `Stream<Object>` Stream.of(map.entrySet().toArray()) .forEach(System.out::println); // 5. Using `toString()` System.out.println(map.entrySet().toString()); // Java 8 – Using `Stream.of()` to get `Stream<String>` Stream.of(map.entrySet().toString()) // count() = 1 .forEach(System.out::println); } } |
We can also use Generics for Iterator and for-each loop method discussed above:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; class Main { public static <K, V> void printMap(Set<Map.Entry<K, V>> entrySet) { // 1. Using an iterator Iterator<Map.Entry<K, V>> itr = entrySet.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } // 2. Using for-each loop for (Map.Entry<K, V> entry: entrySet) { K key = (K)entry.getKey(); V value = entry.getValue(); System.out.println(key + "=" + value); } // 3. Using forEach() entrySet.forEach(entry -> { K key = (K) entry.getKey(); V value = entry.getValue(); System.out.println(key + "=" + value); }); } // Program to iterate map in Java using `entrySet()` public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); map.put(1, "One"); map.put(2, "Two"); // Pass the `map.entrySet()` which contains both key-value pairs printMap(map.entrySet()); } } |
That’s all about iterating over a Map in Java using the entrySet() method.
Related Post:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)