put()
方法
- 通过插入元素的
hash
值找到插入元素对应的桶的索引。 - 桶为
NULL
直接插入,否则处理红黑树的插入TreeNode.putTreeVal
或者链表的插入,链表的插入逻辑比较简单,遍历链表的元素,如果链表元素与插入元素相等则取消插入,否则插入到链表的尾部,同时如果插入后的链表长度大于TREEIFY_THRESHOLD
则将链表转换成红黑树
。 - 处理是否覆盖旧值。
- 如果容量大于扩容阈值则进行扩容
resize()
。
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
// table[]数组初始化操作
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
// 如果通过hash值取模得到的桶为空,则直接把新生成的节点放入该桶
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
// 判断桶的第一个元素的key值是否相同(hash值相同,且能equals),如果相同,则返回当前元素(函数末尾进行统一处理)
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)
// 插入到红黑树中
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
// 插入链表尾部
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // 如果链表插入后的长度大于等于TREEIFY_THRESHOLD,则转换成红黑树。
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;
}
红黑树的插入putTreeVal()
- 从红黑树的根节点开始遍历,比较插入元素与当前节点的
hash
,如果相等则查找当前节点或红黑树的子节点看是否存在旧值,如果存在则返回旧值让上层处理,否则根据hash
值或者是否继承Comparable
等判断插入元素在父节点的左节点或右节点。 - 插入元素后,通过变色、左旋、右旋操作调整红黑树的自平衡,使得二叉树再次满足红黑树的规则。
- 确保table[]数组对应下标的首节点指向红黑树的首节点。
final TreeNode<K,V> putTreeVal(HashMap<K,V> map, Node<K,V>[] tab, int h, K k, V v) {
Class<?> kc = null;
boolean searched = false;
TreeNode<K,V> root = (parent != null) ? root() : this;
for (TreeNode<K,V> p = root;;) {
int dir, ph; K pk;
if ((ph = p.hash) > h)
dir = -1;
else if (ph < h)
dir = 1;
else if ((pk = p.key) == k || (k != null && k.equals(pk)))
return p;
else if ((kc == null &&
(kc = comparableClassFor(k)) == null) ||
(dir = compareComparables(kc, k, pk)) == 0) {
if (!searched) {
TreeNode<K,V> q, ch;
searched = true;
if (((ch = p.left) != null &&
(q = ch.find(h, k, kc)) != null) ||
((ch = p.right) != null &&
(q = ch.find(h, k, kc)) != null))
return q;
}
dir = tieBreakOrder(k, pk);
}
TreeNode<K,V> xp = p;
if ((p = (dir <= 0) ? p.left : p.right) == null) {
Node<K,V> xpn = xp.next;
TreeNode<K,V> x = map.newTreeNode(h, k, v, xpn);
if (dir <= 0)
xp.left = x;
else
xp.right = x;
xp.next = x;
x.parent = x.prev = xp;
if (xpn != null)
((TreeNode<K,V>)xpn).prev = x;
moveRootToFront(tab, balanceInsertion(root, x));
return null;
}
}
}
treeifyBin()
方法
链表转换红黑树条件:容量 >= MIN_TREEIFY_CAPACITY 且 链表长度 >= TREEIFY_THRESHOLD。
treeifyBin()
方法处理单向链表转换红黑树的逻辑,先将单向链表转换成双向链表,再将双向链表转换成红黑树。
final void treeifyBin(Node<K,V>[] tab, int hash) {
int n, index; Node<K,V> e;
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 {
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);
if ((tab[index] = hd) != null)
hd.treeify(tab);
}
}
链表转换红黑树treeify()
方法
- 遍历所有元素,将遍历的第一个元素作为红黑树的根节点,其余的元素平衡插入到红黑树中。
- 确保table[]数组对应下标的首节点指向红黑树的首节点。
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 {
// 将当前节点x插入到二叉树中
K k = x.key;
int h = x.hash;
Class<?> kc = null;
// 从根节点开始遍历
for (TreeNode<K,V> p = root;;) {
int dir, ph;
K pk = p.key;
if ((ph = p.hash) > h) // 如果当前节点hash 大于 指定key的hash值
dir = -1; // 要添加的元素应该放置在当前节点的左侧
else if (ph < h) // 如果当前节点hash 小于 指定key的hash值
dir = 1; // 要添加的元素应该放置在当前节点的右侧
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;
}
}
}
}
// 移动平衡后的红黑树的根节点到table[]数组对应下标的首元素中
moveRootToFront(tab, root);
}