Java 8 Summary Statistics

By Yashwant Chavan, Views 6213, Last updated on 06-Nov-2016

In this tutorial you will learn about Java 8 Summary Statistics classes LongSummaryStatistics, IntSummaryStatistics and DoubleSummaryStatistics, It will help you to get count, min, max, sum, and average values.

tags java

Refer below steps.

Java 8 provides summary statistics classes for Double, Long, and Integer primitive data types. The java.util package contains three classes to summarizing data and return a summary statistics object to get count, min, max, sum, and average value.

• DoubleSummaryStatistics
• LongSummaryStatistics
• IntSummaryStatistics

In first example we will use LongSummaryStatistics to find out the total count, min, max, sum, and average values.

In second we will use IntSummaryStatistics for summarizing person data base on age criteria to get the details such as Total number of Persons, Sum Of Age, Min Age, Max Age, and Average Age.

Person Pojo

Create simple Person pojo class with different attributes such as personId, gender, name,age and gender.

```package com.technicalkeeda.app;

public class Person {
private int personId;
private String name;
private int age;
private Gender gender;

public Person(int personId, String name, int age, Gender gender) {
super();
this.personId = personId;
this.name = name;
this.age = age;
this.gender = gender;
}

public boolean isMale() {
return this.gender == Gender.MALE;
}

public boolean isFemale() {
return this.gender == Gender.FEMALE;
}

public int getPersonId() {
return personId;
}

public void setPersonId(int personId) {
this.personId = personId;
}

public String getName() {
return name;
}

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

public int getAge() {
return age;
}

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

public Gender getGender() {
return gender;
}

public void setGender(Gender gender) {
this.gender = gender;
}

@Override
public String toString() {
StringBuilder str = null;
str = new StringBuilder();
str.append("Person Id:- " + getPersonId() + " Gender:- " + getGender() + " Name:- " + getName() + " Age:- " +
getAge());
return str.toString();
}

public static enum Gender {
MALE,
FEMALE
}

}
```

Java 8 Summary Statistics Examples

```package com.technicalkeeda.app;

import java.util.ArrayList;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.stream.Collectors;

import com.technicalkeeda.app.Person.Gender;

public class Java8SummaryStatistics {
public static void main(String[] args) {

LongSummaryStatistics stats = new LongSummaryStatistics();
stats.accept(1);
stats.accept(2);
stats.accept(3);
stats.accept(4);
stats.accept(5);

System.out.println("Count:- " + stats.getCount());
System.out.println("Sum:- " + stats.getSum());
System.out.println("Min:- " + stats.getMin());
System.out.println("Max:- " + stats.getMax());
System.out.println("Average:- " + stats.getAverage());

List <Person> persons = new ArrayList <Person> ();

IntSummaryStatistics summaryByAge = persons.stream().collect(Collectors.summarizingInt(p -> p.getAge()));

System.out.println();

System.out.println("Total number of Persons:- " + summaryByAge.getCount());
System.out.println("Sum Of Age:- " + summaryByAge.getSum());
System.out.println("Min Age:- " + summaryByAge.getMin());
System.out.println("Max Age:- " + summaryByAge.getMax());
System.out.println("Average Age:- " + summaryByAge.getAverage());

}
}
```

output

```Count:- 5
Sum:- 15
Min:- 1
Max:- 5
Average:- 3.0

Total number of Persons:- 8
Sum Of Age:- 206
Min Age:- 21
Max Age:- 32
Average Age:- 25.75
```

Yashwant

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][gmail.com]