-
Bug
-
Resolution: Fixed
-
P3
-
15
-
b18
-
b06
-
Not verified
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
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
- relates to
-
JDK-8176894 Provide specialized implementation for default methods putIfAbsent, computeIfAbsent, computeIfPresent, compute, merge in TreeMap
-
- Resolved
-