A Map in Java stores data in the form of key-value pairs. Java provides several ways to iterate over a map, depending on whether we need both keys and values or only one of them.
- entrySet() is useful when both key and value are required.
- keySet(), values(), Iterator, and forEach() can be used for different iteration requirements.
Note: A Map is not a subtype of Collection, so we cannot directly use an iterator on the map. Instead, we can iterate through views such as entrySet() and keySet().
Ways to Iterate Over a Map
The following are the common ways to iterate over a Map in Java.
1. Using entrySet() with For-Each Loop
The entrySet() method returns a set containing all key-value pairs of the map. Each pair is represented by Map.Entry<K, V>.This approach is preferred when we need both the key and value.
import java.util.*;
public class MapIteration {
public static void main(String[] args)
{
Map<String, Integer> marks = new LinkedHashMap<>();
marks.put("Akshat", 85);
marks.put("Rahul", 78);
marks.put("Priya", 92);
marks.put("Aman", 88);
for (Map.Entry<String, Integer> entry : marks.entrySet()) {
System.out.println("Name = " + entry.getKey() + ", Marks = " + entry.getValue());
}
}
}
Output
Name = Akshat, Marks = 85 Name = Rahul, Marks = 78 Name = Priya, Marks = 92 Name = Aman, Marks = 88
Explanation: entrySet() provides each key-value pair as a Map.Entry object. The getKey() method returns the key, while getValue() returns its corresponding value.
2. Using keySet() and values()
The keySet() method returns all keys of the map, while the values() method returns all values. This approach is useful when we need only keys or only values.
import java.util.*;
public class MapIteration{
public static void main(String[] args)
{
Map<String, Integer> marks = new LinkedHashMap<>();
marks.put("Akshat", 85);
marks.put("Rahul", 78);
marks.put("Priya", 92);
marks.put("Aman", 88);
System.out.println("Keys:");
for (String name : marks.keySet()){
System.out.println(name);
}
System.out.println("\nValues:");
for (Integer mark : marks.values()){
System.out.println(mark);
}
}
}
Output
Keys: Akshat Rahul Priya Aman Values: 85 78 92 88
Explanation: keySet() is used to iterate through the keys, whereas values() is used to iterate through the values. If both key and value are required together, entrySet() is generally more suitable.
3. Using Iterator
We can use an Iterator with the entrySet() view of a map. This approach is useful when we need more control over the iteration, such as removing an entry safely during iteration.
import java.util.*;
public class MapIteration {
public static void main(String[] args) {
Map<String, Integer> marks = new LinkedHashMap<>();
marks.put("Akshat", 85);
marks.put("Rahul", 78);
marks.put("Priya", 92);
marks.put("Aman", 88);
Iterator<Map.Entry<String, Integer>> iterator =
marks.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Integer> entry = iterator.next();
System.out.println(
"Name = " + entry.getKey()
+ ", Marks = " + entry.getValue()
);
}
}
}
Output
Name = Akshat, Marks = 85 Name = Rahul, Marks = 78 Name = Priya, Marks = 92 Name = Aman, Marks = 88
Explanation: The Iterator traverses the entries returned by entrySet(). The hasNext() method checks whether another entry is available, and next() returns the next entry.
4. Using forEach()
Java 8 introduced the forEach() method, which allows us to iterate over a map using a lambda expression. It provides a short and readable way to process key-value pairs.
import java.util.*;
public class MapIteration {
public static void main(String[] args)
{
Map<String, Integer> marks = new LinkedHashMap<>();
marks.put("Akshat", 85);
marks.put("Rahul", 78);
marks.put("Priya", 92);
marks.put("Aman", 88);
marks.forEach((name, mark)
-> System.out.println("Name = " + name + ", Marks = " + mark));
}
}
Output
Name = Akshat, Marks = 85 Name = Rahul, Marks = 78 Name = Priya, Marks = 92 Name = Aman, Marks = 88
Explanation: The forEach() method accepts an action that is performed for every key-value pair. Here, name represents the key and mark represents the value.
5. Using keySet() with get()
Another way is to iterate through the keys using keySet() and retrieve the corresponding value using get().
import java.util.*;
public class MapIteration {
public static void main(String[] args) {
Map<String, Integer> marks = new LinkedHashMap<>();
marks.put("Akshat", 85);
marks.put("Rahul", 78);
marks.put("Priya", 92);
marks.put("Aman", 88);
for (String name : marks.keySet()) {
Integer mark = marks.get(name);
System.out.println(
"Name = " + name + ", Marks = " + mark
);
}
}
}
Output
Name = Akshat, Marks = 85 Name = Rahul, Marks = 78 Name = Priya, Marks = 92 Name = Aman, Marks = 88
Explanation: In this approach, keySet() provides each key and get(key) is used to obtain its corresponding value. When both key and value are required, entrySet() is generally a simpler choice.