Iterate Map in Java using keySet() method
This post will discuss various methods to iterate map using keySet() in Java.
We know that the keySet() method returns a set view of the keys contained in the map. So, we can iterate a map using keySet() and for each key calling map.get(key) to fetch a value. 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. Since Set extends the Collection interface, we can get an iterator to set of keys returned by keySet(). Now we can easily process each key by using a simple while loop, as shown below:
|
1 2 3 4 5 6 7 8 |
Iterator<K> itr = map.keySet().iterator(); while (itr.hasNext()) { K key = itr.next(); V value = map.get(key); System.out.println(key + "=" + value); } |
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 the Collection interface, and Collection extends Iterable interface, we can use a for-each loop to loop through the keySet. Please note that this approach will also invoke the iterator() method behind the scenes.
|
1 2 3 |
for (K key: map.keySet()) { System.out.println(key + "=" + map.get(key)); } |
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.
|
1 2 3 4 |
// using lambda expression map.keySet() .iterator() .forEachRemaining(key -> System.out.println(key + "=" + map.get(key))); |
4. Using Stream.forEach() method
We can use streams in Java 8 and above to iterate a map by passing the lambda expression to the forEach() method of the Stream interface that performs an action for each element of this stream.
|
1 2 3 |
map.keySet() .stream() .forEach(key -> System.out.println(key + "=" + map.get(key))); |
5. Using Stream.of() + toArray() + forEach() method
We can also call forEach operation on stream of objects returned by Stream.of() method:
|
1 2 |
Stream.of(map.keySet().toArray()) .forEach(key -> System.out.println(key + "=" + map.get(key))); |
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 |
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.stream.Stream; class Main { // Program to iterate map using `keySet()` in Java 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<Integer> itr = map.keySet().iterator(); while (itr.hasNext()) { Integer key = itr.next(); String value = map.get(key); System.out.println(key + "=" + value); } // 2. For-each loop for (Integer key: map.keySet()) { System.out.println(key + "=" + map.get(key)); } // 3. Java 8 - Iterator.forEachRemaining() map.keySet() .iterator() .forEachRemaining(key -> System.out.println(key + "=" + map.get(key))); // 4. Java 8 - Stream.forEach() map.keySet().stream() .forEach(key -> System.out.println(key + "=" + map.get(key))); // 5. Java 8 - Stream.of() + toArray() + forEach() Stream.of(map.keySet().toArray()) .forEach(key -> System.out.println(key + "=" + map.get(key))); } } |
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 |
import java.util.HashMap; import java.util.Iterator; import java.util.Map; class Main { public static <K, V> void printMap(Map<K, V> map) { // using Iterator Iterator<K> itr = map.keySet().iterator(); while (itr.hasNext()) { K key = itr.next(); V value = map.get(key); System.out.println(key + "=" + value); } // For-each loop for (K key: map.keySet()) { System.out.println(key + "=" + map.get(key)); } // using Stream API map.keySet().forEach(key -> System.out.println(key + "=" + map.get(key))); } // Program to iterate map using `keySet()` in Java public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); map.put(1, "One"); map.put(2, "Two"); printMap(map); } } |
That’s all about iterating over a Map in Java using the keySet() 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 :)