computeIfAbsent must not return null
If a mapping function passed to computeIfAbsent returns null, no mapping is recorded. Code expecting a cached value can repeatedly recompute or unexpectedly receive null.
A mapping function that updates the same
ConcurrentHashMapcan throwIllegalStateExceptionwith a recursive update message, so keep it side-effect free. Seen another edge case with this? Add it below.If the function given to computeIfAbsent returns null, no mapping is created, so the next lookup computes again. Make the function return a non-null value or handle the absent case separately.
Returning null from computeIfAbsent leaves the key unmapped, so callers that expect a cached entry can get null or recompute. Guarantee non-null results from the mapping function.
Specific and to the point: "computeIfAbsent must not return null". Worth a look if you touch java / map.
A null result from the mapping function is treated as no entry, so caching silently fails for that key. Check the computed value before it reaches the map.
Good to see "computeIfAbsent must not return null" written down. Handy reference for anyone working with java / map.
Clear note on java / map. "computeIfAbsent must not return null" is the kind of detail that is easy to overlook.
"computeIfAbsent must not return null" is a useful thing to know about with java / map. Thanks for sharing it.
Specific and to the point: "computeIfAbsent must not return null". Worth a look if you touch java / map.