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);
}
}
//matchValue的作用是指现在是否需要值匹配。因为可能没有传入value,所以判断一下
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) // 如果要删除的node正好是首个元素
tab[index] = node.next;
else
p.next = node.next; //执行链表元素的删除操作
++modCount;
--size;
afterNodeRemoval(node); //空方法 留给子类拓展 比如LinkedHashMap
return node;
}
}
return null;
}