Performance Comparison of Different HashMap Traversal Methods in Java
This article benchmarks various Java HashMap traversal techniques—keySet with iterator and for-loop, entrySet with iterator and for-loop, and values iteration—using two million‑entry maps to measure execution time and determine the most efficient method for different scenarios.
The article investigates how to choose the most efficient HashMap traversal method in Java by conducting practical performance tests.
1. Origin
To understand which iteration technique yields the best speed for large data sets, the author compares entrySet and keySet based approaches, as many articles suggest entrySet is faster for massive lookups.
2. Test Data Preparation
Two HashMaps each containing 1,000,000 entries are created:
Map<String,String> map = new HashMap<>();
String key, value;
for (int i = 1; i <= num; i++) {
key = "" + i;
value = "value" + i;
map.put(key, value);
}HashMap2 uses keys spaced by 50 (1, 51, 101, …) to simulate a different key distribution.
Map<String,String> map = new HashMap<>();
String key, value;
for (int i = 1; i <= num; i++) {
key = "" + (i * 50);
value = "value" + key;
map.put(key, value);
}3. Scenario Tests
3.1 Traversing key+value
Four approaches are measured:
keySet with Iterator
long startTime1 = System.currentTimeMillis();
Iterator<String> iter = map.keySet().iterator();
while (iter.hasNext()) {
key = iter.next();
value = map.get(key);
}
long endTime1 = System.currentTimeMillis();
System.out.println("First program runtime:" + (endTime1 - startTime1) + "ms");keySet with for‑each loop
long startTime2 = System.currentTimeMillis();
for (String k : map.keySet()) {
value = map.get(k);
}
long endTime2 = System.currentTimeMillis();
System.out.println("Second program runtime:" + (endTime2 - startTime2) + "ms");entrySet with Iterator
long startTime3 = System.currentTimeMillis();
Iterator<Map.Entry<String,String>> iter3 = map.entrySet().iterator();
while (iter3.hasNext()) {
Map.Entry<String,String> entry = iter3.next();
key = entry.getKey();
value = entry.getValue();
}
long endTime3 = System.currentTimeMillis();
System.out.println("Third program runtime:" + (endTime3 - startTime3) + "ms");entrySet with for‑each loop
long startTime4 = System.currentTimeMillis();
for (Map.Entry<String,String> entry : map.entrySet()) {
key = entry.getKey();
value = entry.getValue();
}
long endTime4 = System.currentTimeMillis();
System.out.println("Fourth program runtime:" + (endTime4 - startTime4) + "ms");3.2 Traversing only keys
Similar four variants (keySet Iterator, keySet for‑each, entrySet Iterator, entrySet for‑each) are executed, measuring only the time to obtain keys.
3.3 Traversing only values
Four variants (keySet Iterator, keySet for‑each, entrySet Iterator, entrySet for‑each) plus two value‑focused loops (values Iterator and values for‑each) are timed.
Iterator<String> iter5 = map.values().iterator();
while (iter5.hasNext()) {
value = iter5.next();
} for (String v : map.values()) {
// no operation, just iteration
}4. Time Comparison
Charts (omitted here) show the runtime results for each scenario.
5. Summary
From the measurements:
Simple keys (sequential integers) provide faster lookup than complex keys.
When the data set is large, iterating with entrySet for key+value is more efficient than using keySet.
If only values are needed, iterating over values() is the fastest approach.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
