Open In App

LongStream summaryStatistics() in Java

Improve
Improve
Like Article
Like
Save
Share
Report

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 produce a result or a side-effect.

Syntax :

LongSummaryStatistics summaryStatistics()

Parameters :

  1. LongSummaryStatistics : A state object for collecting statistics such as count, min, max, sum, and average.

Return Value : LongSummaryStatistics summaryStatistics() returns an LongSummaryStatistics describing various summary data about the elements of this stream.

Note : LongStream summaryStatistics() is a special case of a reduction. A reduction operation, also known as fold takes a sequence of input elements and combines them into a single summary result by repeated application of a combining operation. The combining operation can be finding the sum or maximum of a set of numbers.

Example 1 : Using LongStream summaryStatistics() to get the LongSummaryStatistics of elements present in given LongStream.




// Java code for LongStream summaryStatistics()
// to get various summary data about the
// elements of the stream.
import java.util.stream.LongStream;
import java.util.LongSummaryStatistics;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an LongStream
        LongStream stream = LongStream.of(4L, 5L, 6L, 7L);
  
        // Using LongStream summaryStatistics()
        LongSummaryStatistics summary_data =
                                  stream.summaryStatistics();
  
        // Displaying the various summary data
        // about the elements of the stream
        System.out.println(summary_data);
    }
}


Output :

LongSummaryStatistics{count=4, sum=22, min=4, average=5.500000, max=7}

Example 2 :Using LongStream summaryStatistics() to get the LongSummaryStatistics of elements present in given range.




// Java code for LongStream summaryStatistics()
// to get various summary data about the
// elements of the stream.
import java.util.stream.LongStream;
import java.util.LongSummaryStatistics;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an LongStream of elements
        // in range [5, 9)
        LongStream stream = LongStream.range(5L, 9L);
  
        // Using LongStream summaryStatistics()
        LongSummaryStatistics summary_data = 
                         stream.summaryStatistics();
  
        // Displaying the various summary data
        // about the elements of the stream
        System.out.println(summary_data);
    }
}


Output :

LongSummaryStatistics{count=4, sum=26, min=5, average=6.500000, max=8


Last Updated : 06 Dec, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads