我正在学习Java 8 lambdas和stream.所以,我得到了一系列不同长度的列表.列表包含整数.
在另一个列表列表中收集垂直切片的最佳方法是什么,即从切片0中的所有原始列表,切片1中的索引1收集索引为0的所有整数,直到最长列表的长度为止(填充零为较短的列表)名单)
我知道为此编写几个传统循环代码是微不足道的,但是使用Java 8功能呢?
这是一个非常有趣的问题 - 感谢您的发帖。我相信您会看到一些有趣的答案。这是我的尝试:
List<Integer> source[];
List<List<Integer>> slices = IntStream.range(0, Arrays.stream(source).mapToInt(List::size).max().getAsInt())
.mapToObj(index -> Arrays.stream(source).map(list -> list.size() > index ? list.get(index) : 0)
.collect(Collectors.toList()))
.collect(Collectors.toList())
Run Code Online (Sandbox Code Playgroud)