如何正确地将数组添加到集合中?
How to add an Array into Set properly?
我正在尝试将整数数组添加到集合中,如下所示,
int[] arr = { 2, 6, 4 , 2, 3, 3, 1, 7 };
Set<Integer> set = new HashSet<Integer>(Arrays.asList(arr));
我收到如下错误提示,
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
其次,我也尝试如下,但仍然出现错误,
int[] arr = { 2, 6, 4 , 2, 3, 3, 1, 7 };
Set<Integer> set = new HashSet<Integer>( );
Collections.addAll(set, arr);
如何在Java的集合中正确添加一个整数数组?谢谢。
您需要使用wrapper类型才能使用Arrays.asList(T...)
Integer[] arr = { 2, 6, 4, 2, 3, 3, 1, 7 };
Set<Integer> set = new HashSet<>(Arrays.asList(arr));
或像
一样手动添加元素
int[] arr = { 2, 6, 4, 2, 3, 3, 1, 7 };
Set<Integer> set = new HashSet<>();
for (int v : arr) {
set.add(v);
}
最后,如果需要保留插入顺序,可以使用LinkedHashSet
。
您试图插入 Set
int
值,但您的 Set
存储 Integer
.
改变
int[] arr = { 2, 6, 4, 2, 3, 3, 1, 7 };
至
Integer[] arr = { 2, 6, 4, 2, 3, 3, 1, 7 };
此外,当您要从整数数组创建一个集合时,请记住整数有一个特殊的缓存池用于范围 -127 to +128
之间的整数。值在此范围内的所有 Integer 对象都引用池中的相同对象。因此,不会为 Set 中的整数分配新内存。
myTest.java:192: error: no suitable constructor found for HashSet(List<int[]>)
请注意,java 中的数组是 Objects
,因此 Arrays.asList(int[])
将在内部将 int[]
视为单个元素。因此,<T> List<T> asList(T... a)
将创建 List<int[]>
而不是 List<Integer>
,因此您不能从数组集合(不是 Integer
元素)创建 Set<Integer>
。
可能的解决方案是,只需使用 Integer
(包装器 class)而不是 int
(原始类型)。(Elliott Frisch
已经说明)。
如果您正在使用 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));
此外,正如Louis Wasserman
所指出的,如果您使用java-8
,您可以直接将数组元素收集到Set
。
Set<Integer> set = Arrays.stream(arr).boxed().collect(Collectors.toSet());
从Java8开始,可以使用Stream了。
int[] arr = { 2, 6, 4 , 2, 3, 3, 1, 7 };
Set<Integer> set = Arrays.stream(number).boxed().collect(Collectors.toSet());
这应该有效。
我正在尝试将整数数组添加到集合中,如下所示,
int[] arr = { 2, 6, 4 , 2, 3, 3, 1, 7 };
Set<Integer> set = new HashSet<Integer>(Arrays.asList(arr));
我收到如下错误提示,
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
其次,我也尝试如下,但仍然出现错误,
int[] arr = { 2, 6, 4 , 2, 3, 3, 1, 7 };
Set<Integer> set = new HashSet<Integer>( );
Collections.addAll(set, arr);
如何在Java的集合中正确添加一个整数数组?谢谢。
您需要使用wrapper类型才能使用Arrays.asList(T...)
Integer[] arr = { 2, 6, 4, 2, 3, 3, 1, 7 };
Set<Integer> set = new HashSet<>(Arrays.asList(arr));
或像
一样手动添加元素int[] arr = { 2, 6, 4, 2, 3, 3, 1, 7 };
Set<Integer> set = new HashSet<>();
for (int v : arr) {
set.add(v);
}
最后,如果需要保留插入顺序,可以使用LinkedHashSet
。
您试图插入 Set
int
值,但您的 Set
存储 Integer
.
改变
int[] arr = { 2, 6, 4, 2, 3, 3, 1, 7 };
至
Integer[] arr = { 2, 6, 4, 2, 3, 3, 1, 7 };
此外,当您要从整数数组创建一个集合时,请记住整数有一个特殊的缓存池用于范围 -127 to +128
之间的整数。值在此范围内的所有 Integer 对象都引用池中的相同对象。因此,不会为 Set 中的整数分配新内存。
myTest.java:192: error: no suitable constructor found for HashSet(List<int[]>)
请注意,java 中的数组是 Objects
,因此 Arrays.asList(int[])
将在内部将 int[]
视为单个元素。因此,<T> List<T> asList(T... a)
将创建 List<int[]>
而不是 List<Integer>
,因此您不能从数组集合(不是 Integer
元素)创建 Set<Integer>
。
可能的解决方案是,只需使用 Integer
(包装器 class)而不是 int
(原始类型)。(Elliott Frisch
已经说明)。
如果您正在使用 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));
此外,正如Louis Wasserman
所指出的,如果您使用java-8
,您可以直接将数组元素收集到Set
。
Set<Integer> set = Arrays.stream(arr).boxed().collect(Collectors.toSet());
从Java8开始,可以使用Stream了。
int[] arr = { 2, 6, 4 , 2, 3, 3, 1, 7 };
Set<Integer> set = Arrays.stream(number).boxed().collect(Collectors.toSet());
这应该有效。