Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Collections

Java putIfAbsent(), compute() and replace()

Reference for Map.putIfAbsent(), compute(), computeIfPresent(), replace() and replaceAll(): conditional updates with exact return values and null rules.

Besides put() and remove(), Map has a family of conditional update methods added in Java 8. Each does a "look up, decide, write" step in a single call, so you avoid writing the check yourself. On ConcurrentHashMap they are also atomic for the key.

Quick guide: putIfAbsent stores a value only if the key has none. compute always recomputes. computeIfPresent recomputes only when a value exists. replace swaps a value only for an existing key (optionally only if it equals an expected value). replaceAll rewrites every value.

Java
map.putIfAbsent("k", 1);
map.compute("k", (key, old) -> old == null ? 1 : old + 1);

Try it

Java putIfAbsent, replace and computeIfPresent

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 compute handles both cases

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
default V putIfAbsent(K key, V value)
default V compute(K key, BiFunction<? super K, ? super V, ? extends V> f)
default V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> f)
default V replace(K key, V value)
default boolean replace(K key, V oldValue, V newValue)
default void replaceAll(BiFunction<? super K, ? super V, ? extends V> f)
Method Returns
putIfAbsent The previous value, or null if the key was absent (and the value was stored).
compute The new value, or null if the entry was removed or stays absent. The function gets (key, oldValueOrNull).
computeIfPresent The new value, or null if the key was absent or the entry was removed. The function gets only non-null old values.
replace(key, value) The previous value, or null if there was no mapping (nothing stored).
replace(key, old, new) true if the value equalled old and was replaced.
replaceAll Nothing; every value becomes the function result.

Throws: NullPointerException for a null function (and for nulls the map does not allow); UnsupportedOperationException on immutable maps.

Usage notes

  • Since Java 8 (replaceAll and the three-argument replace included).
  • compute returning null removes the entry, and does nothing when the key was absent.
  • Do not modify the map inside the function; HashMap may throw ConcurrentModificationException.
  • putIfAbsent evaluates its value eagerly. For expensive or allocating values use computeIfAbsent().
  • putIfAbsent returns the old value, not the stored one, so map.putIfAbsent(k, new ArrayList<>()).add(x) can throw NullPointerException the first time.
  • Null values: HashMap treats a key mapped to null as absent for putIfAbsent, compute* and merge.
  • Atomic only on concurrent maps: ConcurrentHashMap runs these atomically per key; HashMap does not, even though the call looks like one step.
  • For counting, merge() is usually shorter than compute().