java 14 HashMap 原始碼學習筆記
阿新 • • 發佈:2020-08-29
1.TREEIFY_THRESHOLD 常量為什麼是 8 ?
參考
https://www.cnblogs.com/linghu-java/p/10598758.html
1.1 為什麼要從連結串列轉成紅黑樹
連結串列查詢效能是O(n),而樹結構能將查詢效能提升到O(log(n))
1.2 為什麼一開始不用紅黑樹
- 當連結串列長度很小的時候,即使遍歷,速度也非常快,但是當連結串列長度不斷變長,肯定會對查詢效能有一定的影響,所以才需要轉成樹。
- TreeNodes佔用空間是普通Nodes的兩倍,所以只有當bin包含足夠多的節點時才會轉成TreeNodes
1.3TREEIFY_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_THRESHOLD,如果都是 8,則可能陷入(樹化<=>樹退化)的死迴圈中. 若是 7,則當極端情況下(頻繁插入和刪除的都是同一個雜湊桶)對一個連結串列長度為 8 的的雜湊桶進行頻繁的刪除和插入,同樣也會導致頻繁的樹化<=>非樹化.
- 更低時,當連結串列長度很小的時候,即使遍歷,速度也非常快。而TreeNodes佔用空間是普通Nodes的兩倍。