Why doesn't Java have a putIfAbsent(key, supplier) method in Map?
Isn't computeIfAbsent
what you want?
If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.
Implementation is something like:
if (map.get(key) == null) {
V newValue = mappingFunction.apply(key);
if (newValue != null) {
map.put(key, newValue);
}
}
So it's not exactly the Supplier<V>
signature you've posted but close to that. Having key as argument in the mapping function definitely makes sense.