为什么要将HashSet<>(0)初始化为零?

编程入门 行业动态 更新时间:2024-10-11 13:23:31
本文介绍了为什么要将HashSet<>(0)初始化为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我喜欢 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&lt;&gt;(0)初始化为零?

本文发布于:2023-11-03 21:26:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556124.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:要将   初始化   为零   HashSet   lt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!