简体   繁体   English

将集合类型转换为 HashSet 和使用集合初始化 HashSet 有什么区别?

[英]What is the difference between type casting a set to HashSet and initializing a HashSet with a set?

I am looking to convert a set values (from Collectors.toSet() ) into a HashSet, what is the difference between these two and which is generally preferred?我正在寻找将一组values (来自Collectors.toSet() )转换为 HashSet,这两者之间有什么区别,通常首选哪个?

HashSet<Integer> set = new HashSet<Integer>(values);
HashSet<Integer> set = (HashSet<Integer>) values;

I don't see the second option used as much, but wouldn't it be faster to cast than creating a new object?我没有看到第二个选项使用得那么多,但它不会比创建一个新的 object 更快吗?

The latter will throw a ClassCastException if the original Set wasn't a HashSet .如果原始Set不是HashSet ,后者将抛出ClassCastException

You're not casting a set anywhere here.你不是在这里的任何地方铸造一个集合。 Set is an interface which means it can't be instantiated (classes that implement set can be instantiated). Set 是一个接口,这意味着它不能被实例化(实现 set 的类可以被实例化)。 What this means is that your variable values need an implementation such as a hashset.这意味着您的变量values需要一个实现,例如哈希集。 So, option two only works if already instantiated some sort of set, which would be slower(more verbose is what I think you mean).因此,选项二仅在已经实例化某种集合的情况下才有效,这会更慢(我认为你的意思是更冗长)。 Like this:像这样:

Set<Integer> = new HashSet<Integer> values;
HashSet<Integer> set = (HashSet<Integer>) values. 

It's likely no one is going to do something like this in the first place, it makes more sense to stick with just plain old making a hashet:很可能一开始就没有人会做这样的事情,坚持只做一个简单的老方法更有意义:

HashSet<Integer> set = new HashSet<Integer>();

You may also be thinking that its ok to do without an implementation like:您可能还认为没有以下实现也可以:

1 Set<Integer> values; 
2 HashSet<Integer> set = (HashSet<Integer>) values;

Since in line 1 we can create a variable of the Set interface (keep in mind no actual object) this line is ok.因为在第 1 行我们可以创建一个 Set 接口的变量(记住没有实际的对象),所以这行没问题。 But without an implementation there would be no point in casting at line 2.但是如果没有实现,那么在第 2 行进行转换就没有意义了。

To sum it up, this question doesn't make too much sense in the first place.总而言之,这个问题首先没有太大意义。 Cast where you actually need to cast.在您实际需要投射的地方投射。

The primary difference is that new creates a new object, but a type cast doesn't.主要区别在于new创建了一个新的 object,但类型转换不会。

Consider the following example:考虑以下示例:

HashSet<Integer> one = ...

HashSet<Integer> two = new HashSet<Integer>(one);
HashSet<Integer> three = (HashSet<Integer>) one;

System.out.println(one == two);  // Prints false
System.out.println(one == three);  // Prints true

In other words, one refers to the same object as three , but two refers to a different object.换句话说, onethree指代相同的 object,但two指代不同的 object。

Apart from the different result from == , you will find that updates to the set referred to by one will be visible via three but not via two .除了与==不同的结果之外,您会发现对one引用的集合的更新将通过three可见,但不能通过two可见。


I don't see the second option used as much, but wouldn't it be faster to cast than creating a new object?我没有看到第二个选项使用得那么多,但它不会比创建一个新的 object 更快吗?

The reason you don't see it used much is because the second option does NOT create a new object!您看不到它使用太多的原因是因为第二个选项不会创建新对象!

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

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