I don't understand why Map.compute()
and Map.computeIfPresent()
take BiFunction
parameters as well as Map.computeIfAbsent()
a Function
:
V compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
I'd expect an ordinary Function<? super V, ? extends V>
, mapping the old value to a new value, resp. a Supplier<? extends V>
for the new value. The caller already has the key (first argument) so the function or the supplier can already make use of it. All examples I found don't use the key. The reasons that come to my mind:
- the key must be (effectively)
final
-- that's easy to manage - there are some fancy easy-to-use method references
But I don't believe these are viable reasons for this design. Do you have any ideas?