我喜欢 HashSet<>()并在使用默认构造函数初始化时急切地使用它:
I love a HashSet<>() and use this eagerly while initializing this with the default constructor:
Set<Users> users = new HashSet<>();现在,我的自动bean创建器(JBoss工具)将其初始化为:
Now, my automatic bean creator (JBoss tools) initializes this as:
Set<Users> users = new HashSet<>(0);为什么 零 ? API告诉我这是初始容量,但将此值设为零的优势是什么?这是建议吗?
Why the zero? The API tells me that this is the initial capacity, but what is the advantage of putting this to zero? Is this advised?
推荐答案默认初始容量为16 ,因此通过传入0可以节省几个字节的内存,如果你最终没有放任何东西。
The default initial capacity is 16, so by passing in 0 you may save a few bytes of memory if you end up not putting anything in the set.
除此之外没有任何真正的优势;当你传递0时,创建的集合的容量为1,一旦你添加了东西,它就必须调整大小。
Other than that there is no real advantage; when you pass 0 the set is created with a capacity of 1 and as soon as you add something it will have to be resized.
更多推荐
为什么要将HashSet<>(0)初始化为零?
发布评论