What does computeIfAbsent do, and why is it the idiomatic way to build multimaps (Map of List)?
answer
- computeIfAbsent: get-or-create-and-store, lazy
- multimap: computeIfAbsent(k, x -> new ArrayList<>()).add(v)
- created exactly once per key
- don't modify the map inside the function
- null result => no entry created
basics
~20 scomputeIfAbsent(key, fn) returns the existing value for key, or if it is missing, runs fn to create a value, stores it, and returns it. It is perfect for grouping: it makes the empty list once, then you add to it.
solid answer
~50 s`computeIfAbsent(key, mappingFunction)` checks the map: if `key` already has a non-null value it returns that and does nothing else; if absent (or null), it calls `mappingFunction.apply(key)`, stores the result (unless the result is null), and returns it. The mapping function is **lazy** — invoked only on a miss — and receives the key. The canonical use is the 'group-by' / multimap pattern: `map.computeIfAbsent(k, key -> new ArrayList<>()).add(item);`. This collapses the get-null-check-create-put-then-add idiom into one line and guarantees the list is created exactly once per key. Caveats: the mapping function should not modify the map (`HashMap` throws `ConcurrentModificationException` on a best-effort basis if it detects that), and on `ConcurrentHashMap`, where it must not, the function runs under a lock per bin, so keep it short and non-blocking. If the function returns null, no mapping is created.
go deeper
Recognizes computeIfAbsent as get-or-create and can write the multimap one-liner.
Explains lazy invocation, exactly-once creation, and why getOrDefault is wrong for grouping.
Discusses the no-self-modification rule and ConcurrentHashMap atomicity/locking implications.
Reasons about deadlock risks with nested computeIfAbsent on concurrent maps and chooses data structures to keep the mapping function cheap and reentrancy-safe.
### The pattern it replaces A very common need is: 'get the value for this key; if there isn't one, create a sensible initial value, store it, and use it.' Written by hand: ```java List<Item> list = map.get(key); if (list == null) { list = new ArrayList<>(); map.put(key, list); } list.add(item); ``` Four lines, easy to get wrong. `computeIfAbsent` does it in one. ### What computeIfAbsent does, precisely `map.computeIfAbsent(key, mappingFunction)`: 1. If `key` is present with a **non-null** value, return that value; the function is **not** called. 2. Otherwise call `mappingFunction.apply(key)` (it receives the key). 3. If the function returns a **non-null** result, store `key -> result` and return it. 4. If the function returns **null**, leave the map unchanged and return null. The function is a `Function<K, V>` — it is **lazy** (runs only on a miss), which is why it is preferred over `putIfAbsent` when the default is expensive to build. ### The multimap / group-by idiom A 'multimap' is a map where each key holds a *collection* of values. With `computeIfAbsent`: ```java Map<String, List<String>> byFirstLetter = new HashMap<>(); for (String name : names) { String letter = name.substring(0, 1); byFirstLetter.computeIfAbsent(letter, k -> new ArrayList<>()).add(name); } ``` On the first occurrence of a letter, the empty list is created and stored; on later occurrences the **same** list is returned. The `.add(name)` then runs on that list. The empty list is created **exactly once** per key — no duplicate allocations, no null checks. ### Important caveats - **No self-modification:** the `Map` contract says the mapping function should not modify the map during computation. `HashMap` and `TreeMap` throw `ConcurrentModificationException` on a best-effort basis if they detect it, as a nested `computeIfAbsent` on JDK 27 shows on both. The default implementation in `Map` promises no detection at all. - **Concurrency:** on `ConcurrentHashMap`, the whole call is atomic and the function runs at most once. Other threads' updates may block while it runs, so its documentation says the computation "should be short and simple" and that the function "must not modify this map". A nested update can hang: the documented `IllegalStateException` is for a computation that "detectably attempts a recursive update to this map that would otherwise never complete". On JDK 27 the outcome depends on the call, the bin and the key, so it can throw, lose an update or finish with no error. A nested `computeIfAbsent` into the same empty bin throws that exception; enough nested calls to force a resize can throw it too, or leave the new entry where `get` cannot find it; one nested call into another bin completed. Two threads nesting in opposite order deadlocked, so a passing test proves nothing. - **Null result = no entry:** returning null from the function creates nothing, and a key already mapped to null stays mapped to null. - **Existing null:** like `putIfAbsent`, it treats a key currently mapped to null as absent, so the function runs. ### Why it beats getOrDefault here `getOrDefault(k, new ArrayList<>())` would allocate a **new list every call** and never store it, so the added items would be lost. `computeIfAbsent` allocates lazily and persists the value.
- Why is computeIfAbsent(k, x -> new ArrayList<>()).add(v) correct but getOrDefault(k, new ArrayList<>()).add(v) buggy?computeIfAbsent stores the new list in the map and returns it, so subsequent adds hit the same list. getOrDefault returns a fresh, unstored list each time, so every added item is dropped.
- What happens if the mapping function returns null?No mapping is created and computeIfAbsent returns null; the key stays absent.
- Using getOrDefault(k, new ArrayList<>()).add(v) (list is never stored)
- Calling computeIfAbsent recursively on the same ConcurrentHashMap
- Believing the function runs even when the key is present
- Doing heavy/blocking work in the mapping function on a ConcurrentHashMap
- Map.computeIfAbsent
- Java 1.8
Assumes JDK 27; computeIfAbsent is a Map default method since Java 8, and this contract reads the same at JDK 25.
context
- Map (Java SE 27 & JDK 27)docs.oracle.com
- ConcurrentHashMap (Java SE 27 & JDK 27)docs.oracle.com
- HashMap (Java SE 27 & JDK 27)docs.oracle.com