computeIfAbsent
public static <K,V> V computeIfAbsent(Map<K,V> map,
K key,
Function<K,V> supplier)
In older versions of java ConcurrentHashMap.computeIfAbsent()
has an issue where threads can contend
on reads even when the value is present. This method provides the same behavior as computeIfAbsent
but avoids such contention.
- Type Parameters:
K
- the key type
V
- the value type
- Parameters:
map
- the map to use for the operation
key
- the key to look up
supplier
- a supplier to be used to generate a new value if one is not already present
- Returns:
- the existing value from the map or the newly added value if no value was previously present