IntSummaryStatistics Class in Java

The IntSummaryStatistics class is one of the most important classes present in the java.util.package. It provides a set of integer objects which are used at the time of dealing with a stream of integers. It keeps the count of the number of integers processed, their sum and various other statistics. We can also use these classes with Streams. The class is mainly used in the manipulation of statistical data.

java.lang.Object → java.util.IntSummaryStatistics

Constructors

The IntSummaryStatistics class has following two constructors:

1. IntSummaryStatistics()

It is a default constructor that will initialize the sum and count to zero and set min to Integer.MIN_VALUE and max to Integer.MAX_VALUE.

Syntax

2. IntSummaryStatistics(count, min, max, sum)

It is a parameterized constructor that will initialize the sum, count, min and max with the given parameters.

Syntax

IntSummaryStatistics Class Methods

The IntSummaryStatistics class has more than one method for performing the manipulation of statistical data. Let's understand each one of them one by one with an example.

accept(int value) Method

The accept() method is an important method provided by the IntSummaryStatistics class that is used for accepting the given value into this summary information.

Syntax

Parameter

It accepts an integer value as a parameter that is to be recorded into this IntSummaryStatistics.

Return value

NA.

Let's take an example to understand the use of the accept() method.

IntSummaryStatisticsExample1.java

Output:

IntSummaryStatistics Class in Java

combine(IntSummaryStatistics other) Method

The combine() method is another important method provided by the IntSummaryStatistics class that is used for combining two IntSummaryStatistics objects.

Syntax

Parameter

It accepts an object of IntSummaryStatistics, which is going to combine with the current statistical data.

Return value

NA.

Let's take an example to understand the use of the combine() method.

IntSummaryStatisticsExample2.java

Output:

IntSummaryStatistics Class in Java

getCount() Method

The getCount() method is another important method provided by the IntSummaryStatistics class that is used to get the count of the number of integers processed.

Syntax

Parameter

NA.

Return value

It returns the count of the numbers of integers processed.

Let's take an example to understand the use of the getCount() method.

IntSummaryStatisticsExample3.java

Output:

IntSummaryStatistics Class in Java

getSum() Method

The getSum() method is another important method provided by the IntSummaryStatistics class that is used to get the sum of all integers processed.

Syntax

Parameter

NA.

Return value

It returns the sum of all integers processed.

Let's take an example to understand the use of the getSum() method.

IntSummaryStatisticsExample4.java

Output:

IntSummaryStatistics Class in Java

getAverage() Method

The getSum() method is another important method provided by the IntSummaryStatistics class that is used to get the average of all integers processed.

Syntax

Parameter

NA.

Return value

It returns the average of all integers processed.

Let's take an example to understand the use of the getAverage() method.

IntSummaryStatisticsExample5.java

Output:

IntSummaryStatistics Class in Java

getMin() Method

The getMin() method is another important method provided by the IntSummaryStatistics class that is used to get the minimum Integer of all integers processed.

Syntax

Parameter

NA.

Return value

It returns the minimum Integer of all integers processed.

Let's take an example to understand the use of the getMin() method.

IntSummaryStatisticsExample6.java

Output:

IntSummaryStatistics Class in Java

getMax() Method

The getMax() method is another important method provided by the IntSummaryStatistics class that is used to get the maximum Integer of all integers processed.

Syntax

public final int getMax()

Parameter

NA.

Return value

It returns the maximum Integer of all integers processed.

Let's take an example to understand the use of the getMax() method.

IntSummaryStatisticsExample7.java

Output:

IntSummaryStatistics Class in Java

toString() Method

The toString() method is used to get the string representation of all statistical data contained in the project.

Syntax

Parameter

NA.

Return value

It returns the string representation of the IntSummaryStatistics object.

Let's take an example to understand the use of the toString() method.

IntSummaryStatisticsExample8.java

Output:

IntSummaryStatistics Class in Java




Latest Courses