Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8024331

j.u.Map.computeIfPresent() default/nondefault implementations don't throw NPE if the remappingFunction is null and the key is absent

XMLWordPrintable

      Method
      http://download.java.net/jdk8/docs/api/java/util/Map.html#computeIfPresent%28K,%20java.util.function.BiFunction%29

      specifies:

      Throws:
          NullPointerException - if ... the remappingFunction is null


      NPE is not thrown if the given key is not present, consider the following code sample:

      ---
              new LinkedHashMap().computeIfPresent("a", (BiFunction) null);
              new TreeMap().computeIfPresent("a", (BiFunction) null);
              new HashMap().computeIfPresent("a", (BiFunction) null);

              new HashMap(){{put("a", "a");}}.computeIfPresent("a", (BiFunction) null);
      ---

      Only the last line will throw NPE.

      The following tests will fail:
      api/java_util/TreeMap/Map8Tests.html#Map8Tests[computeIfPresent_mappingFunctionIsNull]
      api/java_util/Collections/synchronizd/SynchronizedSortedMap8Tests.html#SynchronizedSortedMap8Tests[computeIfPresent_mappingFunctionIsNull]
      api/java_util/Collections/synchronizd/SynchronizedMap8Tests.html#SynchronizedMap8Tests[computeIfPresent_mappingFunctionIsNull]
      api/java_util/IdentityHashMap/Map8Tests.html#Map8Tests[computeIfPresent_mappingFunctionIsNull]
      api/java_util/HashMap/Map8Tests.html#Map8Tests[computeIfPresent_mappingFunctionIsNull]
      api/java_util/LinkedHashMap/Map8Tests.html#Map8Tests[computeIfPresent_mappingFunctionIsNull]
      api/java_util/WeakHashMap/Map8Tests.html#Map8Tests[computeIfPresent_mappingFunctionIsNull]

            bpb Brian Burkhalter
            dbessono Dmitry Bessonov
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: