Map.compute() confusing Javadoc

Mike Duigou mike.duigou at oracle.com
Tue Jul 22 00:33:20 UTC 2014


Hi Roman;

Somewhat unfortunately, "just return null" is what the default and all conforming implementations do of compute do when presented with a Map containing a mapping to null and a mapping function returning null. The specification of the new Java 8 Map methods does not always handle maps containing null values as clean as might be wished. This is mostly to maintain consistent semantics with some pre-java 8 ConcurrentHashMap operations. The chain of debatable decisions all starts with "putIfAbsent()" and then tries to proceed consistently to the other new default operations. In general a null-value mapping in a Map is treated as absent--the same way as CHM would treat a get() returning null. Every attempt I made at improving the behaviour of mapping to null values ended up being weirder and more mysterious than what Java 8 shipped with.

Sorry.

Mike

On Jul 20 2014, at 15:25 , David Holmes <David.Holmes at oracle.com> wrote:

> See:
> 
> http://mail.openjdk.java.net/pipermail/core-libs-dev/2013-June/018251.html
> 
> and related bug report.
> 
> David
> 
> On 21/07/2014 6:01 AM, Paul Sandoz wrote:
>> Begin forwarded message:
>>> -------- Пересылаемое сообщение--------
>>> 17.07.2014, 19:20, "Roman Leventov" <leventov at ya.ru>:
>>> 
>>> Map.compute() Javadoc has the following paragraph:
>>> --------
>>> The default implementation is equivalent to performing the following steps for this map, then returning the current value or null if absent:
>>> 
>>>  V oldValue = map.get(key);
>>>  V newValue = remappingFunction.apply(key, oldValue);
>>>  if (oldValue != null ) {
>>>     if (newValue != null)
>>>        map.put(key, newValue);
>>>     else
>>>        map.remove(key);
>>>  } else {
>>>     if (newValue != null)
>>>        map.put(key, newValue);
>>>     else
>>>        return null;
>>>  }
>>> --------
>>> 
>>> But this code don't correspond neither verbal description of the behaviour nor the actual default implementation in java.util.Map. If the oldValue is null and newValue is null, map should still remove the key, not to just `return null;`
>>> 
>> 




More information about the core-libs-dev mailing list