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...
Method 1(Using keys array): You can convert all the keys of LinkedHashMap to a set using Keyset method and then convert the set to an array by using toArray method now using array index access the key and get the value from LinkedHashMap.
In the ArrayList chapter, you learned that Arrays store items as an ordered collection, and you have to access them with an index number ( int type). A HashMap however, store items in "key/value" pairs, and you can access them by an index of another type (e.g. a String ).
LinkedHashMap in Java LinkedHashMap maintains the order of insertion. So while iterating over its keys, the elements are returned in the order they were inserted. LinkedHashMap uses a doubly-linked list to maintain the order of insertion. If a key is reinserted, its insertion order is not affected.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With