JavaStreamreduce

アキュムレータを使用して要素を縮減します。

構文

stream.reduce(identity, accumulator)

使用例

下記の値を入力するとサンプルに即時反映されます。

List
nums
of
stream
reduce
System
out
println
List<Integer> nums = List.of(1, 2, 3, 4, 5);
int sum = nums.stream()
    .reduce(0, Integer::sum);
System.out.println(sum); // 15