# Java 8 Summary Statistics Example

By Arvind Rai, September 06, 2016
On this page we will provide java 8 summary statistics example. We can calculate all statistical information such as count, min, max, sum, and average in one go. Java 8 provides `DoubleSummaryStatistics` for double data type, `IntSummaryStatistics` for integer data type and `LongSummaryStatistics` for long data type. These classes work with stream of elements.

### Summary Statistics Methods

Find the summary statistics methods. These methods belong to the java 8 classes of summary statistics.
getAverage(): It returns the average of all accepted value.
getCount(): It calculates the count of all element.
getMax(): It returns the maximum value.
getMin(): It returns the minimum value.
getSum(): It returns the sum of all elements.
accept(): It adds the element into the summary information.
combine(): It combines two summary statistics.

### DoubleSummaryStatistics and Collectors.summarizingDouble()

`DoubleSummaryStatistics` collects the statistical information for the stream of `double` data type. `Collectors.summarizingDouble()` accepts the `double` mapping function and returns the `Collector` of double summary statistics.
```DoubleSummaryStatistics dstats = list.stream()
.collect(Collectors.summarizingDouble(Rectangle::getWidth)); ```

### IntSummaryStatistics and Collectors.summarizingInt()

`IntSummaryStatistics ` collects the statistical information for the stream of `int` data type. `Collectors.summarizingInt()` accepts the `int` mapping function and returns the `Collector` of integer summary statistics.
```IntSummaryStatistics istats = list.stream()
.collect(Collectors.summarizingInt(Rectangle::getLength)); ```

### LongSummaryStatistics and Collectors.summarizingLong()

`LongSummaryStatistics` collects the statistical information for the stream of `long` data type. `Collectors.summarizingLong()` accepts the `long` mapping function and returns the `Collector` of `long` summary statistics.
```LongSummaryStatistics lstats = list.stream().
collect(Collectors.summarizingLong(Rectangle::getId)); ```

### Summary Statistics Example with Primitive Data Type

SummaryStatisticsWithPrimitiveDataType.java
```package com.concretepage;
import java.util.DoubleSummaryStatistics;
import java.util.IntSummaryStatistics;
import java.util.LongSummaryStatistics;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class SummaryStatisticsWithPrimitiveDataType {
public static void main(String[] args) {
System.out.println("---DoubleSummaryStatistics---");
DoubleSummaryStatistics dstats = DoubleStream.of(5.33d,2.34d,5.32d,2.31d,3.51d).
collect(DoubleSummaryStatistics::new, DoubleSummaryStatistics::accept,
DoubleSummaryStatistics::combine);
System.out.println("Max:"+dstats.getMax()+", Min:"+dstats.getMin());
System.out.println("Count:"+dstats.getCount()+", Sum:"+dstats.getSum());
System.out.println("Average:"+dstats.getAverage());

System.out.println("---LongSummaryStatistics---");
LongSummaryStatistics lstats = LongStream.of(51l,23l,53l,23l,35l).
collect(LongSummaryStatistics::new, LongSummaryStatistics::accept,
LongSummaryStatistics::combine);
System.out.println("Max:"+lstats.getMax()+", Min:"+lstats.getMin());
System.out.println("Count:"+lstats.getCount()+", Sum:"+lstats.getSum());
System.out.println("Average:"+lstats.getAverage());

System.out.println("---IntSummaryStatistics---");
IntSummaryStatistics istats = IntStream.of(51,22,50,27,35).
collect(IntSummaryStatistics::new, IntSummaryStatistics::accept,
IntSummaryStatistics::combine);
System.out.println("Max:"+istats.getMax()+", Min:"+istats.getMin());
System.out.println("Count:"+istats.getCount()+", Sum:"+istats.getSum());
System.out.println("Average:"+istats.getAverage());
}
} ```
Output
```---DoubleSummaryStatistics---
Max:5.33, Min:2.31
Count:5, Sum:18.81
Average:3.7619999999999996
---LongSummaryStatistics---
Max:53, Min:23
Count:5, Sum:185
Average:37.0
---IntSummaryStatistics---
Max:51, Min:22
Count:5, Sum:185
Average:37.0 ```

### Summary Statistics Example with Objects

SummaryStatisticsDemoWithObject.java
```package com.concretepage;
import java.util.DoubleSummaryStatistics;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.stream.Collectors;
public class SummaryStatisticsDemoWithObject {
public static void main(String[] args) {
System.out.println("--DoubleSummaryStatistics--");
List<Rectangle> list = Rectangle.getRectangle();
DoubleSummaryStatistics dstats = list.stream()
.collect(Collectors.summarizingDouble(Rectangle::getWidth));
System.out.println("Max:"+dstats.getMax()+", Min:"+dstats.getMin());
System.out.println("Count:"+dstats.getCount()+", Sum:"+dstats.getSum());
System.out.println("Average:"+dstats.getAverage());

System.out.println("--IntSummaryStatistics--");
list = Rectangle.getRectangle();
IntSummaryStatistics istats = list.stream()
.collect(Collectors.summarizingInt(Rectangle::getLength));
System.out.println("Max:"+istats.getMax()+", Min:"+istats.getMin());
System.out.println("Count:"+istats.getCount()+", Sum:"+istats.getSum());
System.out.println("Average:"+istats.getAverage());

System.out.println("--LongSummaryStatistics--");
list = Rectangle.getRectangle();
LongSummaryStatistics lstats = list.stream().
collect(Collectors.summarizingLong(Rectangle::getId));
System.out.println("Max:"+lstats.getMax()+", Min:"+lstats.getMin());
System.out.println("Count:"+lstats.getCount()+", Sum:"+lstats.getSum());
System.out.println("Average:"+lstats.getAverage());
}
} ```
Rectangle.java
```package com.concretepage;
import java.util.ArrayList;
import java.util.List;
public class Rectangle {
private long id;
private int length;
private double width;
public Rectangle(long id, int length, double width) {
this.id = id;
this.length = length;
this.width = width;
}
public long getId() {
return id;
}
public int getLength() {
return length;
}
public double getWidth() {
return width;
}
public static List<Rectangle> getRectangle() {
List<Rectangle> list = new ArrayList<>();
return list;
}
} ```
Output
```--DoubleSummaryStatistics--
Max:154.12, Min:114.23
Count:4, Sum:547.01
Average:136.7525
--IntSummaryStatistics--
Max:253, Min:213
Count:4, Sum:942
Average:235.5
--LongSummaryStatistics--
Max:400, Min:100
Count:4, Sum:1000
Average:250.0 ```
POSTED BY
ARVIND RAI