Open In App

LongStream sum() in Java

Last Updated : 25 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

LongStream sum() returns the sum of elements in this stream. This is a special case of a reduction. LongStream 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 :

long sum()

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

Example 1 : 

Java




// Java code for LongStream.sum() to
// find the sum of elements in LongStream
import java.util.*;
import java.util.stream.LongStream;
 
class GFG {
 
    // Driver code
    public static void main(String[] args)
    {
        // Creating an LongStream
        LongStream stream = LongStream.of(2L, 4L, 6L, -2L, -4L);
 
        // Using LongStream.sum() to find
        // sum of elements in LongStream
        long sumOfElements = stream.sum();
 
        // Displaying the calculated sum
        System.out.println(sumOfElements);
    }
}


Output:

6

Example 2 : 

Java




// Java code for LongStream.sum() to
// find the sum of elements
// divisible by 3 in given range
import java.util.*;
import java.util.stream.LongStream;
 
class GFG {
 
    // Driver code
    public static void main(String[] args)
    {
        // Using LongStream.sum() to find
        // sum of elements divisible by 3
        // in given range
        long sumOfElements = LongStream.range(2L, 10L)
                                 .filter(num -> num % 3 == 0)
                                 .sum();
 
        // Displaying the calculated sum
        System.out.println(sumOfElements);
    }
}


Output:

18


Similar Reads

LongStream generate() method in Java
LongStream generate(LongSupplier s) returns an infinite sequential unordered stream where each element is generated by the provided LongSupplier(a supplier of long-valued results). This is suitable for generating constant streams, streams of random elements, etc. Syntax : static LongStream generate(LongSupplier s) Parameters : LongStream : a sequen
1 min read
LongStream parallel() in Java
LongStream parallel() is a method in java.util.stream.LongStream. This method returns a parallel LongStream, i.e, it may return itself, either because the stream was already present, or because the underlying stream state was modified to be parallel. LongStream parallel() is an intermediate operation. These operations are always lazy. Intermediate
2 min read
LongStream peek() in Java with examples
LongStream peek() is a method in java.util.stream.LongStream. The function returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream. Syntax : LongStream peek(LongConsumer action) Where, LongStream is a sequence of primitive long-valued
3 min read
LongStream rangeClosed() in Java
LongStream rangeClosed(long startInclusive, long endInclusive) returns an LongStream from startInclusive (inclusive) to endInclusive (inclusive) by an incremental step of 1. Syntax : static LongStream rangeClosed(long startInclusive, long endInclusive) Parameters : LongStream : A sequence of primitive long-valued elements. startInclusive : The incl
1 min read
LongStream range() in Java
LongStream range(long startInclusive, long endExclusive) returns a sequential ordered LongStream from startInclusive (inclusive) to endExclusive (exclusive) by an incremental step of 1. Syntax : static LongStream range(long startInclusive, long endExclusive) Parameters : LongStream : A sequence of primitive long-valued elements. startInclusive : Th
1 min read
LongStream sequential() in Java
LongStream sequential() returns a sequential LongStream. It may return itself, either because the stream was already sequential, or because the underlying stream state was modified to be sequential. LongStream sequential() is an intermediate operation. Intermediate operations are invoked on a Stream instance and after they finish their processing,
2 min read
LongStream skip() in Java
LongStream skip(long n) returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream. If this stream contains fewer than n elements then an empty stream will be returned. Note : LongStream skip() is a stateful intermediate operation i.e, it may incorporate state from previously seen elemen
2 min read
LongStream sorted() in Java
LongStream sorted() returns a stream consisting of the elements of this stream in sorted order. It is a stateful intermediate operation i.e, it may incorporate state from previously seen elements when processing new elements. Stateful intermediate operations may need to process the entire input before producing a result. For example, one cannot pro
2 min read
LongStream summaryStatistics() in Java
LongStream summaryStatistics() returns an LongSummaryStatistics describing various summary data about the elements of this stream like count of number of elements in the LongStream, average of all elements present in LongStream, minimum and maximum element in the LongStream and so on. This is a terminal operation i.e, it may traverse the stream to
2 min read
LongStream average() in Java with Examples
java.util.stream.LongStream in Java 8, deals with primitive Longs. It helps to solve the old problems like finding maximum value in array, finding minimum value in array, sum of all elements in array, and average of all values in array in a new way. LongStream average() returns an OptionalDouble describing the arithmetic mean of elements of this st
2 min read
Article Tags :
Practice Tags :