RFR: 8293630: Simplify TreeMap.get() with Comparator.naturalOrder()

Сергей Цыпанов duke at openjdk.org
Mon Sep 12 09:40:19 UTC 2022


On Sat, 20 Aug 2022 16:37:29 GMT, liach <duke at openjdk.org> wrote:

>> We can use `Comparator.naturalOrder()` for cases when a `TreeMap` instance is constructed without comparator. This allows to squash two branches in `TreeMap.get()` into one.
>> 
>> P.S. I think the comment of `TreeMap.getEntryUsingComparator()` is outdated. Should we also change it?
>
> src/java.base/share/classes/java/util/TreeMap.java line 3318:
> 
>> 3316:             // Adapt or create a key-based comparator
>> 3317:             Comparator<? super K> treeComparator = tree.comparator;
>> 3318:             return Map.Entry.comparingByKey(treeComparator == null ? natural() : treeComparator);
> 
> You can probably have:
> 
> return treeComparator == nul ?
>         Map.Entry.comparingByKey() :
>         Map.Entry.comparingByKey(treeComparator);
> 
> instead.

Nope, there'd be a compilation error

-------------

PR: https://git.openjdk.org/jdk/pull/9901


More information about the core-libs-dev mailing list