Java Reference ยท Collections
Java Map.entrySet()
Reference for Map.entrySet(): iterate keys and values together, update values through entries, remove while iterating and sort entries by value.
entrySet() returns a Set<Map.Entry<K, V>> view of a map: one entry object for each key-value pair. It is the standard way to loop over keys and values together, because it avoids a second lookup per key (which for (K k : map.keySet()) map.get(k) would do).
The set is a live view: removing an entry removes the mapping, and calling setValue() on an entry writes through to the map. You cannot add to it. The iteration order is that of the map: unspecified for HashMap, insertion order for LinkedHashMap, sorted for TreeMap.
for (Map.Entry<String, Integer> e : map.entrySet()) { System.out.println(e.getKey() + " = " + e.getValue()); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
Set<Map.Entry<K, V>> entrySet()
Map.Entry member |
Description |
|---|---|
K getKey() |
The key. |
V getValue() |
The value. |
V setValue(V value) |
Replaces the value in the backing map and returns the old one. Optional operation. |
static comparingByKey(), comparingByValue() |
Comparators for sorting entries (plus overloads taking a Comparator). |
static <K,V> Map.Entry<K,V> copyOf(Map.Entry<? extends K, ? extends V>) |
Immutable copy of an entry (Java 17). |
Returns: a live set view of the mappings.
Usage notes
- Prefer
entrySet()overkeySet()plusget()when you need both key and value: one lookup less per element. - Modifying while iterating: use
Iterator.remove(),entrySet().removeIf(...)orvalues().removeIf(...); callingmap.remove()ormap.put()of a new key in the loop throwsConcurrentModificationException(best effort). - Entries may be reused or invalidated by some implementations after the iteration moves on or the map changes, so do not store them. Copy with
Map.entry(k, v)ornew AbstractMap.SimpleEntry<>(e). setValue()writes through for ordinary maps. Entries fromMap.of(),Map.entry()and thefirstEntry()family ofTreeMapare immutable.- Order follows the map implementation; sort a copy of the entries (as above) when you need ordering by value.
Map.forEach((k, v) -> ...)is shorter when you do not need to modify the map.- Streams:
map.entrySet().stream()is the starting point for filtering and transforming maps. - Thread safety: the view of a
ConcurrentHashMapis weakly consistent and never throwsConcurrentModificationException.
Related
- Map, HashMap, TreeMap, Map.of() and Map.entry()
- Lessons: HashMap, Stream collectors