static class Entry<K,V> extends HashMap.Node<K,V> {
Entry<K,V> before, after;
Entry(int hash, K key, V value, Node<K,V> next) {
super(hash, key, value, next);
}
}

//accessOrder默认为false，即按照插入顺序来连接，true则为按照访问顺序来连接
accessOrder = false;
}
super(initialCapacity);
accessOrder = false;
}
super();
accessOrder = false;
}
public LinkedHashMap(Map<? extends K, ? extends V> m) {
super();
accessOrder = false;
putMapEntries(m, false);
}
boolean accessOrder) {
this.accessOrder = accessOrder;
}

 if ((p = tab[i = (n - 1) & hash]) == null)
//调用newNode()方法
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
//同上
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}

Node<K,V> newNode(int hash, K key, V value, Node<K,V> e) {
//将节点插入链尾
return p;
}
private void linkNodeLast(LinkedHashMap.Entry<K,V> p) {
tail = p;
// 如果链尾为空，则双向链表为空，则p即为头结点也为尾节点
if (last == null)
else {
//否则的话修改指针，让之前链尾的after指针指向p，p的before指向之前链尾
p.before = last;
last.after = p;
}
}

if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
//在节点被访问后移动链尾
afterNodeAccess(e);
return oldValue;
}
}

void afterNodeAccess(Node<K,V> e) { // move node to last
if (accessOrder && (last = tail) != e) {
(LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;
// 因为要移动到链尾，所以先至尾指针为空
p.after = null;
//如果前面没有元素，则p之前为头结点，直接让a成为头结点
if (b == null)
else
// 否则b的尾指针指向a
b.after = a;
if (a != null)
//如果a不为空，则a的头指针指向b
a.before = b;
else
//否则 p之前就为尾指针，则另b成为尾指针
last = b;
if (last == null)
//如果双向链表中只有p一个节点，则令p即为头结点，也为尾节点
else {
//否则 将p插入链尾
p.before = last;
last.after = p;
}
tail = p;
++modCount;
}
}

 /**
*      p将引用移除
*              b          |          p            |          a
*       -------------     |     -------------     |     -------------
*      |before| after| <==|==> |before| after| <==|==> |before| after|
*       -------------     |     -------------     |     -------------
*
*     1.b为NULL时,则a变为头结点
*                             a                          p
*       (b)             -------------                -------------
*      NULL    <------ |before| after|  ......      |before| after| (p最后将插入链尾)
*                       -------------                -------------
*     2.a为NULL时，则b变为链尾节点
*
*             tail
*              b                                           p
*       -------------             (a)                -------------
*      |before| after|  ------->  NULL   ......     |before| after| (p最后将插入链尾)
*       -------------                                -------------
*     3.a,b都为NULL时，p即为头结点，又为尾节点
*
*     因为p前后都没有元素，则双向链表中只有p一个节点
*
*/

public V get(Object key) {
Node<K,V> e;
if ((e = getNode(hash(key), key)) == null)
return null;
// accessOder为true时，被访问的节点被置于双向链表尾部
if (accessOrder)
afterNodeAccess(e);
return e.value;
}

void afterNodeInsertion(boolean evict) { // possibly remove eldest
if (evict && (first = head) != null && removeEldestEntry(first)) {
K key = first.key;
removeNode(hash(key), key, null, false, true);
}
}

  public V remove(Object key) {
Node<K,V> e;
return (e = removeNode(hash(key), key, null, false, true)) == null ?
null : e.value;
}
final Node<K,V> removeNode(int hash, Object key, Object value,
boolean matchValue, boolean movable) {
Node<K,V>[] tab; Node<K,V> p; int n, index;
if ((tab = table) != null && (n = tab.length) > 0 &&
(p = tab[index = (n - 1) & hash]) != null) {
Node<K,V> node = null, e; K k; V v;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
node = p;
else if ((e = p.next) != null) {
if (p instanceof TreeNode)
node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
else {
do {
if (e.hash == hash &&
((k = e.key) == key ||
(key != null && key.equals(k)))) {
node = e;
break;
}
p = e;
} while ((e = e.next) != null);
}
}
if (node != null && (!matchValue || (v = node.value) == value ||
(value != null && value.equals(v)))) {
if (node instanceof TreeNode)
((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
else if (node == p)
tab[index] = node.next;
else
p.next = node.next;
++modCount;
--size;
//回调从双向链表中移除node
afterNodeRemoval(node);
return node;
}
}
return null;
}

LinkedHashIterator() {
expectedModCount = modCount;
current = null;
}

public final boolean hasNext() {
return next != null;
}

}