1.插入新节点时,会将该节点加到链表尾部
 
public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>{
    /**
     * The head (eldest) of the doubly linked list.
     */
    transient LinkedHashMapEntry<K,V> head;
    /**
     * The tail (youngest) of the doubly linked list.
     */
    transient LinkedHashMapEntry<K,V> tail;
    Node<K,V> newNode(int hash, K key, V value, Node<K,V> e) {
        LinkedHashMapEntry<K,V> p = new LinkedHashMapEntry<K,V>(hash, key, value, e);
        linkNodeLast(p);
        return p;
    }
    // link at the end of list
    private void linkNodeLast(LinkedHashMapEntry<K,V> p) {
        LinkedHashMapEntry<K,V> last = tail;
        tail = p; //更新p为尾节点
        if (last == null){ //如果之前尾节点为null,说明链表为null,那更新头节点为p
            head = p;
        } else {
            p.before = last;
            last.after = p;
        }
    }
}2. 移除一个元素后,会将该元素从链表移除
 
public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V> {
    void afterNodeRemoval(Node<K,V> e) { // unlink
        LinkedHashMapEntry<K,V> p = (LinkedHashMapEntry<K,V>)e, b = p.before, a = p.after;
        p.before = p.after = null;
        if (b == null){//之前p为head
            head = a;//更新p的下一个元素为head
        } else{
            b.after = a;
        }
        if (a == null){//之前p为tail
            tail = b;
        } else{
            a.before = b;
        }
    }
}


















