How to add an Array into Set properly?

冷暖自知 提交于 2020-08-19 04:17:51

问题


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

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.


回答1:


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

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

or add 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.




回答2:


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

Note that arrays in java are Objects so 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 Integer elements).

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

If you are using Java-8 and getting int[] and can not change to 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-8 you can directly collect array elements to the Set.

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



回答3:


You trying to insert into Set int values, but your Set stores Integer.

Change

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.




回答4:


Since Java 8, you can use Stream.

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

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

This should work.



来源:https://stackoverflow.com/questions/34478006/how-to-add-an-array-into-set-properly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!