3 ms·
The one thing that frustrates me about merge is that it returns the value inserted, rather than the value being replaced (as Map::put does). There are good use
by twic 8y ago
The one thing that frustrates me about merge is that it returns the value inserted, rather than the value being replaced (as Map::put does).
There are good uses cases for what it does - but there are also good use cases for returning the previous value, and when i have one of those, i can't use merge, and have to fall back to a get followed by a put.
Perhaps what i would really like is a Rust-style entry API, where i could use a key to obtain a Map.Entry, which i could then inspect and mutate how i liked. It might look like:
Entry<K, V> getOrCreateEntry(K key, Function<? super K, ? extends V> mappingFunction)
And then i could write:
void sell(Potato) throws FarmException { ...}
Map<Field, Potato> potatoesByField = new HashMap<>();
for (var potato: getAllPotatoes()) {
var fieldAndPotato = potatoesByField.getOrCreateEntry(potato.getField(), f -> potato);
var oldPotato = fieldAndPotato.getValue();
if (potato.getWeight() > oldPotato.getWeight()) {
potatoAndField.setValue(potato);
sell(oldPotato);
} else {
sell(potato);
}
}
To collect my prize potatoes into a map, and sell the rest. Still ugly, but hey, at least it's not Go.
- barrkel 8y agoYour API works less well in concurrent map scenarios, though.