Java Streams - IntStream reduce(int identity, IntBinaryOperator op) example








IntStream reduce(int identity, IntBinaryOperator op) pPerforms 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.

int reduce(int identity,  IntBinaryOperator op)

Example

The following example shows how to use reduce.

import java.util.stream.IntStream;
/*  www .j  a v a2 s  .c o  m*/
public class Main {
  public static void main(String[] args) {
    IntStream i = IntStream.of(6,5,7,1, 2, 3, 3);
    int v = i.reduce(0,(n1,n2)->n1+n2);

    System.out.println(v);  
    
  }
}

The code above generates the following result.