0
0

More than 1 year has passed since last update.

stream reduce method

Posted at

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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0