[英]Java 2D array into 2D ArrayList with stream
所以我有一个Integer[][] data
,我想将其转换为ArrayList<ArrayList<Integer>>
,所以我尝试使用流,并得出以下行:
ArrayList<ArrayList<Integer>> col = Arrays.stream(data).map(i -> Arrays.stream(i).collect(Collectors.toList())).collect(Collectors.toCollection(ArrayList<ArrayList<Integer>>::new));
但是最后一部分collect(Collectors.toCollection(ArrayList<ArrayList<Integer>>::new))
给我一个错误,它无法将ArrayList<ArrayList<Integer>> to C
转换ArrayList<ArrayList<Integer>> to C
内部的collect(Collectors.toList()
返回List<Integer>
,而不是ArrayList<Integer>
,因此您应该将这些内部List
收集到ArrayList<List<Integer>>
:
ArrayList<List<Integer>> col =
Arrays.stream(data)
.map(i -> Arrays.stream(i)
.collect(Collectors.toList()))
.collect(Collectors.toCollection(ArrayList<List<Integer>>::new));
或者,使用Collectors.toCollection(ArrayList<Integer>::new)
收集内部Stream
的元素:
ArrayList<ArrayList<Integer>> col =
Arrays.stream(data)
.map(i -> Arrays.stream(i)
.collect(Collectors.toCollection(ArrayList<Integer>::new)))
.collect(Collectors.toCollection(ArrayList<ArrayList<Integer>>::new));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.