- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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
Advertisements