HashSet负载因子

发布于 2024-09-15 21:42:59 字数 159 浏览 4 评论 0原文

如果我使用初始容量为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

路弥 2024-09-22 21:42:59

负载因子是衡量 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

趴在窗边数星星i 2024-09-22 21:42:59

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.

极度宠爱 2024-09-22 21:42:59

这是第二种情况。 HashSet和hashMap的loadFactor都是一个相对因子。

It's the second case. The loadFactor of both HashSet and hashMap is a relative factor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文