To iterate over a map in Java, loop over map.entrySet() when you need both keys and values. Use keySet() when you only need keys and values() when you only need values. Since Java 8 you can also call map.forEach((key, value) -> ...). This guide shows four ways to iterate map java code, with a runnable example for each.
A map is not a Collection, so you cannot put it directly in an enhanced for loop. You always iterate over one of its views or use a method that visits each entry.
All examples use the same small map. LinkedHashMap keeps insertion order, so the output is predictable. A plain HashMap would give the same entries in an unspecified order.
entrySet() returns a view of Map.Entry objects. Each entry gives you getKey() and getValue() from one pass, with no extra lookups. This is the default choice.
You can shorten the type with var: for (var entry : stock.entrySet()).
If you only need keys, loop over keySet(). If you only need values, loop over values().
Avoid calling stock.get(name) inside a keySet() loop just to read the value. For a HashMap that is an extra hash lookup per entry, and for a TreeMap it is an extra tree search. Use entrySet() instead.
Since Java 8, Map.forEach takes a BiConsumer, so you receive the key and value as two lambda parameters.
This is concise, but you cannot use break or continue, and you cannot change local variables from inside the lambda because captured locals must be effectively final.
A stream over entrySet() is useful when you also want to filter, sort or collect.
An explicit Iterator is the only safe way to remove entries while you loop:
For a simple condition, stock.entrySet().removeIf(e -> e.getValue() < 5) or stock.values().removeIf(v -> v < 5) does the same in one line.
| Need | Use |
|---|---|
| Keys and values | entrySet() loop |
| Only keys | keySet() loop |
| Only values | values() loop |
| Short action, no early exit | forEach with a lambda |
| Filter, sort or collect | Stream over entrySet() |
| Remove while looping | Iterator.remove() or removeIf |
Iteration order depends on the map type: HashMap has no guaranteed order, LinkedHashMap follows insertion order, and TreeMap follows key order.
Map does not extend Iterable. You loop over one of its collection views: entrySet(), keySet() or values().
Adding or removing entries through the map during iteration usually throws ConcurrentModificationException. Remove through the iterator, use removeIf, or iterate over a copy. Changing a value through entry.setValue(...) is allowed.
For most maps, entrySet() and forEach perform about the same, and both beat looping over keySet() with get. Choose by readability unless a profiler shows a problem.
Use a TreeMap, or stream the entries and sort them with Map.Entry.comparingByKey() or comparingByValue().
Use entrySet() as the default way to iterate a map in Java, keySet() or values() when you need one side only, forEach for short actions, and streams for filtering and sorting. Remove entries during a loop only with Iterator.remove() or removeIf.