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

TreeMap.computeIfAbsent deviates from spec

    XMLWordPrintable

    Details

    • Subcomponent:
    • Introduced In Build:
      b18
    • Introduced In Version:
      15
    • Resolved In Build:
      b06
    • Verification:
      Not verified

      Description

      A DESCRIPTION OF THE PROBLEM :
      Mapping a key-value to `null` in a TreeMap and then invoking computeIfAbsent with that key will return null instead of the provided default value and also fail to establish the new mapping.

      Appears to have been caused by https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8176894

      Spec: "If the specified key is not already associated with a value (or is mapped to null)"
      Interface default impl: `if ((v = get(key)) == null)`

      REGRESSION : Last worked in version 14

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      see test case

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      all four lines printed should say "default"
      ACTUAL -
      the treemap maintains the mapping to `null`

      ---------- BEGIN SOURCE ----------
      import java.util.HashMap;
      import java.util.TreeMap;

      class Scratch {
      public static void main(String[] args) {
      TreeMap treemap = new TreeMap();
      treemap.put("a", null);
      System.out.println(treemap.computeIfAbsent("a", key -> "default"));
      System.out.println(treemap.get("a"));

      HashMap hashmap = new HashMap();
      hashmap.put("a", null);
      System.out.println(hashmap.computeIfAbsent("a", key -> "default"));
      System.out.println(hashmap.get("a"));
      }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

              Assignee:
              tvaleev Tagir Valeev
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: