Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't LinkedHashMap provide access by index?

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...

like image 749
Eternal Noob Avatar asked Apr 14 '11 16:04

Eternal Noob


People also ask

How do I get the index value in LinkedHashMap?

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.

Can you access a HashMap by index?

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 ).

How does LinkedHashMap maintain access order?

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.


2 Answers

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.

like image 86
Sean Patrick Floyd Avatar answered Nov 04 '22 04:11

Sean Patrick Floyd


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.

like image 38
aioobe Avatar answered Nov 04 '22 06:11

aioobe