TreeMap in JDK15 deviates from spec

Stuart Marks stuart.marks at oracle.com
Wed Jan 13 21:52:46 UTC 2021


Thanks for filing this!

Tagir Valeev picked this up and has filed a PR:

https://git.openjdk.java.net/jdk/pull/2058

s'marks


On 1/11/21 10:05 AM, Phil Smith wrote:
> Hello, I submitted bug report 9068554 recently, but I consider this a
> pretty big issue and I'd like for this not to get lost in triage.
> 
> With JDK-8176894, a specialized implementation for computeIfAbsent was
> added to TreeMap, however it doesn't handle null values correctly.
> Spec states a mapping to null is equivalent to no mapping being
> present, however code will `return t.value;` without checking that
> value. The interface default, for reference, looks like `if ((v =
> get(key)) == null)`.
> 
> A simple repro follows
> 
> 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"));
> 
> Phil
> 


More information about the core-libs-dev mailing list