Map.merge Javadoc is confusing

David Holmes david.holmes at oracle.com
Thu Nov 21 18:53:01 UTC 2013


On 22/11/2013 4:17 AM, Louis Wasserman wrote:
> 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.

There seems to be a hole in the spec regarding how a null value 
parameter is handled. The default implementation treats it as-if the 
remappingFunction returns null. Not unreasonable but not specified.

David




More information about the core-libs-dev mailing list