我有这个数组:
Integer[] originalItems = itemsArray.stream()
.distinct()
.sorted()
.toArray(Integer[]::new);
我想将其返回为int[]
而不是Integer[]
。
我曾尝试致电.toArray(int[]::new)
,但出现此错误:不存在类型变量A的实例,因此int []符合A []
.mapToInt(Integer::intValue).toArray();
代替
.toArray(Integer[]::new);
因为
<A> A[] toArray(IntFunction<A[]> generator);
不适用于基本类型。
Stream<Integer>
个对象流。您可以将其转换为通过IntStream
处理原始int
的mapToInt(i->i)
,然后在该流上调用toArray()
方法,该方法返回int[]