**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.

**Syntax :**

double sum()

**Return Value : **The function returns the sum of elements in this stream.

**Note : **

- 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 :**

`// Java code for DoubleStream.sum() to ` `// find the sum of elements in DoubleStream ` `import` `java.util.*; ` `import` `java.util.stream.DoubleStream; ` ` ` `class` `GFG { ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// Creating an DoubleStream ` ` ` `DoubleStream stream = DoubleStream.of(` `2.2` `, ` `4.3` `, ` `6.4` `, ` ` ` `-` `2.5` `, -` `4.6` `); ` ` ` ` ` `// Using DoubleStream.sum() to find ` ` ` `// sum of elements in DoubleStream ` ` ` `double` `sumOfElements = stream.sum(); ` ` ` ` ` `// Displaying the calculated sum ` ` ` `System.out.println(sumOfElements); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

5.800000000000001

**Example 2 :**

`// Java code for DoubleStream.sum() to ` `// find the sum of elements ` `// greater than 2.5 ` `import` `java.util.*; ` `import` `java.util.stream.DoubleStream; ` ` ` `class` `GFG { ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// Creating an DoubleStream ` ` ` `// Using DoubleStream.sum() to find ` ` ` `// sum of elements greater than 2.5 ` ` ` `double` `sumOfElements = DoubleStream.of(` `2.2` `, ` `4.2` `, ` `6.4` `, ` ` ` `-` `2.5` `, -` `4.5` `) ` ` ` `.filter(num -> num > ` `2.5` `) ` ` ` `.sum(); ` ` ` ` ` `// Displaying the calculated sum ` ` ` `System.out.println(sumOfElements); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

10.600000000000001

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

## Recommended Posts:

- DoubleStream of() in Java
- DoubleStream summaryStatistics() in Java
- DoubleStream toArray() in Java
- DoubleStream parallel() in Java
- DoubleStream sorted() in Java
- DoubleStream sequential() in Java
- DoubleStream concat() in Java
- DoubleStream max() in Java with examples
- DoubleStream mapToLong() in Java
- DoubleStream min() in Java with examples
- DoubleStream skip() in Java
- DoubleStream iterator() in Java
- DoubleStream mapToInt() in Java
- DoubleStream mapToObj() in Java
- DoubleStream peek() in Java with examples
- DoubleStream anyMatch() in Java with examples
- DoubleStream limit() in Java with Examples
- DoubleStream reduce(DoubleBinaryOperator op) in Java
- DoubleStream.Builder build() in Java
- DoubleStream boxed() in Java with Examples

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.