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)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…