Comments (2)
我们知道红黑树(Red Black Tree) 是一种自平衡二叉查找树,红黑树和AVL树类似,都是在进行插入和删除操作时通过特定操作保持二叉查找树的平衡,从而获得较高的查找性能。
为什么是8呢?我暂时只能想到/查阅到两个答案
- 这个值是HashMap执行查询的时候时间复杂度最低的阈值,也就是当桶(bucket)上的结点数大于8时才会有转成红黑树才有必要;
- 理想情况下使用随机的哈希码,容器中节点分布在hash桶中的频率遵循泊松分布(具体可以查看http://en.wikipedia.org/wiki/Poisson_distribution),* 按照泊松分布的计算公式计算出了桶中元素个数和概率的对照表,可以看到链表中元素个数为8时的概率已经非常小,再多的就更少了,所以原作者在选择链表元素个数时选择了8,是根据概率统计而选择的。(https://blog.csdn.net/Mollychin/article/details/80444967
)
from javaguide.
@winyiwin
参见 putVal
方法
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
当桶(bucket)上的结点数大于这个值时会转成红黑树
resize()
中的 ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
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);
}
}
if (hiHead != null) {
if (hc <= UNTREEIFY_THRESHOLD)
tab[index + bit] = hiHead.untreeify(map);
else {
tab[index + bit] = hiHead;
if (loHead != null)
hiHead.treeify(tab);
}
}
当桶(bucket)上的结点数小于这个值时树转链表
注意阅读源码。
from javaguide.
Related Issues (20)
- 缓存穿透中的布隆过滤器链接失效了 HOT 1
- 在外部调用静态方法时,可以使用 类名.方法名 的方式,也可以使用 对象.方法名 的方式",这里是不是有问题呀 HOT 2
- 排序算法部分,快速排序的最坏时间复杂度是O(N^2)吧,原文中图片和代码部分下面写的都是O(N * log(N)) HOT 1
- 「Java内存区域详解(重点)」 文档的「方法区」部分描述好像有误 HOT 1
- LinkedHashMap实现LRU部分勘误
- 关于聚簇索引和非聚簇索引的定义 HOT 1
- 关于Mysql三大日志详解--undo-log描述不够具体 HOT 2
- Java集合概览部分多写了一个map HOT 1
- Firefox移动端页面返回到顶部按钮不会跟随页面阅读多少变化 HOT 1
- 死信队列这里的介绍语句 HOT 2
- 索引下推里例子的月份写错了 HOT 2
- Mongodb使用b+树,为什么放一篇b树的论证链接? HOT 1
- Java NIO 核心知识总结文章-个别图片背景是透明的,导致深色模式看不清字 HOT 1
- 构造方法用synchronized关键词修饰解释有误 HOT 1
- 关于线程等待和阻塞两种状态 HOT 1
- 双亲委派模型举例不严谨 HOT 1
- SQL语句中having的执行顺序是在select之前吧 HOT 1
- jdk1.7起,静态变量是位于堆区,而不是方法区
- 内容不严谨,复杂度错误 HOT 1
- SPI机制的设计思路和使用场景没有讲到核心
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from javaguide.