48

我有一个int数组:

int[] a = {1, 2, 3};

我需要一个类型的集合:

Set<Integer> s;

如果我执行以下操作:

s = new HashSet(Arrays.asList(a));

它当然认为我的意思是:

List<int[]>

而我的意思是:

List<Integer>

这是因为 int 是一个原语。如果我使用过 String,一切都会奏效:

Set<String> s = new HashSet<String>(
    Arrays.asList(new String[] { "1", "2", "3" }));

如何轻松、正确、简洁地从:

A) int[] a...

B) Integer[] a ...

谢谢!

4

7 回答 7

41

使用流:

// int[] nums = {1,2,3,4,5}
Set<Integer> set = Arrays.stream(nums).boxed().collect(Collectors.toSet())
于 2020-04-25T17:30:41.653 回答
20

该问题提出了两个单独的问题:从. 转换int[]Integer[]和创建 a 。两者都可以通过 Java 8 流轻松实现:HashSet<Integer>int[]

int[] array = ...
Integer[] boxedArray = IntStream.of(array).boxed().toArray(Integer[]::new);
Set<Integer> set = IntStream.of(array).boxed().collect(Collectors.toSet());
//or if you need a HashSet specifically
HashSet<Integer> hashset = IntStream.of(array).boxed()
    .collect(Collectors.toCollection(HashSet::new));
于 2016-03-20T03:15:18.793 回答
11

Some further explanation. The asList method has this signature

public static <T> List<T> asList(T... a)

So if you do this:

List<Integer> list = Arrays.asList(1, 2, 3, 4)

or this:

List<Integer> list = Arrays.asList(new Integer[] { 1, 2, 3, 4 })

In these cases, I believe java is able to infer that you want a List back, so it fills in the type parameter, which means it expects Integer parameters to the method call. Since it's able to autobox the values from int to Integer, it's fine.

However, this will not work

List<Integer> list = Arrays.asList(new int[] { 1, 2, 3, 4} )

because primitive to wrapper coercion (ie. int[] to Integer[]) is not built into the language (not sure why they didn't do this, but they didn't).

As a result, each primitive type would have to be handled as it's own overloaded method, which is what the commons package does. ie.

public static List<Integer> asList(int i...);
于 2012-08-20T03:28:42.770 回答
4

或者您可以轻松使用Guava转换int[]List<Integer>

Ints.asList(int...)

作为列表

public static List<Integer> asList(int... backingArray)

返回由指定数组支持的固定大小列表,类似于Arrays.asList(Object[]). 该列表支持List.set(int, Object),但任何设置值的尝试null都会导致NullPointerException.

Integer返回的列表维护写入或读取的对象的值,但不维护标识。例如,list.get(0) == list.get(0)未指定返回列表是否为真。

于 2012-08-19T23:34:48.053 回答
2

您可以在Apache Commons中使用 ArrayUtils :

int[] intArray  = { 1, 2, 3 };
Integer[] integerArray = ArrayUtils.toObject(intArray);
于 2012-08-19T23:11:48.060 回答
1

另一种选择是使用来自Eclipse Collections的原始集。您可以轻松地将 a 转换int[]MutableIntSetaSet<Integer>Integer[]如下所示,或者您可以按MutableIntSet原样使用它,这将提高内存效率和性能。

int[] a = {1, 2, 3};
MutableIntSet intSet = IntSets.mutable.with(a);
Set<Integer> integerSet = intSet.collect(i -> i);  // auto-boxing
Integer[] integerArray = integerSet.toArray(new Integer[]{});

如果您想直接从 int 数组转到 Integer 数组并保留顺序,那么这将起作用。

Integer[] integers = 
        IntLists.mutable.with(a).collect(i -> i).toArray(new Integer[]{});

注意:我是 Eclipse Collections 的提交者

于 2017-01-28T07:37:26.597 回答
0

只需使用以下代码段将数组中的元素添加到 Set

public class RemoveDuplicateElements {

    public static void main(String args[]){
        int array[] =  {0,1,2,3,4,5,6,7,8,9,1,2,3,4,5};
        Set <Integer> abc = new HashSet <Integer>();
        for (Integer t:array){  
            abc.add(t); 
        }       
        System.out.println("sampleSet"+abc);  
    }

}
于 2020-02-17T13:33:11.010 回答