<细节向>jdk1.8中HashMap底层链表转红黑树的阈值为什么是8?红黑树转链表为什么是6?

本文探讨了JDK1.8中HashMap中链表转红黑树阈值为8的原因,以及红黑树转链表阈值为6的考虑。这与hashcode碰撞次数的泊松分布有关,旨在平衡时间和空间效率。实验数据显示,8次碰撞的概率极低,超过此阈值意味着高概率的hash冲突,适合转为红黑树。而阈值为6能避免频繁的转换,防止资源浪费。
摘要由CSDN通过智能技术生成

先看源码

/*
     * Implementation notes.
     *
     * This map usually acts as a binned (bucketed) hash table, but
     * when bins get too large, they are transformed into bins of
     * TreeNodes, each structured similarly to those in
     * java.util.TreeMap. Most methods try to use normal bins, but
     * relay to TreeNode methods when applicable (simply by checking
     * instanceof a node).  Bins of TreeNodes may be traversed and
     * used like any others, but additionally support faster lookup
     * when overpopulated. However, since the vast majority of bins in
     * normal use are not overpopulated, checking for existence of
     * tree bins may be delayed in the course of table methods.
     *
     * Tree bins (i.e., bins whose elements are all TreeNodes) are
     * ordered primarily by hashCode, but in the case of ties, if two
     * elements are of the same "class C implements C
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值