Skip to content
Related Articles
Get the best out of our app
Open App

Related Articles

Java.util.LongSummaryStatistics class with Examples

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The LongSummaryStatistics class is present in java.util package. It takes a collection of Long objects and is useful in the circumstances when we are dealing with a stream of large integers. It maintains a count of the number of values it has processed, their sum and various other statistics. The class can also be used with Streams.

It is useful in the sense that it maintains a running sum, average, etc. of the values and hence can be used in the manipulation of statistical data.

Class Hierarchy

↳ java.util.LongSummaryStatistics


  1. LongSummaryStatistics(): A default constructor which initializes the count and sum to zero, and sets max to Long.MIN_VALUE and min to Long.MAX_VALUE.

    public LongSummaryStatistics()


  1. accept(): The function is overloaded and it adds the passed integer or long value into the statistical data.

    public void accept(int value)
    public void accept(long value)
  2. combine(): The function combines the statistical data of the passed LongSummaryStatistics object with the current statistical data.

    public void combine(LongSummaryStatistics other)
  3. getCount(): Returns the count of the number of long values processed.

    public final long getCount()
  4. getSum(): Returns the sum of all the long values processed.

    public final long getSum()
  5. getAverage(): Returns the average of all the long values processed.

    public final double getAverage()
  6. getMin() : Returns the minimum integer of all the long values processed.

    public final long getMin()
  7. getMax(): Returns the maximum integer of all the long values processed.

    public final long getMax()
  8. toString(): Returns the string representation of all the statistical data contained in the object.

    public String toString()
  9. Example to demonstrate LongSummaryStatistics in action.

    // Java program to demonstrate
    // LongSummaryStatistics class
    import java.util.*;
    public class LongSummaryStatisticsDemo {
        public static void main(String[] args)
            LongSummaryStatistics longSummaryStatistics
                = new LongSummaryStatistics();
            List<Long> list = new LinkedList<>();
            Iterator<Long> iterator = list.listIterator();
            while (iterator.hasNext()) {
                // Add the values to the LongSummaryStatistics object
            // Use various methods to obtain the data
            System.out.println("The count of values is "
                               + longSummaryStatistics.getCount());
            System.out.println("The average of values is "
                               + longSummaryStatistics.getAverage());
            System.out.println("The sum of values is "
                               + longSummaryStatistics.getSum());
            System.out.println("The maximum of values is "
                               + longSummaryStatistics.getMax());
            System.out.println("The minimum of values is "
                               + longSummaryStatistics.getMin());
            System.out.println("The string representation is ");



    The count of values is 10
    The average of values is 550.0
    The sum of values is 5500
    The maximum of values is 1000
    The minimum of values is 100
    The string representation is 
    LongSummaryStatistics{count=10, sum=5500, min=100, average=550.000000, max=1000}


    My Personal Notes arrow_drop_up
Last Updated : 27 Jun, 2019
Like Article
Save Article
Similar Reads
Related Tutorials