LinkedHashMap is supposed to contain an insertion-order linked list. Is there any way to access that list? I can't find anything in the javadocs for it.
2 Answers
No, those are implementation details and should never be exposed to the external world.
2 Comments
WinterDev
So theirs no way to get the Previous() and Next() of a key in something better than O(n) using the iterator?
Grzegorz Piwowarek
@WinterDev I did not say this. Your question was about accessing guts of some another structure. Also, are you sure that previous and next are not O(1) actually? You sure you need a LinkedHashMap?
No it isn't, but depending on what you're trying to do, the iterator returned by iterator() may be sufficient.
7 Comments
WinterDev
That sucks, I was hoping to get access to .previous() and .next() in O(1) instead of looping through the entries in O(n) to get the previous and next.
Peter Lawrey
@WinterDev next() is O(1)
WinterDev
@PeterLawrey If I have a key K and want to get the equivalent of K.next(), I have to loop through the iterator until I find K and then get the next one, which is O(n)
Peter Lawrey
@WinterDev in that case,t his is not an intended use case for this Map. I suggest using a NavigableMap which has next and previous.
Louis Wasserman
@WinterDev it's O(log n) like basically all other operations.
|
keySet()do it?