Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java int[]数组到哈希集<;整数>;_Java_Generics_Collections - Fatal编程技术网

Java int[]数组到哈希集<;整数>;

Java int[]数组到哈希集<;整数>;,java,generics,collections,Java,Generics,Collections,我有一个int数组: int[] a = {1, 2, 3}; 我需要从中键入一组: Set<Integer> s; 当然,它认为我的意思是: List<int[]> 到 谢谢 您可以在以下位置使用ArrayUtils: 或者您可以轻松地使用将int[]转换为List: asList 公共静态列表asList(int…backingArray) 返回指定数组支持的固定大小列表,类似于Arrays.asList(Object[])。列表支持list.set(int,

我有一个int数组:

int[] a = {1, 2, 3};
我需要从中键入一组:

Set<Integer> s;
当然,它认为我的意思是:

List<int[]>

谢谢

您可以在以下位置使用ArrayUtils:

或者您可以轻松地使用将
int[]
转换为
List

asList
公共静态列表asList(int…backingArray)

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

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


一些进一步的解释。asList方法具有此签名

public static <T> List<T> asList(T... a)
公共静态列表asList(T…a)
因此,如果您这样做:

List<Integer> list = Arrays.asList(1, 2, 3, 4)
List List=Arrays.asList(1,2,3,4)
或者这个:

List<Integer> list = Arrays.asList(new Integer[] { 1, 2, 3, 4 })
List List=Arrays.asList(新整数[]{1,2,3,4})
在这些情况下,我相信java能够推断出您想要返回一个列表,因此它填充类型参数,这意味着它希望方法调用使用整数参数。因为它能够将值从int自动装箱到Integer,所以这很好

然而,这是行不通的

List<Integer> list = Arrays.asList(new int[] { 1, 2, 3, 4} )
List List=Arrays.asList(新的int[]{1,2,3,4})
因为原语到包装器的强制(即int[]到Integer[])没有内置到语言中(不确定他们为什么没有这样做,但他们没有)

因此,每个原语类型都必须作为其自己的重载方法来处理,这就是commons包所做的。即

public static List<Integer> asList(int i...);
公共静态列表asList(int i.);

该问题提出两个独立的问题:将
int[]
转换为
Integer[]
和从
int[]
创建
哈希集。使用Java 8流,两者都很容易做到:

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));
int[]数组=。。。
整数[]boxedArray=IntStream.of(array).boxed().toArray(整数[]::new);
Set Set=IntStream.of(array.boxed().collect(Collectors.toSet());
//或者如果您特别需要哈希集
HashSet HashSet=IntStream.of(数组).boxed()
.collect(Collectors.toCollection(HashSet::new));

另一个选项是使用来自的基元集。您可以轻松地将
int[]
转换为
MutableIntSet
转换为
Set
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[]{});

注意:我是Eclipse集合的提交者

只需使用下面的代码片段将数组中的元素添加到集合中即可

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);  
    }

}
公共类RemovedUpplicateElements{
公共静态void main(字符串参数[]){
int数组[]={0,1,2,3,4,5,6,7,8,9,1,2,3,4,5};
Set abc=newhashset();
对于(整数t:array){
abc.add(t);
}       
System.out.println(“样本集”+abc);
}
}
使用流:

// int[] nums = {1,2,3,4,5}
Set<Integer> set = Arrays.stream(nums).boxed().collect(Collectors.toSet())
//int[]nums={1,2,3,4,5}
Set Set=Arrays.stream(nums).boxed().collect(Collectors.toSet())

可能的重复:如果Commons有一个方法来做这件事,这意味着没有一种方法可以用基本JDK做这件事。。。谢谢你的指点!Commons库占用了Android上大量的方法引用,强烈建议不要使用它。这是有意义的,并且适用于普通Java。我相信你也可以调用
数组。asList(1,2,3,4)
问题的标题是
Java int[]数组到HashSet
,你的答案中没有
HashSet
。我认为值得一提的是,这行技术比较慢。我使用了
IntStream.of(nums).boxed().collect(Collectors.toSet())
而不是仅仅在元素上循环,这将我的运行时间平均从18ms增加到25ms。这应该是这个问题的最佳答案。OP在问JAVA8。
List<Integer> list = Arrays.asList(new int[] { 1, 2, 3, 4} )
public static List<Integer> asList(int i...);
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));
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[]{});
Integer[] integers = 
        IntLists.mutable.with(a).collect(i -> i).toArray(new Integer[]{});
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);  
    }

}
// int[] nums = {1,2,3,4,5}
Set<Integer> set = Arrays.stream(nums).boxed().collect(Collectors.toSet())