Useless null check in HashMap.merge()

Martin Buchholz martinrb at google.com
Thu Jul 5 15:26:05 UTC 2018


The null check will be removed as part of the next jsr166 integration.
http://cr.openjdk.java.net/~martin/webrevs/jdk/jsr166-integration/miscellaneous/index.html

On Wed, Jul 4, 2018 at 2:42 AM, Zheka Kozlov <orionllmain at gmail.com> wrote:

> I noticed dead code in java.util.HashMap.merge():
>
> public V merge(K key, V value,
>                    BiFunction<? super V, ? super V, ? extends V>
> remappingFunction) {
>     if (value == null)
>         throw new NullPointerException();
>
>     ...
>
>     if (value != null) { *// Condition ' value != null' is always true*
>         if (t != null)
>             t.putTreeVal(this, tab, hash, key, value);
>         else {
>             tab[i] = newNode(hash, key, value, first);
>             if (binCount >= TREEIFY_THRESHOLD - 1)
>                 treeifyBin(tab, hash);
>         }
>         ++modCount;
>         ++size;
>         afterNodeInsertion(true);
>     }
>     return value;
> }
>
> The code in the if branch will never be executed because `value` was
> previously checked at the beginning of the method.
>
> Is this a mistake?
>


More information about the core-libs-dev mailing list