编程语言
首页 > 编程语言> > java 14 HashMap 源码学习笔记

java 14 HashMap 源码学习笔记

作者:互联网

1. TREEIFY_THRESHOLD 常量为什么是 8 ?

参考

https://www.cnblogs.com/linghu-java/p/10598758.html

https://www.v2ex.com/t/651978

1.1 为什么要从链表转成红黑树

链表查找性能是O(n),而树结构能将查找性能提升到O(log(n))

 

1.2 为什么一开始不用红黑树

 

1.3 TREEIFY_THRESHOLD 常量为什么是 8

 * Because TreeNodes are about twice the size of regular nodes, we
 * use them only when bins contain enough nodes to warrant use
 * (see TREEIFY_THRESHOLD). And when they become too small (due to
 * removal or resizing) they are converted back to plain bins.  In
 * usages with well-distributed user hashCodes, tree bins are
 * rarely used.  Ideally, under random hashCodes, the frequency of
 * nodes in bins follows a Poisson distribution
 * ( http://en.wikipedia.org/wiki/Poisson_distribution) with a
 * parameter of about 0.5 on average for the default resizing
 * threshold of 0.75, although with a large variance because of
 * resizing granularity. Ignoring variance, the expected
 * occurrences of list size k are (exp(-0.5) * pow(0.5, k) /
 * factorial(k)). The first values are:
 *
 * 0:    0.60653066
 * 1:    0.30326533
 * 2:    0.07581633
 * 3:    0.01263606
 * 4:    0.00157952
 * 5:    0.00015795
 * 6:    0.00001316
 * 7:    0.00000094
 * 8:    0.00000006
 * more: less than 1 in ten million

理想情况下随机hashCode算法下所有bin中节点的分布会遵循泊松分布。根据泊松分布概率质量函数,一个哈希桶达到 9 个元素的概率小于一千万分之一



1.4 UNTREEIFY_THRESHOLD 为什么是6

 

标签:TREEIFY,java,14,链表,源码,bins,THRESHOLD,nodes,TreeNodes
来源: https://www.cnblogs.com/GY8023/p/13581718.html