HashMap Iteration in Java
Problem
A HashMap can be iterated three ways depending on what's needed — entrySet() for key-value pairs together, keySet() for just the keys, and values() for just the values.
Given a HashMap of scores, print its contents three ways: as key-value entries, as keys only, and as values only.
Java Program
import java.util.HashMap;
import java.util.Map;
public class HashMapIteration {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 90);
scores.put("Bob", 82);
scores.put("Charlie", 95);
System.out.println("Entries:");
for (Map.Entry<String, Integer> entry : scores.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
System.out.println("Keys:");
for (String key : scores.keySet()) {
System.out.println(key);
}
System.out.println("Values:");
for (int value : scores.values()) {
System.out.println(value);
}
}
}Output
Core Logic
Each of entrySet(), keySet(), and values() returns a different view over the same underlying map, so looping over any of them walks the same entries in the same order, just exposing different parts of each one.
- 1
entrySet()returns a view ofMap.Entryobjects, each carrying both a key and its value together. - 2
keySet()returns a view of just the keys, with no direct access to their values from that loop alone. - 3
values()returns a view of just the values, with no way to tell which key each one came from. - 4All three loops walk the map's internal storage in the exact same order — only what each view exposes per entry differs.
Key Point: HashMap's iteration order is not insertion order and isn't guaranteed to stay the same across different JVM versions or map sizes — it's driven by each key's hash code, not by when it was added. Code that needs a predictable order should use LinkedHashMap or TreeMap instead.
Why: Each style visits every one of the map's n entries exactly once; entrySet()/keySet()/values() are live views over the map's own storage, not copies, so none of them allocate space proportional to n.
Key Concepts
Approach 2: Java 8
import java.util.HashMap;
import java.util.Map;
public class HashMapIterationStream {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 90);
scores.put("Bob", 82);
scores.put("Charlie", 95);
System.out.println("forEach:");
// Key and value arrive as two separate lambda parameters, no Map.Entry needed
scores.forEach((key, value) -> System.out.println(key + " = " + value));
}
}
Output
Core Logic
Map's own forEach() offers a fourth way to walk the map — a single call that hands both the key and value to a lambda, with no explicit view or loop needed.
- 1
scores.forEach((key, value) -> ...)passes a two-argument lambda that runs once per entry. - 2Unlike
entrySet(), there's noMap.Entryobject to unpack — the key and value arrive as two separate lambda parameters directly. - 3The map still supplies entries in its own internal order — the same order the other three styles visit them in.
- 4Printing
key + " = " + valueinside the lambda reproduces the same output as the entrySet() loop.
forEach((key, value) -> ...) prints Bob = 82, Alice = 90, Charlie = 95 — the same entries, in the same order, as the entrySet() loop.Key Point: Map.forEach() is a default method added to the Map interface itself in Java 8 — it's not a stream operation, but it is the modern, most concise way to visit both keys and values together without naming an intermediate view.
Why: forEach() still visits each of the map's n entries exactly once, passing key and value directly to the lambda with no extra storage.