java HashSet 负载因子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3564638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HashSet load factor
提问by xdevel2000
If I use a HashSetwith a initial capacity of 10 and a load factor of 0.5
then every 5 elements added the HashSetwill be increased or first the HashSetis increased of 10 elements and after at 15 at 20 atc. the capacity will be increased?
如果我使用HashSet初始容量为 10 且负载系数为 0.5 的 a,则每添加 5 个元素,HashSet就会增加或首先HashSet增加 10 个元素,然后在 20 atc 时增加 15 个元素。容量会增加吗?
回答by Sheo
The load factor is a measure of how full the HashSet is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.
负载因子是衡量 HashSet 在其容量自动增加之前允许达到多满的指标。当哈希表中的条目数超过负载因子和当前容量的乘积时,重新哈希表(即重建内部数据结构),使哈希表具有大约两倍的桶数。
回答by Vivek Goel
Default initial capacity of the HashMap takes is 16 and load factor is 0.75f (i.e 75% of current map size). The load factor represents at what level the HashMap capacity should be doubled.
HashMap 的默认初始容量为 16,加载因子为 0.75f(即当前地图大小的 75%)。负载因子表示 HashMap 容量应该在什么级别增加一倍。
For example product of capacity and load factor as 16 * 0.75 = 12. This represents that after storing the 12th key – value pair into the HashMap , its capacity becomes 32.
例如容量和负载因子的乘积为 16 * 0.75 = 12。这表示将第 12 个键值对存储到 HashMap 后,其容量变为 32。
回答by Riduidel
It's the second case. The loadFactor of both HashSet and hashMap is a relative factor.
这是第二种情况。HashSet 和 hashMap 的 loadFactor 都是一个相对因素。

