LongStream reduce(long identity, LongBinaryOperator op) example

Description

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 w w  . java  2s . c  o 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.





















Home »
  Java Streams »
    Reference »




Collectors
DoubleStream
DoubleStream.Builder
IntStream
IntStream.Builder
LongStream
LongStream.Builder
Stream
Stream.Builder