[PATCH] Simplification of TreeMap

Сергей Цыпанов sergei.tsypanov at yandex.ru
Tue Sep 4 19:14:21 UTC 2018


currently (latest code of JDK 11) an instance of TreeMap created with no-arg contructor has nullable comparator i.e. utilizes no comparator.

As it comes from the code, a TreeMap created with nullable comparator works exactly as a TreeMap created with comparator provided by Comparator.naturalOrder(). This is also explicitly specifid in Javadoc.

I propose to initialize default comparator of TreeMap with instance of Comparator returned by Comparator.naturalOrder() instead of null.
This allows to remove the code responsible for handling nullable comparator, e. g. TreeMap::getEntryUsingComparator can be completely removed in favour of TreeMap::getEntry.
Similar simplification available for TreeMap::put, TreeMap::compare, EntrySpliterator::getComparator.

I've prepared a patch for this.
The patch contains both described major change and some tiny clean-ups e. g. utilization of Objects::requireNonNull where appropriate and Objects::equals instead of hand-written TreeMap::valEquals.

TreeMapTest is green after my changes.

Sergey Tsypanov

-------------- next part --------------
A non-text attachment was scrubbed...
Name: treemap.patch
Type: text/x-diff
Size: 13581 bytes
Desc: not available
URL: <http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20180904/108ae4e9/treemap-0001.patch>

More information about the core-libs-dev mailing list