liveBook · Manning

Map.putIfAbsent

As explained in 100 Java Mistakes and How to Avoid Them

Map.putIfAbsent is a Java Map interface method that associates a value when a key has no non-null mapped value and returns the prior value.

Map.putIfAbsent associates a value when a key has no non-null mapping and returns the previous value. Its treatment of null is easy to misread: an existing null mapping is treated as absent, but passing null as the new value can create a null mapping. section 8.4

A repeated call can change the map

var map = new HashMap<String, String>(); map.putIfAbsent("x", null); map.putIfAbsent("x", "1"); The first call can create a null mapping. Because putIfAbsent treats that mapping as absent, the second call replaces it with "1". Therefore, calling the method twice with the same key does not guarantee that the second call has no effect. section 8.4

var map = new HashMap<String, String>();
map.putIfAbsent("x", null);     #1
System.out.println(map);    #2
map.putIfAbsent("x", "1");     #3
System.out.println(map);    #4

Inspect the minimal HashMap sequence that exposes putIfAbsent's two different null behaviors. section 8.4

Use the method deliberately:

  • Avoid null values when possible; putIfAbsent, merge, compute, and computeIfAbsent follow different rules for existing null mappings and null results. section 8.4
  • For concurrently updated maps, prefer a single atomic Map operation instead of a separate get followed by put; use merge for occurrence counting. section 9.12

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Compares putIfAbsent with related Map methods through concrete calls and a null-behavior table, without detailing implementation internals.
Examples
Uses a HashMap null-mapping sequence and contrasts non-atomic get-and-put counting with merge.
What you can do after
Apply putIfAbsent with explicit null-value expectations and replace non-atomic concurrent get-and-put sequences with suitable atomic operations.
How it connects
Connects putIfAbsent with Map, merge, compute, computeIfAbsent, and concurrent collections.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Java Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.