gpt4 book ai didi

java - 为什么 java hashmap 在计数为 8 时对 bin 进行树形化,而在计数为 6 时对 bin 进行树形化?

转载 作者:行者123 更新时间:2023-12-02 09:50:29 26 4
gpt4 key购买 nike

请赐教。我只能想象这种情况可以避免频繁的treeify和untreeify,同时bin的数量为8。

/**
* The bin count threshold for using a tree rather than list for a
* bin. Bins are converted to trees when adding an element to a
* bin with at least this many nodes. The value must be greater
* than 2 and should be at least 8 to mesh with assumptions in
* tree removal about conversion back to plain bins upon
* shrinkage.
*/
static final int TREEIFY_THRESHOLD = 8;

/**
* The bin count threshold for untreeifying a (split) bin during a
* resize operation. Should be less than TREEIFY_THRESHOLD, and at
* most 6 to mesh with shrinkage detection under removal.
*/
static final int UNTREEIFY_THRESHOLD = 6;

其实我是想问为什么这些值不一样。您能否向我展示有关他们使用不同计数的原因的具体统计数据?

最佳答案

Hashmap是哈希表数据结构的实现。当我们插入一个键值对时,使用哈希函数系统会识别该键的哈希值并插入到相应的 bin 中。如果插入另一个键,第二个键的值对和哈希值也提供相同的值,它将进入同一个容器。这种情况称为哈希冲突。如果 bin 的大小很小,那么稍后搜索元素时,bin 的 List/array 实现会提供更好的性能。否则 bin 大小很大,二叉树可能有助于轻松识别 key 。您注意到的是 HashMap 的性能优化。

关于java - 为什么 java hashmap 在计数为 8 时对 bin 进行树形化,而在计数为 6 时对 bin 进行树形化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56352440/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com