[concurrency-interest] About LinkedHashMap

Rémi Forax forax at univ-mlv.fr
Sat Oct 15 13:08:54 EDT 2005

Hu, Jinsong a écrit :
> I know this is an off-topic question to post here, but I did struggling
> to find the right way to use this class, and I know there are lot of
> experts in this mail list. I would appreciate it very much on this if
> you could throw some lights. 
> Suppose I have a DataBlock which contains a list of DataElement, and I
> have a HashMap which basically acts a hash index to quickly identify the
> DataElement according to a given key. Now, if I get the element and I
> want to find the next element very quickly, but it seems to me that
> current Collection API does not provide a quick solution to this, it
> appears that I have to link the DataElement by myself to achieve this.
> LinkedHashMap seems a promising solution to this problem, but it does
> not provide a public method to get the entry, it only guarantees the
> order of insertion or access.
> Any thought? 

Mustang introduces a new interface, NavigableMap, that permits
to find the previous or the next entry using
lowerEntry(K key) and higherEntry(K key).

But LinkedHashMap doesn't implements NavigableMap :(
I have personnaly warn doug lea about this.
He said that the JSR166 commitee decide to make
NavigableMap a subclass of SortedMap so
because LinkedHashMap doesn't implements SortedMap, it couldn't
be retrofited to implements NavigableMap without breaking
programs already written.

Workaround : use a TreeMap instead of LinkedHashMap
even if it's the method get() have a log(n) complexity.

> Thanks
> Jinsong

Rémi Forax

More information about the Concurrency-interest mailing list