Friday, August 24, 2018

Java8 Summary Statistics Introduction with 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 a 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 sum with a number of elements
This need to be handled by the developer. You can also use apache libraries to have the same functionalities.

Java 7 Summary Statistics example

For example for finding/ calculating sum and average of the array of numbers, We have to write a code as follows.

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 is
Sum:20.0
Average:6.666666666666667

Java8 sum and average using stream example

 This is an example for calculating 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
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

 java8 has introduced three classes in Java.util package to get summary statics With java8, It is simplified by introduced new classes for calculating summary statistics of numeric primitive types.
IntSummaryStatistics class - for Integer data type.
LongSummaryStatistics class- for Long data type
DoubleSummaryStatistics class - for Double data type.

These classes are designed to operate on 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. This works with Streams of java8.

  • First, create an array of streams, 
  • This stream of data is passed to 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 sum and average of a double array
DoubleSummaryStatistics stats = Arrays.stream(numberValues).collect(
Collectors.summarizingDouble(Double::doubleValue));
System.out.println(stats.getSum());
System.out.println(stats.getAverage());
System.out.println(stats);
Sum:20.0
20.0
6.666666666666667
DoubleSummaryStatistics{count=3, sum=20.000000, min=5.000000, average=6.666667, max=10.000000}

Related article


EmoticonEmoticon