5

As I referred to the documentation of LinkedHashMap, It says, a double linked list(DLL) is maintained internally

I was trying to understand why a DLL was chosen over S(ingle)LL The biggest advantage I get with a DLL would be traversing backwards, but I dont see any use case for LinkedHashMap() exploiting this advantage, since there is no previous() sort of operation like next() in the Iterable interface..

Can anyone explain why was a DLL, and not a SLL?

smc
  • 710
  • 1
  • 9
  • 26
  • Might be related to delete operations. –  Oct 27 '12 at 02:44
  • @bdares I believe Optimal traversal gaurentees optimal deletion too. Traversal wise, I believe SLL is as optimal as DLL – smc Oct 27 '12 at 03:41

1 Answers1

8

It's because with an additional hash map, you can implement deletes in O(1). If you are using a singly linked list, delete would take O(n).

Consider a hash map for storing a key value pair, and another internal hash map with keys pointing to nodes in the linked list. When deleting, if it's a doubly linked list, I can easily get to the previous element and make it point to the following element. This is not possible with a singly linked list.

http://www.quora.com/Java-programming-language/Why-is-a-Java-LinkedHashMap-or-LinkedHashSet-backed-by-a-doubly-linked-list

Neo M Hacker
  • 899
  • 7
  • 11