DoubleStream sum() method in Java


The sum() method of the DoubleStream class in Java returns the sum of elements in this stream.

The syntax is as follows

double sum()

To use the DoubleStream class in Java, import the following package

import java.util.stream.DoubleStream;

Create DoubleStream and add some elements

DoubleStream doubleStream = DoubleStream.of(23.6, 45.3, 59.6, 60.6, 73.6, 84.7, 94.8);

Now, sum the elements of the stream

double sum = doubleStream.sum();

The following is an example to implement DoubleStream sum() method in Java

Example

 Live Demo

import java.util.*;
import java.util.stream.DoubleStream;
public class Demo {
   public static void main(String[] args) {
      DoubleStream doubleStream = DoubleStream.of(23.6, 45.3, 59.6, 60.6, 73.6, 84.7, 94.8);
      double sum = doubleStream.sum();
      System.out.println("The sum of all the elements of the stream = "+sum);
   }
}

Output

The sum of all the elements of the stream = 442.2

Updated on: 30-Jul-2019

135 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements