π 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
HashMap
in 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 :