Java8 Summary Statistics Primitive Numeric Data Examples

In this blog post, We are going to cover the basics of primitive Summary Statistics classes in java8.

java8 Summary Statistics Introduction

Summary Statistics is to calculate Statistical data such as count, sum, max, min, and average operations on numeric data.

Before java8, here are steps to get Summary Statistics of the sum and average operations.

  • First Numeric data might be iterated or loop
  • Add each element to a temporary variable to sum it up
  • average would be returned by dividing the sum by the number of elements

It needs to be handled by the developer. You can also use apache libraries to have the same functionalities.

How to calculate the sum and average of numbers in Java 7?

For example for finding/ calculating the sum and average of the 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);




Java8 sum and average using stream example

This is an example for calculating the sum and average of an array of data using streams without summary statics classes 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)
        double sumOfNumbers = Arrays.stream(numberValues).mapToDouble(Double::doubleValue)
        System.out.println("Sum:" + sumOfNumbers);
        System.out.println("Average:" + averageOptionalDouble.getAsDouble());

output is


Primitive Numeric Summary Statistics Examples

java8 has introduced three classes in the Java.util package to get summary statics. It is simplified by introducing new classes for calculating summary statistics of numeric primitive types.

These classes are designed to operate with new java8 features - streams and lambda expressions. These implementation classes do not thread-safe.

With Summary Statistics classes, the same code can be simplified with fewer lines of code. DoubleSummaryStatistics class is to calculate statistics operations for double data. This works with Streams of java8.

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




DoubleSummaryStatistics{count=3, sum=20.000000, min=5.000000, average=6.666667, max=10.000000}  


Learned Summary Statics primitive types object creation and methods usage with example

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.