Java8 Summary Statistics Primitive Numeric Data Examples

In this blog post, Learn the fundamentals of primitive Summary Statistics classes in Java 8.

Introduction to Java 8 Summary Statistics

Summary Statistics involve the calculation of statistical data such as count, sum, max, min, and average operations on numeric data.

Before Java 8, developers typically followed a series of steps to obtain Summary Statistics:

  • Iterate or loop through numeric data.
  • save each element into a temporary variable to calculate the sum.
  • Calculate the average by dividing the sum by the number of elements.

This process required manual handling by developers or the use of external libraries such as Apache Commons.

Calculating Sum and Average in Java 7

For exampleobtaining the sum and average of an array of numbers, We have to write a code as follows.

import java.util.IntSummaryStatistics;
import java.util.stream.IntStream;

public class Java7SummaryStats {

    public static void main(String[] args) {
        double numberValues[] = { 10, 5, 5 };
        double sumOfNumbers = 0;
        for (double value : numberValues) {
            sumOfNumbers += value;
        }
        Double average =sumOfNumbers / numberValues.length;
        System.out.println("Sum:" + sumOfNumbers);
        System.out.println("Average:" + average);

    }
}

output:

Sum:20.0
Average:6.666666666666667

Java 8: Sum and Average Using Streams

Java 8 introduced a more streamlined approach to calculate the sum and average using streams, as depicted in the following example:

Java 8 introduced a more streamlined approach to calculate the sum and average using streams, as depicted in the following example:

Created Array of streams, pass this stream each element to mapToDouble() function which output double value and call terminal operation sum and the average.


import java.util.Arrays;
import java.util.OptionalDouble;

public class summaryStats {

    public static void main(String[] args) {
        Double numberValues[] = { 10d, 5d, 5d };
        OptionalDouble averageOptionalDouble = Arrays.stream(numberValues)
                .mapToDouble(Double::doubleValue).average();
        double sumOfNumbers = Arrays.stream(numberValues).mapToDouble(Double::doubleValue)
                .sum();
        System.out.println("Sum:" + sumOfNumbers);
        System.out.println("Average:" + averageOptionalDouble.getAsDouble());
    }
}

output is

Sum:20.0
Average:6.666666666666667

Primitive Numeric Summary Statistics Examples in Java 8

Java 8 introduced three classes in the java.util package to simplify obtaining summary statistics for numeric primitive types:

  • IntSummaryStatistics: for Integer data type.
  • LongSummaryStatistics: for Long data type.
  • DoubleSummaryStatistics: for Double data type.

These classes leverage Java 8 features such as streams and lambda expressions, providing a concise and efficient solution for statistical operations on numeric data.

With Summary Statistics classes, the code becomes more concise, requiring fewer lines of code to achieve the same results. The DoubleSummaryStatistics class, for instance, is specifically designed to calculate statistics operations for double data types, making use of Java 8 streams.

  • First, create an array of streams,
    • This stream of data is passed to the collect method by passing Collectors.summarizingDouble and returns DoubleSummaryStatistics object. This object contains all the operations result wrapped with it.

The below code is another way to calculate the sum and average of a double array.


import java.util.Arrays;
import java.util.DoubleSummaryStatistics;
import java.util.stream.Collectors;

public class DoubleSummaryStatisticsExample {

    public static void main(String[] args) {
        Double numberValues[] = { 10d, 5d, 5d };

        DoubleSummaryStatistics stats = Arrays.stream(numberValues).collect(
                Collectors.summarizingDouble(Double::doubleValue));
        System.out.println(stats.getSum());
        System.out.println(stats.getAverage());
        System.out.println(stats);

    }

}

Output:

Sum:20.0
20.0
6.666666666666667
DoubleSummaryStatistics{count=3, sum=20.000000, min=5.000000, average=6.666667, max=10.000000}

Conclusion

In conclusion, this blog post has provided insights into creating Summary Statistics objects for primitive types in Java 8 and demonstrated their usage with examples. These Summary Statistics classes offer a convenient and efficient way to perform statistical calculations on numeric data, leveraging the power of Java 8 features.