News for hackers. Stuff that matters.
Re:Map.merge() – One method to rule them all by twic on Sunday March 31, 2019 @11:25AM (#19536046)
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.
Re:Map.merge() – One method to rule them all by barrkel on Sunday March 31, 2019 @11:49AM (#19536188)
Your API works less well in concurrent map scenarios, though.