从数组返回流为 int[] 而不是 Integer[]

Gil*_*oot 0 java arrays integer java-stream

我有这个数组:

Integer[] originalItems = itemsArray.stream()
            .distinct()
            .sorted()
            .toArray(Integer[]::new);
Run Code Online (Sandbox Code Playgroud)

我想返回它 asint[]而不是 as Integer[]。

我试图打电话,.toArray(int[]::new)但我收到此错误:

不存在类型变量 A 的实例,因此 int[] 符合 A[]

And*_*lko 6

.mapToInt(Integer::intValue).toArray();
Run Code Online (Sandbox Code Playgroud)

代替

.toArray(Integer[]::new);
Run Code Online (Sandbox Code Playgroud)

因为

<A> A[] toArray(IntFunction<A[]> generator);
Run Code Online (Sandbox Code Playgroud)

不适用于原始类型。