Java Streams - LongStream reduce(long identity, LongBinaryOperator op) example








LongStream reduce(long identity, LongBinaryOperator op) performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value.

Syntax

reduce has the following syntax.

long reduce(long identity, LongBinaryOperator op)

Example

The following example shows how to use reduce.

import java.util.stream.LongStream;
//w  ww.  ja  v  a 2  s . co m
public class Main {
  public static void main(String[] args) {
    LongStream b = LongStream.of(1L, 2L, 3L, 4L);
    
    long v = b.reduce(0,Long::sum);
    System.out.println(v);
    
  }
}

The code above generates the following result.