如何收集List<Integer>
包含二维int[][]
数组每一行的第一个元素。
请看下面的示例代码
int[][] input = new int[][] { {1,2,3,4,5}, {4,5,6,7,8} };
// Expecting output : List<Integer> -> {1, 4 }
// Trying something like this
List<Integer> output = Arrays.stream(input)
.collect(r -> r[0]);
采用 map(Function<? super T, ? extends R> mapper);
List<Integer> output = Arrays.stream(input)
.map(r->r[0])
.collect(Collectors.toList());
如果您只想使用collect
-><R> R collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner)
这样做:
Arrays.stream(input)
.collect(ArrayList::new,(l,e)->l.add(e[0]),List::addAll);
供应商: ArrayList::new
累加器:(l,e)->l.add(e[0])
合路器: List::addAll
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句