1.8HashMap学习
--- 学自图灵周瑜大大vip课程
大致学了学 - - 梳理了下过程 红黑树操作比较绕 没有特别细研究
//LinkendHashmap才用得到 -> evict 重复覆盖不? => onlyIfAbsent
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
//1.7叫Entry
Node<K,V>[] tab; Node<K,V> p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0)
//数组为空 初始化
n = (tab = resize()).length;
//算下标 为空直接赋值 此时p等于table[i]
if ((p = tab[i = (n - 1) & hash]) == null)
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))))
//key一样p赋值给e
e = p;
//是红黑树类型
else if (p instanceof TreeNode)
//比较 寻找 卡卡卡一顿整进去了
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
//是链表
for (int binCount = 0; ; ++binCount) {
//next属性为空? 插到尾部
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
//是不是大于阈值(8) >=7 如果binCount为7 也就是有8个元素
//但是新节点已经过来了 其实已经有9个了(binCount不会因为插入节点而变化)
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;
}
}
//有重复的覆盖返回旧的
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
final void treeifyBin(Node<K,V>[] tab, int hash) {
int n, index; Node<K,V> e;
//数组为空或者数组长度小于64
//改为红黑树是为了提升插入和查询效率
if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
//扩容->缩短链表
resize();
else if ((e = tab[index = (n - 1) & hash]) != null) {
TreeNode<K,V> hd = null, tl = null;
//遍历每一个节点
do {
//new TreeNode<>(e.hash, e.key, e.value, next) 是Node子类
//属性 left right parent prev boolean red 和 Node的那几个
TreeNode<K,V> p = replacementTreeNode(e, null);
if (tl == null)
hd = p;
else {
//生成双向链表
p.prev = tl;
tl.next = p;
}
tl = p;
} while ((e = e.next) != null);
//hd是链表里的第一个元素
if ((tab[index] = hd) != null)
//真正的树化 遍历判断 有判断能不能compare比较 有看实现接口 。。。
hd.treeify(tab);
}
}
//扩容 初始化都在这
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
//阈值
int oldThr = threshold;
int newCap, newThr = 0;
//老数组大于0 扩容
if (oldCap > 0) {
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold
}
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else { // zero initial threshold signifies using defaults
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
if (newThr == 0) {
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
threshold = newThr;
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
//上面是获取阈值 数组大小啥的 下面是扩容逻辑
table = newTab;
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
//老数组有元素
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
//就一个元素
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e;
else if (e instanceof TreeNode)
//红黑树扩容
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
else { // preserve order
//lo应该是lower 低位 hi是height高位
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
next = e.next;
//哈希值 & 2的幂次方
//要么有一位为1 要么都是0 结果要么是0 要么是2次幂
if ((e.hash & oldCap) == 0) {
//卧槽 抱团? 结果一样的搞一起 hxd一起走
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
else {
if (hiTail == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
if (loTail != null) {
loTail.next = null;
//低位抱团走了
newTab[j] = loHead;
}
if (hiTail != null) {
hiTail.next = null;
//高位抱团走了
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}
static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root,
TreeNode<K,V> x) {
x.red = true;
for (TreeNode<K,V> xp, xpp, xppl, xppr;;) {
//父节点为空 那么当前节点就是头
if ((xp = x.parent) == null) {
x.red = false;
return x;
}
//父节点不是红 是黑色的 或者 x的父节点的父节点为空 不用调整
else if (!xp.red || (xpp = xp.parent) == null)
return root;
//此时x的父节点一定为红色
//x的父节点xp 是x的爷爷节点xpp的左子节点
if (xp == (xppl = xpp.left)) {
//x的爷爷节点的右子节点(叔叔节点)不为空 并且是红色
if ((xppr = xpp.right) != null && xppr.red) {
//叔叔变黑 父变黑 爷爷变红
xppr.red = false;
xp.red = false;
xpp.red = true;
//调整完了 需要把上面的递归调整
x = xpp;
}
//x的爷爷节点的右子节点(叔叔节点)为空 或者为黑色 -> 旋转加变色
else {
//x是父节点的右节点 先左旋 父子交换
if (x == xp.right) {
root = rotateLeft(root, x = xp);
xpp = (xp = x.parent) == null ? null : xp.parent;
}
if (xp != null) {
xp.red = false;
if (xpp != null) {
xpp.red = true;
root = rotateRight(root, xpp);
}
}
}
}
else {
if (xppl != null && xppl.red) {
xppl.red = false;
xp.red = false;
xpp.red = true;
x = xpp;
}
else {
if (x == xp.left) {
root = rotateRight(root, x = xp);
xpp = (xp = x.parent) == null ? null : xp.parent;
}
if (xp != null) {
xp.red = false;
if (xpp != null) {
xpp.red = true;
root = rotateLeft(root, xpp);
}
}
}
}
}
}
//左旋 看吐了。。。
static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root,
TreeNode<K,V> p) {
TreeNode<K,V> r, pp, rl;
if (p != null && (r = p.right) != null) {
if ((rl = p.right = r.left) != null)
rl.parent = p;
if ((pp = r.parent = p.parent) == null)
(root = r).red = false;
else if (pp.left == p)
pp.left = r;
else
pp.right = r;
r.left = p;
p.parent = r;
}
return root;
}
if (root == null || root.right == null ||(rl = root.left) == null || rl.left == null) 红黑树会退化成链表