如何在原始数组中收集 Stream 的结果?

How to collect the results of a Stream in a primitive array?

我正在尝试将二维列表转换为二维 int 数组。但是,我好像只能收集对象,不能收集原语。

当我这样做时:

data.stream().map(l -> l.stream().toArray(int[]::new)).toArray(int[][]::new);

我收到编译时错误 Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>)

但是,如果我将 int[] 更改为 Integer[],它会编译。我怎样才能让它只使用 int?

使用mapToInt方法生成原始整数流:

int[][] res = data.stream().map(l -> l.stream().mapToInt(v -> v).toArray()).toArray(int[][]::new);

内部 toArray 调用不再需要 int[]::new,因为 IntStream 产生 int[]

Demo.