[PATCH] Simplification of TreeMap
jdk at fiolino.de
Tue Sep 4 20:02:38 UTC 2018
I was also wondering why TreeMap doesn't just use a default comparator
and always checks for null instead.
The problem with your patch is that deserialized TreeMap instances which
were serialized from a previous version would still have the comparator
set to null, thus resulting in a NPE after your patch. And you can't
easily fix this because comparator is a final field.
On 04.09.2018 21:14, Сергей Цыпанов wrote:
> 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
More information about the core-libs-dev