Java ProgramsCollectionsHashMap Iteration

HashMap Iteration in Java

beginner·  Collections  ·  Map

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.

Input
put("Alice", 90), put("Bob", 82), put("Charlie", 95)
Output
Entries: Bob = 82, Alice = 90, Charlie = 95 ...

Java Program

Java
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

Entries: Bob = 82 Alice = 90 Charlie = 95 Keys: Bob Alice Charlie Values: 82 90 95

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.

How It Works
  1. 1entrySet() returns a view of Map.Entry objects, each carrying both a key and its value together.
  2. 2keySet() returns a view of just the keys, with no direct access to their values from that loop alone.
  3. 3values() returns a view of just the values, with no way to tell which key each one came from.
  4. 4All three loops walk the map's internal storage in the exact same order — only what each view exposes per entry differs.
The map holds three entries; whichever view is iterated, the three underlying entries always appear in the same relative order — here that's Bob, then Alice, then Charlie.
💡

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.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

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

HashMapentrySet()keySet()values()

Approach 2: Java 8

Java
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

forEach: Bob = 82 Alice = 90 Charlie = 95

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.

How It Works
  1. 1scores.forEach((key, value) -> ...) passes a two-argument lambda that runs once per entry.
  2. 2Unlike entrySet(), there's no Map.Entry object to unpack — the key and value arrive as two separate lambda parameters directly.
  3. 3The map still supplies entries in its own internal order — the same order the other three styles visit them in.
  4. 4Printing key + " = " + value inside the lambda reproduces the same output as the entrySet() loop.
For the same map, 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.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

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.

Key Concepts

Map.forEach()BiConsumer

Related Programs