1. //在节点删除后,维护链表,传入删除的节点
    2. void afterNodeRemoval(Node<K,V> e) { // unlink
    3. //p指向待删除元素,b执行前驱,a执行后驱
    4. LinkedHashMap.Entry<K,V> p =
    5. (LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;
    6. //这里执行双向链表删除p节点操作,很不简单。
    7. p.before = p.after = null;
    8. if (b == null)
    9. head = a;
    10. else
    11. b.after = a;
    12. if (a == null)
    13. tail = b;
    14. else
    15. a.before = b;
    16. }
    1. //在节点被访问后根据accessOrder判断是否需要调整链表顺序
    2. void afterNodeAccess(Node<K,V> e) { // move node to last
    3. LinkedHashMap.Entry<K,V> last;
    4. if (accessOrder && (last = tail) != e) {
    5. LinkedHashMap.Entry<K,V> p =
    6. (LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;
    7. p.after = null;
    8. //这里执行双向链表删除操作
    9. if (b == null)
    10. head = a;
    11. else
    12. b.after = a;
    13. if (a != null)
    14. a.before = b;
    15. else
    16. last = b;
    17. //这里执行将p放到尾部
    18. if (last == null)
    19. head = p;
    20. else {
    21. p.before = last;
    22. last.after = p;
    23. }
    24. tail = p;
    25. ++modCount;
    26. }
    27. }
    1. void afterNodeInsertion(boolean evict) { // possibly remove eldest
    2. LinkedHashMap.Entry<K,V> first;
    3. //removeEldestEntry(first)默认返回false,所以afterNodeInsertion这个方法其实并不会执行
    4. if (evict && (first = head) != null && removeEldestEntry(first)) {
    5. K key = first.key;
    6. removeNode(hash(key), key, null, false, true);
    7. }
    8. }
    9. protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
    10. return false;
    11. }

    此方法可用来实现LRU,重写removeEldestEntry方法,