DoubleStream sum() method in Java

Java 8ProgrammingObject Oriented Programming

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
raja
Published on 26-Mar-2019 14:46:56
Advertisements