Map.merge Javadoc is confusing

Henry Jen henry.jen at oracle.com
Sat Nov 23 00:01:38 UTC 2013


On 11/21/2013 06:33 PM, David Holmes wrote:
> On 22/11/2013 5:02 AM, Louis Wasserman wrote:
>> While I agree that case should be specified, I'm not certain I follow why
>> that's what's going on here.
>>
>> The weird condition is that if oldValue is null, not value; oldValue
>> is the
>> old result of map.get(key).  The Javadoc seems not just unspecified, but
>> actively wrong.  Here's a worked example:
>>
>> Map<String, Integer> map = new HashMap<>();
>> map.merge("foo", 3, Integer::plus);
>> Integer fooValue = map.get("foo");
>>
>> Going through the Javadoc-specified default implementation:
>>
>>    V oldValue = map.get(key); // oldValue is null
>>    V newValue = (oldValue == null) ? value :
>>                 remappingFunction.apply(oldValue, value);
>>       // newValue is set to value, which is 3
>>    if (newValue == null) // newValue is nonnull, branch not taken
>>        map.remove(key);
>>    else if (oldValue == null) // oldValue is null, branch taken
>>        map.remove(key); // removes the entry from the map
>>    else // else if was already triggered, branch not taken
>>        map.put(key, newValue);
>>

Seems like a document bug to me, we should fix this @implSpec.

Cheers,
Henry




More information about the core-libs-dev mailing list