Java 8 Summary Statistics

By Yashwant Chavan, Views 6152, Date 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> ();
        persons.add(new Person(1, "Divya", 24, Gender.FEMALE));
        persons.add(new Person(2, "Mahesh", 22, Gender.MALE));
        persons.add(new Person(3, "Dinesh", 32, Gender.MALE));
        persons.add(new Person(4, "Vinay", 25, Gender.MALE));
        persons.add(new Person(5, "Kapil", 29, Gender.MALE));
        persons.add(new Person(6, "Pallavi", 23, Gender.FEMALE));
        persons.add(new Person(7, "Mayuri", 21, Gender.FEMALE));
        persons.add(new Person(8, "Yashwant", 30, Gender.MALE));

        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 Chavan

Yashwant Chavan

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@gmail.com