Using Dual Linked List in LinkedHashMap over Single LinkedList

I know that my question is very similar to the one that was published earlier. I went through a few posts for this, but still not very clear with the answer. That is why I am sending it again.

Why does the associated HashMap use the LinkedList twice over the Single LinkedList, and the order can also be maintained through the Single LinkedList.

In replies to some of the previous posts, it was mentioned that LinkedHashMap provides O (1) complexity for deletion, since it has a previous as well as next element pointer, but I think HashMap also provides O (1) for deletion.

Thank,

+4
source share
1 answer

, HashMap O (1)

, HashMap . , ( Java-. Java 8 ), O(1).

LinkedHashMap, , , , , . , , , , , .

, LinkedHashMap :

void afterNodeRemoval(Node<K,V> e) { // unlink
    LinkedHashMap.Entry<K,V> p =
        (LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;
    p.before = p.after = null;
    if (b == null)
        head = a;
    else
        b.after = a;
    if (a == null)
        tail = b;
    else
        a.before = b;
}

.

+5

All Articles