reduce methodは、BinalyOperatorを引き渡し累積的に処理する
return type はOperator<T>
初期値を渡すと、初期値の型を返す
public class Outer {
public static void main(String[] args) {
List<Integer> l = List.of(1,2,3,4,5);
l.stream().reduce((a,b) -> {
System.out.print("a:" + a);
System.out.println(" b:" + b);
return a+b;
}).ifPresent(System.out::println);
int sum = l.stream().reduce(100,(a,b) -> {
System.out.print("a:" + a);
System.out.println(" b:" + b);
return a+b;
});
System.out.println(sum);
}
}
a:1 b:2
a:3 b:3
a:6 b:4
a:10 b:5
15
a:100 b:1
a:101 b:2
a:103 b:3
a:106 b:4
a:110 b:5
115