Java.util.LongSummaryStatistics class with Examples

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.lang.Object
↳ java.util.LongSummaryStatistics

Constructors

  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()
    

Methods:

  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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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<>();
            list.add(100l);
            list.add(200l);
            list.add(300l);
            list.add(400l);
            list.add(500l);
            list.add(600l);
            list.add(700l);
            list.add(800l);
            list.add(900l);
            list.add(1000l);
      
            Iterator<Long> iterator = list.listIterator();
            while (iterator.hasNext()) {
                // Add the values to the LongSummaryStatistics object
                longSummaryStatistics.accept(iterator.next());
            }
      
            // 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 ");
            System.out.println(longSummaryStatistics.toString());
        }
    }

    chevron_right

    
    

    Output:

    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}
    

    Reference: https://docs.oracle.com/javase/9/docs/api/java/util/LongSummaryStatistics.html



    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.