Friday, August 2, 2019

Java 8 DoubleSummaryStatistics API - Working Examples

1. Overview


In this tutorial, We'll guide to DoubleSummaryStatistics class which added in Java 8 API. This is introduced in java.util package.

DoubleSummaryStatistics

DoubleSummaryStatistics class is designed to get statistics of a for a stream such as count, min, max, sum, and average operations for double values or DoubleStream.


We will see the syntax and its useful methods. But this will work for only Double values. But, java 8 introduced IntSummaryStatistics for integer values and LongSummaryStatistics for long values.

And also this implements DoubleConsumer.

public class DoubleSummaryStatistics extends Object implements DoubleConsumer

2. Methods


Below are all methods of DoubleSummaryStatistics.

accept(double value): Records another value into the summary information.
combine(DoubleSummaryStatistics other): Combines the state of another DoubleSummaryStatistics into this one.
getAverage(): Returns the arithmetic mean of values recorded, or zero if no values have been recorded.
getCount(): Return the count of values recorded.
getMax(): Returns the maximum recorded value, Double.NaN if any recorded value was NaN or Double.NEGATIVE_INFINITY if no values were recorded.
getMin(): Returns the minimum recorded value, Double.NaN if any recorded value was NaN or Double.POSITIVE_INFINITY if no values were recorded.
getSum(): Returns the sum of values recorded, or zero if no values have been recorded.
toString(): Returns a non-empty string representation of this object suitable for debugging.

3. Examples


Creating an Employee class with setters and getters.

class Employee {
 private int id;
 private String name;
 private double sal;
 private int age;
 private double noOfDaysIn;

 public Employee(int id, String name, double sal, int age, double noOfDaysIn) {
  this.id = id;
  this.name = name;
  this.sal = sal;
  this.age = age;
  this.noOfDaysIn = noOfDaysIn;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public double getSal() {
  return sal;
 }

 public void setSal(double sal) {
  this.sal = sal;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public double getNoOfDaysIn() {
  return noOfDaysIn;
 }

 public void setNoOfDaysIn(double noOfDaysIn) {
  this.noOfDaysIn = noOfDaysIn;
 }

}

Generating employee Salary and noOfDaysIn statistics using Collectors.summarizingDouble() method.


package com.java.w3schools.blog.java8.statistics;

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

/**
 * 
 * Java 8 DoubleSummaryStatistics API - Working Examples
 * 
 * @author venkatesh
 *
 */
public class DoubleSummaryStatisticsExample {

 public static void main(String[] args) {
  Employee[] employees = new Employee[] { new Employee(100, "Paul", 10000, 20, 365),
    new Employee(200, "Amul", 20000, 30, 600), new Employee(300, "Aliy", 30000, 45, 1000) };

  List empList = Arrays.asList(employees);
  DoubleSummaryStatistics salaryStatistics = empList.stream()
    .collect(Collectors.summarizingDouble(Employee::getSal));
  System.out.println("Empoyee salary statistics : " + salaryStatistics);

  DoubleSummaryStatistics noOfdaysInStatistics = empList.stream()
    .collect(Collectors.summarizingDouble(Employee::getNoOfDaysIn));
  System.out.println("Empoyee no. of days statistics : " + noOfdaysInStatistics);

 }

Collectors.summarizingDouble(Employee::getSal) collects the all double values of salary and generate the statistics for salary. Finally returns DoubleSummaryStatistics instance.

Output:

Empoyee salary statistics : DoubleSummaryStatistics{count=3, sum=60000.000000, min=10000.000000, average=20000.000000, max=30000.000000}
Empoyee no. of days statistics : DoubleSummaryStatistics{count=3, sum=1965.000000, min=365.000000, average=655.000000, max=1000.000000}

It is calculating all the operations for us internally and showing count, sum, min, max, average values.

4. Object creation using Constructor

We can create an object for DoubleSummaryStatistics manually like below. But this kind of code is not useful because we need to provide count, sum, min and max values.

Syntax:

DoubleSummaryStatistics(long count, double min, double max, double sum)

Example object creation using constructor:

DoubleSummaryStatistics doubleSummaryStatistics = new DoubleSummaryStatistics(100, 100000, 500000, 100000000);
System.out.println("doubleSummaryStatistics count : " + doubleSummaryStatistics.getCount());
System.out.println("doubleSummaryStatistics average : " + doubleSummaryStatistics.getAverage());
System.out.println("doubleSummaryStatistics max : " + doubleSummaryStatistics.getMax());
System.out.println("doubleSummaryStatistics min : " + doubleSummaryStatistics.getMin());
System.out.println("doubleSummaryStatistics sum : " + doubleSummaryStatistics.getSum());

If we are using stream API then it would be easier to gather statistics using Collectors API.

Output:

doubleSummaryStatistics count : 100
doubleSummaryStatistics average : 1000000.0
doubleSummaryStatistics max : 500000.0
doubleSummaryStatistics min : 100000.0
doubleSummaryStatistics sum : 1.0E8

5. Conclusion


In this article, We've covered new java 8 DoubleSummaryStatistics and its usage.

Using Stream API and Collectors API makes relly very cool and reduces the additional work to accumulate the statistics in one pass.

This can be also used with DoubleStream with double values.

Example codes shown are over GitHub.
API Ref

No comments:

Post a Comment