Stream流の二维数组List<List>互转
少而好学,如日出之阳;壮而好学,如日中之光;老而好学,如炳烛之明。一一刘向
数组转List<List<Integer>>
List<List<Integer>> collect = Arrays.stream(array).map(a1 -> Arrays.stream(a1).boxed().collect(Collectors.toList())).collect(Collectors.toList()); |
List<List<Integer>>
转int[][]
array = collect.stream().map(integers -> integers.stream().mapToInt(value -> value).toArray()).toArray(int[][]::new); |
二维数组和List<List<Integer>>
之间的转换使用stream
的话就非常简单了
int[][] array = new int[][]{{1, 2}, {1, 3, 3, 4}, {2, 3}}; |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 蒋立坤的博客!
评论
Va