DoubleStream reduce(DoubleBinaryOperator op) example

Description

DoubleStream reduce(DoubleBinaryOperator op) performs a reduction on the elements of this stream, using an associative accumulation function, and returns an OptionalDouble describing the reduced value, if any.

Syntax

reduce has the following syntax.


OptionalDouble reduce(DoubleBinaryOperator op)

Example

The following example shows how to use reduce.


import java.util.OptionalDouble;
import java.util.stream.DoubleStream;
/*from   www . j  a  v  a 2 s.c o  m*/
public class Main {
  public static void main(String[] args) {
    DoubleStream b = DoubleStream.of(1.1, 2.2, 3.3, 4.4, 5.5);

    OptionalDouble d = b.reduce(Double::sum);
    if (d.isPresent()) {
      System.out.println(d.getAsDouble());
    } else {
      System.out.println("no value");
    }
  }
}

The code above generates the following result.





















Home »
  Java Streams »
    Reference »




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