Skip to content
Tutorials Center
My learning

Topics

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.

Java
for (Map.Entry<String, Integer> e : map.entrySet()) {
    System.out.println(e.getKey() + " = " + e.getValue());
}

Try it

Java Looping over entries

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.

Java Sorting entries and entry pitfalls

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

Java
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() over keySet() plus get() when you need both key and value: one lookup less per element.
  • Modifying while iterating: use Iterator.remove(), entrySet().removeIf(...) or values().removeIf(...); calling map.remove() or map.put() of a new key in the loop throws ConcurrentModificationException (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) or new AbstractMap.SimpleEntry<>(e).
  • setValue() writes through for ordinary maps. Entries from Map.of(), Map.entry() and the firstEntry() family of TreeMap are 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 ConcurrentHashMap is weakly consistent and never throws ConcurrentModificationException.