Map.merge Javadoc is confusing

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

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


More information about the core-libs-dev mailing list