我有这个数组:
Integer[] originalItems = itemsArray.stream()
.distinct()
.sorted()
.toArray(Integer[]::new);
我想将其返回为 int[]
而不是 Integer[]
.
我尝试调用.toArray(int[]::new)
但我收到此错误:
no instance(s) of type variable(s) A exist so that int[] conforms to A[]
最佳答案
.mapToInt(Integer::intValue).toArray();
而不是
.toArray(Integer[]::new);
因为
<A> A[] toArray(IntFunction<A[]> generator);
不适用于原始类型。
关于java - 从数组返回流作为 int[] 而不是 Integer[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61188177/