百度了一下,感覺能說清楚的並不多,所以在此記錄一下。
首先說一說轉換為紅黑樹的必要性:
紅黑樹的插入、刪除和遍歷的最壞時間復雜度都是log(n),
因此,意外的情況或者惡意使用下導致hashCode()方法的返回值很差時,
性能的下降將會是"優雅"的,只要Key具有可比性。
但由於TreeNodes的大小是常規Nodes的兩倍,所以只有桶中包含足夠多
的元素以供使用時,我們才會使用樹。那為什么這個數字是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
簡單解釋一下,理想情況下,在隨機哈希代碼下,桶中的節點頻率遵循
泊松分布,文中給出了桶長度k的頻率表。
由頻率表可以看出,桶的長度超過8的概率非常非常小。所以作者應該是根據
概率統計而選擇了8作為閥值。
---------------------
作者:zycc_dai
來源:CSDN
原文:https://blog.csdn.net/daiyuhe/article/details/89424736
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!