HashSet< Integer>的初始容量 [英] Initial capacity for a HashSet<Integer>

查看:460
本文介绍了HashSet< Integer>的初始容量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该使用什么初始容量的HashSet,我知道我要插入1000个整数,以防止需要任何内部重建?



我虽然应该使用1000,但是阅读描述initialCapacity参数的构造函数,它说构造一个新的空集合;支持HashMap实例具有指定的初始容量和默认负载因子(0.75)。



所以如果我将容量设置为1000,则hashMap将在达到750个元素时调整大小?



此外,我假设hashMap的有效性需要一些空格,以便解决IC * 0.75 = 1000以获得像1334之类的东西也不是最好的解决方案吗?



更新:

1)我意识到内部的大小不是一个重要的,但仍然是一个学习和更好地了解我正在使用的环境的机会。并且努力应该是最小的。



2)关于选择数据结构的几点意见。请查看我以前的问题:数据结构推荐,其中提供了有关我的场景的更准确的信息

解决方案

您需要一个 size / load-factor 才能避免调整大小注意:它将永远是HashSet& HashMap。


What Initial Capacity should I use for a HashSet into which I know that I am going to insert 1000 integers to prevent the need for any internal rebuilds ?

At first I though that I should use 1000 but reading the description of the constructor that taks the initialCapacity parameter it says Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75)..

So If I set capacity to 1000 the hashMap will resize when reaching 750 elements?

Also I assume that some "space" is required for the effectiveness of the hashMap so solving IC*0.75=1000 to get something like 1334 might also not be the best solution or is it?

UPDATE:
1) I am aware that the implication of internal re-size is not a significant one but still its a chance to learn and better understand the environment which I am using. and the effort should be minimal.

2) Several comments where made regarding the choice of data structure. Please have a look at my previous Q here: Data structure recommendation where more exact information is provided about my scenario.

解决方案

You need a size/load-factor to avoid a resize. Note: it will always be the next power of 2 for HashSet & HashMap.

这篇关于HashSet< Integer>的初始容量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆