Monday, November 18, 2024
Google search engine
HomeLanguagesJavaJava.util.IntSummaryStatistics class with Examples

Java.util.IntSummaryStatistics class with Examples

The IntSummaryStatistics class is present in java.util package. It takes a collection of Integer objects and is useful in the circumstances when we are dealing with a stream of integers. It maintains a count of the number of integers 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 integers and hence can be used in the manipulation of statistical data.

Class Hierarchy

java.lang.Object
↳ java.util.IntSummaryStatistics

Constructors

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

    Syntax:

    public IntSummaryStatistics()
    
  2. IntSummaryStatistics(count, min, max, sum): Initializes the various data members with the parameters passed during invocation.

    Syntax:

    public IntSummaryStatistics(long count, int min, int max, long sum)
                         throws IllegalArgumentException
    

Methods:

  1. accept() – This function adds the passed integer into the statistical data.

    Syntax:

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

    Syntax:

    public void combine(IntSummaryStatistics other)
    
  3. getCount(): This method returns the count of the number of integers processed.

    Syntax:

    public final long getCount()
    
  4. getSum(): This method returns the sum of all the integers processed.

    Syntax:

    public final long getSum()
    
  5. getAverage(): This method returns the average of all the integers processed.

    Syntax:

    public final double getAverage()
    
  6. getMin(): This method returns the minimum integer of all the integers processed.

    Syntax:

    public final int getMin()
    
  7. getMax(): This method returns the maximum integer of all the integers processed.

    Syntax:

    public final int getMax()
    
  8. toString(): This method returns the string representation of all the statistical data contained in the object.

    Syntax:

    public String toString()
    
  9. Example To demonstrate IntSummaryStatistics in action.




    // Java program to demonstrate
    // IntSummaryStatistics class
      
    import java.util.*;
      
    public class IntSummaryStatisticsDemo {
        public static void main(String[] args)
        {
      
            IntSummaryStatistics intSummaryStatistics
                = new IntSummaryStatistics();
      
            List<Integer> list
                = Arrays.asList(10, 20, 30, 40, 50);
      
            Iterator<Integer> iterator = list.listIterator();
            while (iterator.hasNext()) {
                // Add the integers to the IntSummaryStatistics object
                intSummaryStatistics.accept(iterator.next());
            }
      
            // Use various methods to obtain the data
            System.out.println("The count of values is "
                               + intSummaryStatistics.getCount());
            System.out.println("The average of values is "
                               + intSummaryStatistics.getAverage());
            System.out.println("The sum of values is "
                               + intSummaryStatistics.getSum());
            System.out.println("The maximum of values is "
                               + intSummaryStatistics.getMax());
            System.out.println("The minimum of values is "
                               + intSummaryStatistics.getMin());
            System.out.println("The string representation is");
            System.out.println(intSummaryStatistics.toString());
        }
    }

    
    
    Output:

    The count of values is 5
    The average of values is 30.0
    The sum of values is 150
    The maximum of values is 50
    The minimum of values is 10
    The string representation is
    IntSummaryStatistics{count=5, sum=150, min=10, average=30.000000, max=50}
    

    Reference: https://docs.oracle.com/javase/10/docs/api/java/util/IntSummaryStatistics.html

RELATED ARTICLES

Most Popular

Recent Comments