Skip to main content
Clarified what is returned
Source Link
Roger Lindsjö
  • 11.6k
  • 1
  • 46
  • 56

The

java.util.concurrent.ConcurrentMap 

and from Java 8

Java.util.Map

has

putIfAbsent(K key, V value) 

which returns the existing value mapped to key or, and if that is null inserts the given value and null. So if no value is mappedexists for the key. returns null and inserts the given value, otherwise returns existing value

If you need lazy evaluation of the value there is

computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)

The

java.util.concurrent.ConcurrentMap 

and from Java 8

Java.util.Map

has

putIfAbsent(K key, V value) 

which returns the value mapped to key or inserts the given value and null if no value is mapped for the key.

If you need lazy evaluation of the value there is

computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)

The

java.util.concurrent.ConcurrentMap 

and from Java 8

Java.util.Map

has

putIfAbsent(K key, V value) 

which returns the existing value, and if that is null inserts given value. So if no value exists for key returns null and inserts the given value, otherwise returns existing value

If you need lazy evaluation of the value there is

computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
Java 8 standard and lazy methods
Source Link
Roger Lindsjö
  • 11.6k
  • 1
  • 46
  • 56

The

java.util.concurrent.ConcurrentMap 

and from Java 8

Java.util.Map

has

putIfAbsent(K key, V value) 

which returns the value mapped to key or inserts the given value and null if no value is mapped for the key.

If you need lazy evaluation of the value there is

computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)

The

java.util.concurrent.ConcurrentMap 

has

putIfAbsent(K key, V value) 

returns the value mapped to key or inserts the given value and null if no value is mapped for the key.

The

java.util.concurrent.ConcurrentMap 

and from Java 8

Java.util.Map

has

putIfAbsent(K key, V value) 

which returns the value mapped to key or inserts the given value and null if no value is mapped for the key.

If you need lazy evaluation of the value there is

computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
Fix description from comments.
Source Link
Roger Lindsjö
  • 11.6k
  • 1
  • 46
  • 56

The

java.util.concurrent.ConcurrentMap 

has

putIfAbsent(K key, V value) 

returns the value mapped to key or inserts the given value and returns this inserted valuenull if no value is mapped for the key.

The

java.util.concurrent.ConcurrentMap 

has

putIfAbsent(K key, V value) 

returns the value mapped to key or inserts the given value and returns this inserted value if no value is mapped for the key.

The

java.util.concurrent.ConcurrentMap 

has

putIfAbsent(K key, V value) 

returns the value mapped to key or inserts the given value and null if no value is mapped for the key.

Source Link
Roger Lindsjö
  • 11.6k
  • 1
  • 46
  • 56
Loading