Java.util.IntSummaryStatistics class with Examples - GeeksforGeeks

24 Jun, 2019
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
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()
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

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

Syntax:
public IntSummaryStatistics()

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

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

Methods:
accept() – This function adds the passed integer into the statistical data.Syntax:public void accept(int value)
combine(): This function combines the statistical data of the passed IntSummaryStatistics object with the current statistical data.Syntax:public void combine(IntSummaryStatistics other)
getCount(): This method returns the count of the number of integers processed.Syntax:public final long getCount()
getSum(): This method returns the sum of all the integers processed.Syntax:public final long getSum()
getAverage(): This method returns the average of all the integers processed.Syntax:public final double getAverage()
getMin(): This method returns the minimum integer of all the integers processed.Syntax:public final int getMin()
getMax(): This method returns the maximum integer of all the integers processed.Syntax:public final int getMax()
toString(): This method returns the string representation of all the statistical data contained in the object.Syntax:public String toString()
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.htmlMy Personal Notes
arrow_drop_upSave
accept() – This function adds the passed integer into the statistical data.Syntax:public void accept(int value)

Syntax:
public void accept(int value)

combine(): This function combines the statistical data of the passed IntSummaryStatistics object with the current statistical data.Syntax:public void combine(IntSummaryStatistics other)

Syntax:
public void combine(IntSummaryStatistics other)

getCount(): This method returns the count of the number of integers processed.Syntax:public final long getCount()

Syntax:
public final long getCount()

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

Syntax:
public final long getSum()

getAverage(): This method returns the average of all the integers processed.Syntax:public final double getAverage()

Syntax:
public final double getAverage()

getMin(): This method returns the minimum integer of all the integers processed.Syntax:public final int getMin()

Syntax:
public final int getMin()

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

Syntax:
public final int getMax()

toString(): This method returns the string representation of all the statistical data contained in the object.Syntax:public String toString()
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.htmlMy Personal Notes
arrow_drop_upSave
Syntax:
public String toString()

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());    }}
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
Java - util package
Java-Classes
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Different ways of Reading a text file in Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Comparator Interface in Java with Examples
HashMap get() Method in Java
Introduction to Java
Difference between Abstract Class and Interface in Java