static int tieBreakOrder(Object a, Object b) {
int d;
if (a == null || b == null ||
(d = a.getClass().getName().
compareTo(b.getClass().getName())) == 0)
d = (System.identityHashCode(a) <= System.identityHashCode(b) ?
-1 : 1);
return d;
}
hd.treeify(tab);
final void treeify(Node<K,V>[] tab) {
TreeNode<K,V> root = null;
for (TreeNode<K,V> x = this, next; x != null; x = next) {
next = (TreeNode<K,V>)x.next;
x.left = x.right = null;
//如果是第一个结点则设置为根结点
if (root == null) {
x.parent = null;
x.red = false;
root = x;
}
//非第一个结点
else {
K k = x.key;
int h = x.hash;
Class<?> kc = null;
for (TreeNode<K,V> p = root;;) {
int dir, ph;
K pk = p.key;
//根据hash值大小判断放在左子结点还是右
if ((ph = p.hash) > h)
dir = -1;
else if (ph < h)
dir = 1;
//hash值一样
else if ((kc == null &&
(kc = comparableClassFor(k)) == null) ||
(dir = compareComparables(kc, k, pk)) == 0)
dir = tieBreakOrder(k, pk);
TreeNode<K,V> xp = p;
if ((p = (dir <= 0) ? p.left : p.right) == null) {
x.parent = xp;
//设置
if (dir <= 0)
xp.left = x;
else
xp.right = x;
root = balanceInsertion(root, x);
break;
}
}
}
}
moveRootToFront(tab, root);
}
TreeNode类似一颗红黑树 看下普通treeMap https://juejin.im/post/5a0658f76fb9a04523415a8d
基本结构:
hd.treeify(tab);
treeifyBin
https://www.cnblogs.com/QH-Jimmy/p/7810644.html