【问题标题】:Java int[] array to HashSet<Integer>Java int[] 数组到 HashSet<Integer>
【发布时间】:2012-08-15 08:52:59
【问题描述】:

我有一个 int 数组:

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

我需要一个类型的集合:

Set<Integer> s;

如果我执行以下操作:

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

当然,它认为我的意思是:

List<int[]>

而我的意思是:

List<Integer>

这是因为 int 是一个原语。如果我使用了字符串,一切都会起作用:

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

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

A) int[] a...

B) Integer[] a ...

谢谢!

【问题讨论】:

标签: java generics collections


【解决方案1】:

使用流:

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

【讨论】:

    【解决方案2】:

    该问题提出了两个独立的问题:将int[] 转换为Integer[] 并从int[] 创建一个HashSet&lt;Integer&gt;。两者都可以通过 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));
    

    【讨论】:

    • 我认为值得一提的是这种线技术较慢。我使用IntStream.of(nums).boxed().collect(Collectors.toSet()) 而不是只循环元素,这将我的运行时间平均从 18 毫秒增加到 25 毫秒。
    【解决方案3】:

    一些进一步的解释。 asList 方法有这个签名

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

    如果你这样做:

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

    或者这个:

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

    在这些情况下,我相信 java 能够推断出您想要返回一个 List,因此它会填充类型参数,这意味着它需要 Integer 参数来调用方法。因为它能够将值从 int 自动装箱到 Integer,所以很好。

    但是,这行不通

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

    因为包装强制的原语(即 int[] 到 Integer[])没有内置到语言中(不知道他们为什么不这样做,但他们没有这样做)。

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

    public static List<Integer> asList(int i...);
    

    【讨论】:

    • 相信你也可以拨打Arrays.&lt;Integer&gt;asList(1, 2, 3, 4)
    • 问题的标题是Java int[] array to HashSet&lt;Integer&gt;,而您的答案中没有HashSet
    【解决方案4】:

    或者您可以轻松使用Guavaint[] 转换为List&lt;Integer&gt;

    Ints.asList(int...)

    asList

    public static List&lt;Integer&gt; asList(int... backingArray)

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

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

    【讨论】:

      【解决方案5】:

      你可以在Apache Commons中使用ArrayUtils:

      int[] intArray  = { 1, 2, 3 };
      Integer[] integerArray = ArrayUtils.toObject(intArray);
      

      【讨论】:

      • 如果 Commons 有方法可以做到这一点,这意味着没有办法使用基本 JDK 来做到这一点......感谢您的指点!
      • Commons 库在 Android 上占用了大量的方法引用,强烈建议避免使用。这很有意义,并且适用于普通 Java。
      【解决方案6】:

      另一种选择是使用来自Eclipse Collections 的原始集。您可以轻松地将int[] 转换为MutableIntSet 再转换为Set&lt;Integer&gt;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 的提交者

      【讨论】:

        【解决方案7】:

        使用下面的 sn-p 将数组中的元素添加到 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);  
            }
        
        }
        

        【讨论】:

        • OP正在询问JAVA8方式。
        猜你喜欢
        • 2011-01-27
        • 2019-03-31
        • 1970-01-01
        • 1970-01-01
        • 2014-07-19
        • 1970-01-01
        • 1970-01-01
        • 2013-04-28
        • 1970-01-01
        相关资源
        最近更新 更多