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.
map.putIfAbsent("k", 1); map.compute("k", (key, old) -> old == null ? 1 : old + 1);
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
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 (
replaceAlland the three-argumentreplaceincluded). computereturningnullremoves the entry, and does nothing when the key was absent.- Do not modify the map inside the function;
HashMapmay throwConcurrentModificationException. putIfAbsentevaluates its value eagerly. For expensive or allocating values usecomputeIfAbsent().putIfAbsentreturns the old value, not the stored one, somap.putIfAbsent(k, new ArrayList<>()).add(x)can throwNullPointerExceptionthe first time.- Null values:
HashMaptreats a key mapped tonullas absent forputIfAbsent,compute*andmerge. - Atomic only on concurrent maps:
ConcurrentHashMapruns these atomically per key;HashMapdoes not, even though the call looks like one step. - For counting,
merge()is usually shorter thancompute().