我试过了 this StackOverflow 答案代码,但我收到错误 Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>)
:
//data is int[][]
Arrays.stream(data)
.map(i -> Arrays.stream(i)
.collect(Collectors.toList()))
.collect(Collectors.toList());
最佳答案
Arrays.stream
将遍历每个 int[]
在int[][]
.
您可以转换 int[]
到IntStream
.
然后,为了转换 int
的流s 到 List<Integer>
,
您首先需要将它们装箱。
一次装箱到Integer
s,您可以将它们收集到列表中。
最后收集List<Integer>
的流放入列表中。
List<List<Integer>> list = Arrays.stream(data)
.map(row -> IntStream.of(row).boxed().collect(Collectors.toList()))
.collect(Collectors.toList());
关于java - 如何使用 Streams 将二维数组转换为二维列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44357940/