map and null values

Boaz Nahum boaznahum at
Tue Jan 1 23:22:06 PST 2013

>From looking on Map::computeIfAbsent and Map::computeIfPresent

it seems that it was greed that get(key)==null  and ! containsKey(key) are
the same (or in other words null values are not interesting)

It is quite sad for some of us, And it is not consistent with methods
Map::putIfAbsent and Map::replace

Also the assumption the null values are not legal is so strong, that the
implementation of replace::

 default boolean replace(K key, V oldValue, V newValue) {
        if (*!containsKey(key) || !get(key).equals(oldValue)*)
            return false;
        put(key, newValue);
        return true;

throws NPE exception in the example below:

mss.put("null", null);

mss.replace("null", null, "null");

Well, I not saying that such decision is not legitimate - but it should be
declared loudly at top of class document.

One last thing, when I suggested,
I was long explainded why it is bad idea(atomic and so), now bunch of
such methods were added to Map.  'computeIfAbsent' is almost what I was
request but from some reason assume that caller want to insert the value
into map if it absent.

Happy New Year

More information about the lambda-dev mailing list