java 如何将数组正确添加到 Set 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34478006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:56:14  来源:igfitidea点击:

How to add an Array into Set properly?

javaset

提问by Arefe

I'm trying to add in Integer array into Set as following,

我正在尝试将整数数组添加到 Set 中,如下所示,

int[] arr = { 2, 6, 4 , 2, 3, 3, 1, 7 }; 
Set<Integer> set = new HashSet<Integer>(Arrays.asList(arr));

I'm getting some error telling as following,

我收到一些错误提示如下,

myTest.java:192: error: no suitable constructor found for HashSet(List<int[]>)
    Set<Integer> set = new HashSet<Integer>(Arrays.asList(arr));
                       ^
constructor HashSet.HashSet(Collection<? extends Integer>) is not applicable
  (argument mismatch; inferred type does not conform to upper bound(s)
      inferred: int[]
      upper bound(s): Integer,Object)
constructor HashSet.HashSet(int) is not applicable
  (argument mismatch; no instance(s) of type variable(s) T exist so that List<T> conforms to int)
 where T is a type-variable:
T extends Object declared in method <T>asList(T...)
Note: Some messages have been simplified; recompile with -Xdiags:verbose to        get full output
   1 error

Secondly, I also tries as following and still getting error,

其次,我也尝试如下但仍然出错,

int[] arr = { 2, 6, 4 , 2, 3, 3, 1, 7 }; 
Set<Integer> set = new HashSet<Integer>( );
Collections.addAll(set, arr);

How to add an Integer array into Set in Java properly ? Thanks.

如何在 Java 中正确地将整数数组添加到 Set 中?谢谢。

回答by Elliott Frisch

You need to use the wrapper type to use Arrays.asList(T...)

您需要使用包装器类型才能使用 Arrays.asList(T...)

Integer[] arr = { 2, 6, 4, 2, 3, 3, 1, 7 };
Set<Integer> set = new HashSet<>(Arrays.asList(arr));

oradd the elements manually like

手动添加元素,如

int[] arr = { 2, 6, 4, 2, 3, 3, 1, 7 };
Set<Integer> set = new HashSet<>();
for (int v : arr) {
    set.add(v);
}

Finally, if you need to preserve insertion order, you can use a LinkedHashSet.

最后,如果您需要保留插入顺序,您可以使用LinkedHashSet.

回答by CoderCroc

myTest.java:192: error: no suitable constructor found for HashSet(List<int[]>)

myTest.java:192: error: no suitable constructor found for HashSet(List<int[]>)

Note that arrays in java are Objectsso Arrays.asList(int[])will internally consider int[]as a single element. So, <T> List<T> asList(T... a)will create List<int[]>instead of List<Integer>and so you can not create Set<Integer>from collection of array (not Integerelements).

请注意,在Java数组是Objects这样Arrays.asList(int[])将在内部考虑int[]作为一个单一的元素。所以,<T> List<T> asList(T... a)将创建List<int[]>而不是,List<Integer>所以你不能Set<Integer>从数组(不是Integer元素)的集合中创建。

Possible solutions could be, just use Integer(wrapper class) instead of int(primitive type).(Which is already stated by Elliott Frisch).

可能的解决方案可能是,只使用Integer(wrapper class) 而不是int(primitive type)。(已经由 说明Elliott Frisch)。

If you are using Java-8and getting int[]and can not change to Integer[],

如果您正在使用Java-8和获取int[]并且无法更改为Integer[]

int[] arr = { 2, 6, 4, 2, 3, 3, 1, 7 };
Integer[] wrapper = Arrays.stream(arr).boxed().toArray(Integer[]::new);
Set<Integer> set = new HashSet<Integer>(Arrays.asList(wrapper));

Moreover, as pointed out by Louis Wasserman, if you are using java-8you can directly collect array elements to the Set.

此外,正如 所指出的Louis Wasserman,如果您正在使用,java-8您可以直接将数组元素收集到Set.

Set<Integer> set = Arrays.stream(arr).boxed().collect(Collectors.toSet());

回答by Azat Nugusbayev

You trying to insert into Setintvalues, but your Setstores Integer.

您试图插入Setint值,但您的Set商店Integer.

Change

改变

int[] arr = { 2, 6, 4, 2, 3, 3, 1, 7 };

int[] arr = { 2, 6, 4, 2, 3, 3, 1, 7 };

to

Integer[] arr = { 2, 6, 4, 2, 3, 3, 1, 7 };

Also as you are going to create a Set out of Array of Integers, remember that Integers have a special cache pool for Integer between range -127 to +128. All Integer objects with value within this range refer to same objects in pool. Hence no new memory will be allocated for Integers in the Set.

此外,当您要从整数数组中创建一个 Set 时,请记住 Integers 有一个特殊的缓存池,用于范围之间的 Integer -127 to +128。值在此范围内的所有 Integer 对象都引用池中的相同对象。因此不会为集合中的整数分配新的内存。

回答by anthonlavell

Since Java 8, you can use Stream.

从 Java 8 开始,您可以使用 Stream。

int[] arr = { 2, 6, 4 , 2, 3, 3, 1, 7 };

Set<Integer> set = Arrays.stream(number).boxed().collect(Collectors.toSet());

This should work.

这应该有效。