简体   繁体   English

这两组陈述之间的确切区别是什么?

[英]What is the exact difference between these two groups of statements?

Set<Type> union = new HashSet<Type>(s1);

And

Set<Type> union = new HashSet<Type>();
Set<Type> s1 = new HashSet<Type>();
union.addAll(s1);

The first will be more efficient because the second Set will be created with the correct amount of space in the backing data structure where in the second piece of code, the Set will have to resize to make room for the new elements. 一个将更有效,因为第二个Set将在后备数据结构中使用正确的空间量创建,在第二段代码中, Set必须调整大小以便为新元素腾出空间。

As far as end results go, they are the same. 就最终结果而言,它们是相同的。

Assuming that Set s1 contains the same contents in the first and second examples, the end results should come out to be the same. 假设Set s1在第一个和第二个例子中包含相同的内容,最终结果应该是相同的。

(However, the second example will not compile because a Set is an interface rather than a concrete class.) (但是,第二个示例不会编译,因为Set是一个接口而不是具体的类。)

One advantage of using the HashSet(Collection) constructor is that it will have an initial capacity that is enough to hold the Collection (in this case, the Set s1 ) that is passed into the constructor: 使用HashSet(Collection)构造函数的一个优点是它的初始容量足以容纳传递给构造函数的Collection (在本例中为Set s1 ):

Constructs a new set containing the elements in the specified collection. 构造一个包含指定集合中元素的新集合。 The HashMap is created with default load factor (0.75) and an initial capacity sufficient to contain the elements in the specified collection. 使用默认加载因子(0.75)创建HashMap ,初始容量足以包含指定集合中的元素。

However, using the HashSet() constructor, the initial size is 16, so if the Set that was added via the Collection.addAll is larger than 16, there would have to be a resizing of the data structure: 但是,使用HashSet()构造函数时,初始大小为16,因此如果通过Collection.addAll添加的Set大于16,则必须调整数据结构的大小:

Constructs a new, empty set; 构造一个新的空集; the backing HashMap instance has default initial capacity (16) and load factor (0.75). 后备HashMap实例具有默认初始容量(16)和加载因子(0.75)。

Therefore, using the HashSet(Collection) constructor to create the HashSet would probably be a better option in terms of performance and efficiency. 因此,在性能和效率方面,使用HashSet(Collection)构造函数创建HashSet可能是更好的选择。

However, from the standpoint of readability of the code, the variable name union seems to imply that the newly created Set is an union of another Set , so probably the one using the addAll method would be more understandable code. 但是,从代码的可读性的角度来看,变量名称union似乎暗示新创建的Set是另一个Set的并Set ,因此使用addAll方法的addAll可能是更容易理解的代码。

If the idea is just to make a new Set from an existing one, then the newly created Set should probably be named differently, such as newSet , copyOfS1 or something to that effect. 如果想法只是从现有的Set创建一个新的Set ,那么新创建的Set应该以不同的名称命名,例如newSetcopyOfS1或其他类似的东西。

There is no difference. 没有区别。 Both will create a new set that contains all of the elements in the set s1. 两者都将创建一个包含集合s1中所有元素的新集合。

Of course, your second code sample won't even compile, since you can't directly instantiate the Set interface, but I'm assuming that you meant to say new HashSet instead of new Set . 当然,你的第二个代码示例甚至不会编译,因为你不能直接实例化Set接口,但我假设你想说new HashSet而不是new Set

有一点不同,因为新的HashSet(s1)将事先确保所有元素都适合,并且不需要重新散列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM