map and null values

Boaz Nahum boaznahum at gmail.com
Wed Jan 2 12:47:33 PST 2013


>
> Consistency here is an impossible goal, since there are already
> inconsistencies present in how the JDK classes handle null, so any new
> functionality will be inconsistent with something already.  So we have to
> pick which of the existing strategies we prefer to extend.  The alternative
> is never adding anything.  Surely you don't want that.
>
> Well I didn't understand it till I tried to write my implentation(much
longer.. ) that satisfies every body:

default V computeIfAbsent(K key, Function<? super K, ? extends V>
mappingFunction) {

        if (containsKey(key)) {
            return get(key);
        } else {

            V newValue = mappingFunction.apply(key);

            if (*newValue != null*) {

                V v = put(key, newValue);

                return v == null ? newValue : v;
            }
            else {
                return null;
            }
        }

    }

Then I realized that in case of newValue == null then I can't distinguish
between valid null to 'non valid' null

One last minor thing, the name 'computeIfAbsent' does not imply that it
actually change the map.

Thank you.

Boaz


More information about the lambda-dev mailing list