Map.merge Javadoc is confusing

Louis Wasserman lowasser at google.com
Thu Nov 21 18:17:57 UTC 2013


The Javadoc for
Map.merge<http://download.java.net/jdk8/docs/api/java/util/Map.html#merge-K-V-java.util.function.BiFunction->states
that its default implementation is equivalent to:

V oldValue = map.get(key);
 V newValue = (oldValue == null) ? value :
              remappingFunction.apply(oldValue, value);
 if (newValue == null)
     map.remove(key);
 else if (oldValue == null)
     map.remove(key);
 else
     map.put(key, newValue);

I'm somewhat confused by the second branch of this if statement, which is
reached if newValue is nonnull and oldValue is null.  Does this really
*remove* the entry for that key?  I would have expected there to be only
two branches: either remove the entry if newValue is null, or put newValue
if it's nonnull.

-- 
Louis Wasserman



More information about the core-libs-dev mailing list