2

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.

3
  • 1
    Does keySet() do it? Commented Nov 16, 2016 at 21:13
  • 1
    What are you trying to do? Commented Nov 16, 2016 at 21:17
  • @shmosel I have a key-value type structure that requires next(), previous() in something hopefully better than O(n). But I'm hoping to keep the efficiency of the hashmap for the other operations. Commented Nov 16, 2016 at 21:29

2 Answers 2

2

No, those are implementation details and should never be exposed to the external world.

Sign up to request clarification or add additional context in comments.

2 Comments

So theirs no way to get the Previous() and Next() of a key in something better than O(n) using the iterator?
@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?
1

No it isn't, but depending on what you're trying to do, the iterator returned by iterator() may be sufficient.

7 Comments

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.
@WinterDev next() is O(1)
@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)
@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.
@WinterDev it's O(log n) like basically all other operations.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.