目录
java8中的removeTreeNode源码
/**
* Removes the given node, that must be present before this call.
* This is messier than typical red-black deletion code because we
* cannot swap the contents of an interior node with a leaf
* successor that is pinned by "next" pointers that are accessible
* independently during traversal. So instead we swap the tree
* linkages. If the current tree appears to have too few nodes,
* the bin is converted back to a plain bin. (The test triggers
* somewhere between 2 and 6 nodes, depending on tree structure).
*/
final void removeTreeNode(HashMap<K, V> map, Node<K, V>[] tab, boolean movable) {
int n;
if(tab == null || (n = tab.length) == 0) {
return;
}
int index = (n - 1) & hash;
TreeNode<K, V> first = (TreeNode<K, V>) tab[index], root = first, rl;
TreeNode<K, V> succ = (TreeNode<K, V>) next, pred = prev;
// pred==null 表示当前待删除的节点没有前驱节点
if(pred == null) {
// 连续赋值的执行顺序
// 这里只是移动了tab[idx]和first指针的位置
tab[index] = first = succ;
} else {
pred.next = succ;
}
// 修改succ的前驱节点,此时被删除的节点为头结点,头节点从链表中删除,但是旧头结点还有指向当前头结点的指针
// 如果不是头结点,双向修改指针,旧节点对链表的链接仍然存在
if(succ != null) {
succ.prev = pred