Close

Java 8 Streams - DoubleStream.reduce Examples

Java 8 Streams Java Java API 


Interface:

java.util.stream.DoubleStream

java.lang.AutoCloseableAutoCloseablejava.util.stream.BaseStreamBaseStreamjava.util.stream.DoubleStreamDoubleStreamLogicBig

Methods:

These terminal operation performs a reduction on the elements.

double reduce(double identity,
              DoubleBinaryOperator op)
Parameters:
identity - the identity value for the accumulating function
op - a function for combining two values



OptionalDouble reduce(DoubleBinaryOperator op)

Parameters:
op - a function for combining two values


Examples


package com.logicbig.example.doublestream;

import java.util.stream.DoubleStream;

public class ReduceExample {

public static void main(String... args) {
DoubleStream ds = DoubleStream.of(1.0, 1.2, 2.0, 2.4, 3.0);
double d = ds.reduce(1, (a, b) -> a * b);
System.out.println(d);
}
}

Output

17.28




package com.logicbig.example.doublestream;

import java.util.OptionalDouble;
import java.util.stream.DoubleStream;

public class ReduceExample2 {

public static void main(String... args) {
DoubleStream ds = DoubleStream.of(1.0, 1.2, 2.0, 2.4, 3.0);
OptionalDouble od = ds.reduce((a, b) -> a * b);
if (od.isPresent()) {
System.out.println(od.getAsDouble());
}
}
}

Output

17.28




See Also