DoubleStream sum() in Java
DoubleStream sum() returns the sum of elements in this stream. This is a special case of a reduction. DoubleStream sum() is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect.
Note : A reduction operation (also called a fold) takes a sequence of input elements and combines them into a single summary result by repeated application of a combining operation, such as finding the sum or maximum of a set of numbers.
Return Value : The function returns the sum of elements in this stream.
- If any stream element is a NaN or the sum is at any point a NaN then the sum will be NaN.
- Elements sorted by increasing absolute magnitude tend to yield more accurate result.
Example 1 :
Example 2 :
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.