java将int数组转换为Set时未能成功使用Collectors.toSet()

huangapple go评论67阅读模式
英文:

java Failed to convert int array to Set using Collectors.toSet()

问题

我在这里有一个基本的东西理解不了,我有:

int[] arr = {2,5,2,4,6,6,1,5,4};
Set<Integer> orederSet = new HashSet<Integer>(Arrays.stream(arr).collect(Collectors.toSet()));

另外,这个方法也不起作用:

Set<Integer> orederSet = new HashSet<Integer>(Arrays.asList(arr));

这给我一个编译错误:

java: method collect in interface java.util.stream.IntStream cannot be applied to given types;
  required: java.util.function.Supplier<R>,java.util.function.ObjIntConsumer<R>,java.util.function.BiConsumer<R,R>
  found: java.util.stream.Collector<java.lang.Object,capture#1 of ?,java.util.Set<java.lang.Object>>
  reason: cannot infer type-variable(s) R
    (actual and formal argument lists differ in length)

我不明白我的代码哪里有问题。

英文:

i can't understand basic thing here i have :

 int[] arr = {2,5,2,4,6,6,1,5,4};
        Set&lt;Integer&gt; orederSet  = new HashSet&lt;Integer&gt;(Arrays.stream(arr).collect(Collectors.toSet()));

side note :
also this not working :

Set&lt;Integer&gt; orederSet  = new HashSet&lt;Integer&gt;(Arrays.asList(arr));

which gives me compile error :

java: method collect in interface java.util.stream.IntStream cannot be applied to given types;
  required: java.util.function.Supplier&lt;R&gt;,java.util.function.ObjIntConsumer&lt;R&gt;,java.util.function.BiConsumer&lt;R,R&gt;
  found: java.util.stream.Collector&lt;java.lang.Object,capture#1 of ?,java.util.Set&lt;java.lang.Object&gt;&gt;
  reason: cannot infer type-variable(s) R
    (actual and formal argument lists differ in length) 

I dont understand what is wrong in my code ..

答案1

得分: 2

尝试转换 IntStream:

Set<Integer> orderSet = Arrays.stream(arr).boxed().collect(Collectors.toSet());
英文:

Try to convert IntStream:

Set&lt;Integer&gt; orderSet = Arrays.stream(arr).boxed().collect(Collectors.toSet());

答案2

得分: 2

你的 arr 是一个 int[]。这意味着你正在调用 Arrays#stream(int[]),它返回一个 IntStream。但是原始流接口<sup>1</sup>中没有 #collect(Collector) 方法。你需要将 IntStream 转换为 Stream&lt;Integer&gt;。最简单的方法是使用 IntStream#boxed() 方法。

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

Set&lt;Integer&gt; set =
        Arrays.stream(arr)                  // IntStream
              .boxed()                      // Stream&lt;Integer&gt;
              .collect(Collectors.toSet()); // Set&lt;Integer&gt;

至于为什么以下代码不起作用:

Set&lt;Integer&gt; set = new HashSet&lt;&gt;(Arrays.asList(arr));

那是因为 Arrays.asList(arr) 返回的是一个 List&lt;int[]&gt; 而不是一个 List&lt;Integer&gt;

原始类型和数组在泛型中都无法很好地工作。原始类型数组尤其糟糕。原始类型至少在适当时可以自动装箱为引用类型(反之亦然,即拆箱)。但是原始类型数组没有这样的特殊处理。


<sup>1. 原始流接口包括 IntStreamLongStreamDoubleStream。</sup>

英文:

Your arr is an int[]. That means you're calling Arrays#stream(int[]), which returns an IntStream. But none of the primitive stream interfaces<sup>1</sup> have a #collect(Collector) method. You have to convert the IntStream into a Stream&lt;Integer&gt;. The easiest way to do that is with the IntStream#boxed() method.

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

Set&lt;Integer&gt; set =
        Arrays.stream(arr)                  // IntStream
              .boxed()                      // Stream&lt;Integer&gt;
              .collect(Collectors.toSet()); // Set&lt;Integer&gt;

As for why the following doesn't work:

Set&lt;Integer&gt; set = new HashSet&lt;&gt;(Arrays.asList(arr));

That's due to Arrays.asList(arr) returning a List&lt;int[]&gt; rather than a List&lt;Integer&gt;.

Neither primitives nor arrays work especially well with generics. Arrays of primitives are worse. A primitive can at least be auto-boxed to the reference type (and vice versa, i.e., unboxed) when appropriate. But primitive arrays have no such special treatment.


<sup>1. The primitive stream interfaces include IntStream, LongStream, and DoubleStream.</sup>

huangapple
  • 本文由 发表于 2023年2月18日 16:32:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定