You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 第一种情况,插入节点是头节点// a b c d e// get(a),将a放到末尾// b c d e a// 第二种情况,插入节点不是头节点// a b c d e// get(b),将b放到末尾// a c d e b// 将元素移动到最后一个voidafterNodeAccess(Node<K,V> e) { // move node to lastLinkedHashMap.Entry<K,V> last;
// 当需要排序并且加入节点e不是尾节点时,进入逻辑(为尾节点时,无需操作,已经为最近访问过的元素)if (accessOrder && (last = tail) != e) {
// 首先暂存插入节点的当前节点,上一个节点,下一个节点LinkedHashMap.Entry<K,V> p =
(LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;
// 将插入节点的后置节点设为null,代表尾节点p.after = null;
// 如果p的前置节点为null,代表插入节点为头节点,将新的头节点设置为插入节点的下一个节点if (b == null)
head = a;
else// 不为空时,将插入节点的前一个节点的下一个节点设置为插入节点的后一个节点b.after = a;
// 插入节点的后一个节点不为null时,插入节点的后一个节点的前一个节点设置为插入节点的前一个节点if (a != null)
a.before = b;
else// 这一块不知道什么时候会进入??last = b;
// 尾节点为null,代表链表为空,头节点直接设置为插入节点if (last == null)
head = p;
else {
// 头节点的前置节点为尾节点p.before = last;
// 尾节点的后一个节点为插入节点last.after = p;
}
// 新的尾节点设置为插入节点tail = p;
// 增加修改数量
++modCount;
}
}