gpt4 book ai didi

java - 为什么hashmap split方法需要在loHead.treeify(tab)之前判断if(hiHead!=Null)

转载 作者:行者123 更新时间:2023-12-01 19:02:56 35 4
gpt4 key购买 nike

在阅读hashmap split方法源码时,发现了一段源码:

    final void split(HashMap<K,V> map, Node<K,V>[] tab, int index, int bit) {
TreeNode<K,V> b = this;
// Relink into lo and hi lists, preserving order
TreeNode<K,V> loHead = null, loTail = null;
TreeNode<K,V> hiHead = null, hiTail = null;
....
if (loHead != null) {
if (lc <= UNTREEIFY_THRESHOLD)
tab[index] = loHead.untreeify(map);
else {
tab[index] = loHead;
if (hiHead != null) // (else is already treeified)
loHead.treeify(tab);
}
}
.......
}

我不明白为什么使用 loHead.treeify (tab);之前需要if(hiHead!=Null)判断。首先,我的理解是hashmap是在单线程中使用的,所以我想不出hiHead和loHead之间有什么关系。官方评论是“else已经树化了。”

最佳答案

该方法的输入是要拆分的树箱。如果该箱中的所有条目落入一个或另一个分割集(即它们对于新添加的散列位都具有相同的值),则输入树已经是正确的。它可以按原样重用,只需将其根设置为新的 bin 值,而另一个为空。

关于java - 为什么hashmap split方法需要在loHead.treeify(tab)之前判断if(hiHead!=Null),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59606106/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com