问题
From Javadoc:Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries.
If it is so, then why doesn't it provide object access like List in java, list.get(index);
UPDATE
I had implemented LRU Cache using LinkedHashMap. My algorithm required me to access LRU Object from the cache. That's why I required random access, but I think that will cost me bad performance, so I have changed the logic and I am accessing the LRU object just when Cache is full...using removeEldestEntry()
Thank you all...
回答1:
a) Because the entries are linked, not randomly accessible. The performance would be miserable, O(N)
if I'm not in error.
b) Because there is no interface to back up this functionality. So the choice would be to either introduce a dedicated interface just for this (badly performing) Implementation or require clients to program against implementation classes instead of interfaces
Btw with Guava there's a simple solution for you:
Iterables.get(map.values(), offset);
And for caching look at Guava's MapMaker and it's expiration features.
回答2:
Since values()
provides a backing collection of the values, you can solve it like this:
map.values().remove(map.values().toArray()[index]);
Perhaps not very efficient (especially memory-wise), but it should be O(N)
just as you would expect it to be.
Btw, I think the question is legitimate for all List
operations. (It shouldn't be slower than LinkedList
anyway, right?)
I set out to do a LinkedHashMapList
which extended the LinkedHashMap
and implemented the List
interface. Surprisingly it seems impossible to do, due to the clash for remove. The existing remove
method returns the previously mapped object, while the List.remove
should return a boolean
.
That's just a reflection, and honestly, I also find it annoying that the LinkedHashMap
can't be treated more like a LinkedList
.
回答3:
Please take a look at Apache Commons LinkedMap
回答4:
It provides an Iterator
interface, each node in the list is linked to the one before it and after it. Having a get(i)
method would be no different than iterating over all the elements in the list since there is no backing array (same as LinkedList
).
If you require this ability which isn't very performant I suggest extending the map yourself
回答5:
If you want random access you can do
Map<K,V> map = new LinkedHashMap<K,V>();
Map.Entry<K,V>[] entries = (Map.Entry<K,V>[]) map.toArray(new Map.Entry[map.size()]);
Map.Entry<K,V> entry_n = entry[n];
As you can see the performance is likely to be very poor unless you cache the entries
array.
I would question the need for it however.
回答6:
There is no real problem to make a Map with log(N) efficiency of access by index. If you use a red-black tree and store for each node the number of elements in the tree starting at that node it is possible to write a get(int index) method that is log(N).
来源:https://stackoverflow.com/questions/5666820/why-doesnt-linkedhashmap-provide-access-by-index