private final void treeifyBin(Node<K,V>[] tab, int index) {
Node<K,V> b; int n, sc;
if (tab != null) {
if ((n = tab.length) < MIN_TREEIFY_CAPACITY)
tryPresize(n << 1);
else if ((b = tabAt(tab, index)) != null && b.hash >= 0) {
synchronized (b) {
if (tabAt(tab, index) == b) {
TreeNode<K,V> hd = null, tl = null;
for (Node<K,V> e = b; e != null; e = e.next) {
TreeNode<K,V> p =
new TreeNode<K,V>(e.hash, e.key, e.val,
null, null);
if ((p.prev = tl) == null)
hd = p;
else
tl.next = p;
tl = p;
}
setTabAt(tab, index, new TreeBin<K,V>(hd));
}
}
}
}
}
其实就像是缝衣服,hd是线头,tl是线尾,首先,线头和线尾都插上第一个节点p,第二遍是p1的prev指向p,线尾的next指向新的p,也就是p的next指向第二个节点p1。循环往复,先组织成新的链表,然后再把此链表树化。
示例图如下

来源:https://www.cnblogs.com/javaddd/p/12293609.html