HashSet负载因子
如果我使用初始容量为 10、负载因子为 0.5 的 HashSet
那么每添加 5 个元素,HashSet
就会增加,或者首先增加 HashSet
在 20 atc 时增加 10 个元素,然后增加 15 个元素。容量会增加吗?
If I use a HashSet
with a initial capacity of 10 and a load factor of 0.5
then every 5 elements added the HashSet
will be increased or first the HashSet
is increased of 10 elements and after at 15 at 20 atc. the capacity will be increased?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
负载因子是衡量 HashSet 在其容量自动增加之前允许达到多满的程度的指标。当哈希表中的条目数超过负载因子与当前容量的乘积时,哈希表将被重新哈希(即重建内部数据结构),使得哈希表的桶数大约为两倍。
来源
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.
source
HashMap 的默认初始容量为 16,负载因子为 0.75f(即当前映射大小的 75%)。负载因子表示 HashMap 容量应该在什么级别增加一倍。
例如容量和负载因子的乘积为 16 * 0.75 = 12。这表示将第 12 个键值对存入 HashMap 后,其容量变为 32。
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.
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.
这是第二种情况。 HashSet和hashMap的loadFactor都是一个相对因子。
It's the second case. The loadFactor of both HashSet and hashMap is a relative factor.