diff --git a/src/share/classes/java/util/HashMap.java b/src/share/classes/java/util/HashMap.java index 1183952eb0653c43071470992ec9581e04a38cb5..454c18f9bd55254da93940b69a84460a54fe4b2a 100644 --- a/src/share/classes/java/util/HashMap.java +++ b/src/share/classes/java/util/HashMap.java @@ -1212,6 +1212,8 @@ public class HashMap extends AbstractMap @Override public V merge(K key, V value, BiFunction remappingFunction) { + if (value == null) + throw new NullPointerException(); if (remappingFunction == null) throw new NullPointerException(); int hash = hash(key); diff --git a/src/share/classes/java/util/Map.java b/src/share/classes/java/util/Map.java index 5c3f954c2a0268977c934562e6ccb3213203ef18..de8bbce01a2b5ed58c9d7393ba93ea3730e2eb25 100644 --- a/src/share/classes/java/util/Map.java +++ b/src/share/classes/java/util/Map.java @@ -600,7 +600,7 @@ public interface Map { * @implSpec * The default implementation is equivalent to, for this {@code map}: *
 {@code
-     * for ((Map.Entry entry : map.entrySet())
+     * for (Map.Entry entry : map.entrySet())
      *     action.accept(entry.getKey(), entry.getValue());
      * }
* @@ -640,7 +640,7 @@ public interface Map { * @implSpec *

The default implementation is equivalent to, for this {@code map}: *

 {@code
-     * for ((Map.Entry entry : map.entrySet())
+     * for (Map.Entry entry : map.entrySet())
      *     entry.setValue(function.apply(entry.getKey(), entry.getValue()));
      * }
* @@ -1110,8 +1110,8 @@ public interface Map { /** * If the specified key is not already associated with a value or is - * associated with null, associates it with the given value. - * Otherwise, replaces the value with the results of the given + * associated with null, associates it with the given non-null value. + * Otherwise, replaces the associated value with the results of the given * remapping function, or removes if the result is {@code null}. This * method may be of use when combining multiple mapped values for a key. * For example, to either create or append a {@code String msg} to a @@ -1121,15 +1121,14 @@ public interface Map { * map.merge(key, msg, String::concat) * } * - *

If the function returns {@code null}, the mapping is removed (or - * remains absent if initially absent). If the function itself throws an - * (unchecked) exception, the exception is rethrown, and the current mapping - * is left unchanged. + *

If the function returns {@code null} the mapping is removed. If the + * function itself throws an (unchecked) exception, the exception is + * rethrown, and the current mapping is left unchanged. * * @implSpec - * The default implementation is equivalent to performing the - * following steps for this {@code map}, then returning the - * current value or {@code null} if absent: + * The default implementation is equivalent to performing the following + * steps for this {@code map}, then returning the current value or + * {@code null} if absent: * *

 {@code
      * V oldValue = map.get(key);
@@ -1137,8 +1136,6 @@ public interface Map {
      *              remappingFunction.apply(oldValue, value);
      * if (newValue == null)
      *     map.remove(key);
-     * else if (oldValue == null)
-     *     map.remove(key);
      * else
      *     map.put(key, newValue);
      * }
@@ -1151,42 +1148,36 @@ public interface Map { * whether the function is applied once atomically only if the value is not * present. * - * @param key key with which the specified value is to be associated - * @param value the value to use if absent + * @param key key with which the resulting value is to be associated + * @param value the non-null value to be merged with the existing value + * associated with the key or, if no existing value or a null value + * is associated with the key, to be associated with the key * @param remappingFunction the function to recompute a value if present - * @return the new value associated with the specified key, or null if none + * @return the new value associated with the specified key, or null if no + * value is associated with the key * @throws UnsupportedOperationException if the {@code put} operation * is not supported by this map * (optional) * @throws ClassCastException if the class of the specified key or value * prevents it from being stored in this map * (optional) - * @throws NullPointerException if the specified key is null and - * this map does not support null keys, or the remappingFunction - * is null + * @throws NullPointerException if the specified key is null and this map + * does not support null keys or the value or remappingFunction is + * null * @since 1.8 */ default V merge(K key, V value, BiFunction remappingFunction) { Objects.requireNonNull(remappingFunction); + Objects.requireNonNull(value); V oldValue = get(key); - if (oldValue != null) { - V newValue = remappingFunction.apply(oldValue, value); - if (newValue != null) { - put(key, newValue); - return newValue; - } else { - remove(key); - return null; - } + V newValue = (oldValue == null) ? value : + remappingFunction.apply(oldValue, value); + if(newValue == null) { + remove(key); } else { - if (value == null) { - remove(key); - return null; - } else { - put(key, value); - return value; - } + put(key, newValue); } + return newValue; } } diff --git a/src/share/classes/java/util/concurrent/ConcurrentMap.java b/src/share/classes/java/util/concurrent/ConcurrentMap.java index 3cb1fe7ea8f50eaa45e1d3b0b8af69897f470e5c..61cda84d93456e673116f6955c1efeed691d7c7e 100644 --- a/src/share/classes/java/util/concurrent/ConcurrentMap.java +++ b/src/share/classes/java/util/concurrent/ConcurrentMap.java @@ -463,9 +463,9 @@ public interface ConcurrentMap extends Map { * {@inheritDoc} * * @implSpec - * The default implementation is equivalent to performing the - * following steps for this {@code map}, then returning the - * current value or {@code null} if absent: + * The default implementation is equivalent to performing the following + * steps for this {@code map}, then returning the current value or + * {@code null} if absent: * *
 {@code
      * V oldValue = map.get(key);
@@ -473,8 +473,6 @@ public interface ConcurrentMap extends Map {
      *              remappingFunction.apply(oldValue, value);
      * if (newValue == null)
      *     map.remove(key);
-     * else if (oldValue == null)
-     *     map.remove(key);
      * else
      *     map.put(key, newValue);
      * }
diff --git a/test/java/util/Map/Defaults.java b/test/java/util/Map/Defaults.java index 46a49a63a517f7b5438bd3019ebfe66a9c081c13..4702601f6f92503288acd975e1bb92603b768e41 100644 --- a/test/java/util/Map/Defaults.java +++ b/test/java/util/Map/Defaults.java @@ -741,7 +741,6 @@ public class Defaults { Collection cases = new ArrayList<>(); cases.addAll(makeMergeTestCases()); - cases.addAll(makeMergeNullValueTestCases()); return cases.iterator(); } @@ -764,32 +763,6 @@ public class Defaults { return cases; } - static Collection makeMergeNullValueTestCases() { - Collection cases = new ArrayList<>(); - - for( Object[] mapParams : makeAllRWMapsWithNulls() ) { - cases.add(new Object[] { mapParams[0], mapParams[1], Merging.Value.OLDVALUE, Merging.Value.NULL, Merging.Merger.NULL, Merging.Value.ABSENT, Merging.Value.NULL }); - } - - for( Object[] mapParams : makeAllRWMapsWithNulls() ) { - cases.add(new Object[] { mapParams[0], mapParams[1], Merging.Value.OLDVALUE, Merging.Value.NULL, Merging.Merger.RESULT, Merging.Value.RESULT, Merging.Value.RESULT }); - } - - for( Object[] mapParams : makeAllRWMapsWithNulls() ) { - cases.add(new Object[] { mapParams[0], mapParams[1], Merging.Value.ABSENT, Merging.Value.NULL, Merging.Merger.UNUSED, Merging.Value.ABSENT, Merging.Value.NULL }); - } - - for( Object[] mapParams : makeAllRWMapsWithNulls() ) { - cases.add(new Object[] { mapParams[0], mapParams[1], Merging.Value.NULL, Merging.Value.NULL, Merging.Merger.UNUSED, Merging.Value.ABSENT, Merging.Value.NULL }); - } - - for( Object[] mapParams : makeAllRWMapsWithNulls() ) { - cases.add(new Object[] { mapParams[0], mapParams[1], Merging.Value.NULL, Merging.Value.NEWVALUE, Merging.Merger.UNUSED, Merging.Value.NEWVALUE, Merging.Value.NEWVALUE }); - } - - return cases; - } - public interface Thrower { public void run() throws T;