Java HashMap Loop β Iterate Over Keys, Values, and Entries
Introduction
HashMap is one of the most used data structures in Java. Whether you’re counting word frequencies, mapping users to settings, or processing key-value data, knowing how to loop through a HashMap efficiently is a must.
By the end of this article, you’ll know how to:
- Iterate over keys, values, and key-value pairs
- Use both classic and Java 8+ looping styles
- Choose the best loop for your use case
Sample HashMap
Let’s use this HashMap<String, Integer> for all examples:
import java.util.HashMap;
HashMap<String, Integer> map = new HashMap<>();
map.put("Apple", 10);
map.put("Banana", 20);
map.put("Mango", 15);
map.put("Grapes", 12);
This map stores fruit names and their quantities.
1. Loop Through Keys (keySet)
for (String key : map.keySet()) {
System.out.println("Key: " + key + ", Value: " + map.get(key));
}
Explanation:
map.keySet()returns a set of all keys.map.get(key)fetches the corresponding value.
Use this if you only need to process keys.
2. Loop Through Values (values)
for (Integer value : map.values()) {
System.out.println("Value: " + value);
}
Explanation:
map.values()returns all values.- Doesnβt give access to keys.
Use this when keys are not needed.
3. Loop Through Entries (entrySet)
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
Explanation:
entrySet()provides both key and value in each loop.entry.getKey()andentry.getValue()access the elements.
This is the most efficient and preferred method for full map traversal.
4. Java 8 forEach + Lambda
map.forEach((key, value) -> {
System.out.println("Key: " + key + ", Value: " + value);
});
Explanation:
- Uses
forEach()with a lambda expression. - Clean and functional style.
Best for modern Java codebases (Java 8+).
Comparison Table
| Loop Type | Access Keys | Access Values | Access Entries | Java Version |
|---|---|---|---|---|
keySet() | Yes | Yes (with get) | No | Java 1.2+ |
values() | No | Yes | No | Java 1.2+ |
entrySet() | Yes | Yes | Yes | Java 1.2+ |
forEach() (Lambda) | Yes | Yes | Yes | Java 8+ |
Summary
In this article, you learned:
- Multiple ways to loop through a
HashMapin Java - When to use
keySet(),values(),entrySet(), andforEach() - Best practices for modifying or filtering data
Knowing the right iteration method boosts performance and keeps your code clean.
FAQ β Java HashMap Looping
Which loop is the most efficient for HashMap?
entrySet() loop is generally the fastest and most memory-efficient when both keys and values are needed.
Can I modify values during looping?
Yes. Use entry.setValue(newValue) inside an entrySet() loop to modify values directly.
Can I remove elements while looping?
Use an Iterator to safely remove elements during iteration:
Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Integer> entry = iterator.next();
if (entry.getValue() < 15) {
iterator.remove();
}
}
Share Now :
