Program/ Example - create Double Stream and use Reduce Method on it in java 8
/**
* Write a program to - create Double Stream and use Reduce Method on it in java 8
*/
import java.util.OptionalDouble;
import java.util.stream.DoubleStream;
public class DoubleStreamReduceMethodExample {
public static void main(String[] args) {
System.out.println("Create DoubleStream");
DoubleStream streamOfIntegers = DoubleStream.of(1.1, 2.2, 3.1);
System.out.println("Use reduce() method - to calculate sum of Double in DoubleStream");
//reduce() method will reduce stream to calculate sum of Integers in DoubleStream
OptionalDouble optionalDouble = streamOfIntegers.reduce((i1, i2) -> i1 + i2);
//Display optionalDouble
System.out.println("sum = "+optionalDouble.getAsDouble()); //get() returns the value.
}
}
/* OUTPUT
Create DoubleStream
Use reduce() method - to calculate sum of Double in DoubleStream
sum = 6.4
*/
|
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.