英文:
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<Integer> orederSet = new HashSet<Integer>(Arrays.stream(arr).collect(Collectors.toSet()));
side note :
also this not working :
Set<Integer> orederSet = new HashSet<Integer>(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<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 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<Integer> orderSet = Arrays.stream(arr).boxed().collect(Collectors.toSet());
答案2
得分: 2
你的 arr
是一个 int[]
。这意味着你正在调用 Arrays#stream(int[])
,它返回一个 IntStream
。但是原始流接口<sup>1</sup>中没有 #collect(Collector)
方法。你需要将 IntStream
转换为 Stream<Integer>
。最简单的方法是使用 IntStream#boxed()
方法。
int[] arr = {2, 5, 2, 4, 6, 6, 1, 5, 4};
Set<Integer> set =
Arrays.stream(arr) // IntStream
.boxed() // Stream<Integer>
.collect(Collectors.toSet()); // Set<Integer>
至于为什么以下代码不起作用:
Set<Integer> set = new HashSet<>(Arrays.asList(arr));
那是因为 Arrays.asList(arr)
返回的是一个 List<int[]>
而不是一个 List<Integer>
。
原始类型和数组在泛型中都无法很好地工作。原始类型数组尤其糟糕。原始类型至少在适当时可以自动装箱为引用类型(反之亦然,即拆箱)。但是原始类型数组没有这样的特殊处理。
<sup>1. 原始流接口包括 IntStream
、LongStream
和 DoubleStream
。</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<Integer>
. The easiest way to do that is with the IntStream#boxed()
method.
int[] arr = {2, 5, 2, 4, 6, 6, 1, 5, 4};
Set<Integer> set =
Arrays.stream(arr) // IntStream
.boxed() // Stream<Integer>
.collect(Collectors.toSet()); // Set<Integer>
As for why the following doesn't work:
Set<Integer> set = new HashSet<>(Arrays.asList(arr));
That's due to Arrays.asList(arr)
returning a List<int[]>
rather than a List<Integer>
.
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论