Proposal for improving performance of TreeMap and others

Thomas Hawtin Thomas.Hawtin at Sun.COM
Mon Jan 7 19:59:53 UTC 2008

cowwoc wrote:
> I noticed that TreeMap (and maybe other classes) require a user to either
> pass in a Comparator or ensure that all keys must implement Comparable. The
> TreeMap code then uses a utility method whenever it needs to compare two
> keys:

I'm not going to comment about performance, but there is a problem with 

TreeMap.comparator is final (and non-transient).

TreeMaps serialised with earlier versions will be deserialised with null 
comparator. So, comparator would either need to be made non-final or 
sun.misc.Unsafe used.

For the serialisation case, it would be necessary to change writeObject 
to use putFields rather than defaultWriteObject (not very nice, but not 
half as nasty as I originally thought).

Tom Hawtin

More information about the core-libs-dev mailing list