java - 关于 HashMap#treeifyBin 的困惑
问题描述
我正在阅读有关HashMap#treeifyBin
函数的源代码。
很困惑为什么要检查
if ((tab[index] = hd) != null)
因为它之前已经检查过了
(e = tab[index = (n - 1) & hash]) != null
代码:
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) // why do we need this line?
hd.treeify(tab);
}
}
解决方案
推荐阅读
- mysql - “#1265 - 数据被截断”错误更改列(集)值
- c# - 在 MVC 中创建一个按钮来隐藏标签并将其替换为 '******'
- python - 需要一个包含唯一经理的列“经理”和包含每个经理下所有销售人员的数组的“list_of_salesmen”
- r - R gWidgets2 不返回值
- mongoose - 当我使用 mongoose .populate.exec 方法时,Model.findById 返回 undefined
- java - ViewModelProvider Fragment 实例化模型
- django - 在 Django 中,从 UpdateView 或 ListView 创建对象的完整副本
- azure - Jira:编辑问题>源表中的流项目字段
- javascript - 是否可以在 Google Apps 脚本中将每个函数都写为“纯函数”?
- javascript - 在 Javascript 中动态创建表单无法在 PHP 上找到 POST 变量