- 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
LongStream sum() method in Java
The sum() method of the LongStream class returns the sum of elements in this stream.
The syntax is as follows
long sum()
To use the LongStream class in Java, import the following package
import java.util.stream.LongStream;
First, create an IntStream and add some elements
LongStream longStream = LongStream.of(100L, 30000L, 45000L, 55000L, 70000L);
Now, add the elements of the stream
long res = longStream.sum();
The following is an example to implement LongStream sum() method in Java
Example
import java.util.stream.LongStream; public class Demo { public static void main(String[] args) { LongStream longStream = LongStream.of(100L, 30000L, 45000L, 55000L, 70000L); long res = longStream.sum(); System.out.println("The sum of elements in the stream = "+res); } }
Output
The sum of elements in the stream = 200100
Advertisements
To Continue Learning Please Login
Login with Google