January 13, 2024

Summarizing Collectors in Java Stream

In this tutorial you’ll learn about the different summarizing collectors in Java Stream API that returns a summary statistics Collectors.

Summarizing collectors in Collectors class

There are three summarizing collector methods in Collectors class which are as following-

  • summarizingInt(ToIntFunction<? super T> mapper)- Returns a Collector which applies an int-producing mapping function to each input element.
  • summarizingLong(ToLongFunction<? super T> mapper)- Returns a Collector which applies an long-producing mapping function to each input element.
  • summarizingDouble(ToDoubleFunction<? super T> mapper)- Returns a Collector which applies an double-producing mapping function to each input element.

Argument to these methods is a corresponding functional interface ToIntFunction, ToLongFunction and ToDoubleFunction that represents a function that produces respectively a int, long or double valued result.

Return type of the method used is also as per type and the Collectors returned are of type IntSummaryStatistics, LongSummaryStatistics and DoubleSummaryStatistics.

Summary statistics object that you can get by using these methods encapsulates attributes like-

  • count
  • min
  • max
  • sum
  • average

These summarizing collectors are great utility methods to compute all the above values with in a single pass.

Collectors.summarizingInt() Java example

For the examples we’ll use the Person class objects.

public class Person {
  private String name;
  private int dependents;
  private long age;
  private double weight;
  Person(String name, int dependents, long age, double weight){
    this.name = name;
    this.dependents = dependents;
    this.age = age;
    this.weight = weight;
  }
  public String getName() {
    return name;
  }
  public int getDependents() {
    return dependents;
  }

  public long getAge() {
    return age;
  }
  public double getWeight() {
    return weight;
  }
}

1. To get the summary statistics for the weight field which is of type int.

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

public class SummarizingCollectorDemo {
  public static void main(String[] args) {
    List<Person> personList = Arrays.asList(new Person("Peter", 2, 45, 75.6),
              new Person("Ram", 3, 34, 80),
              new Person("Priscilla", 1, 26, 68),
              new Person("Ajay", 4, 35, 71.5),
              new Person("Dan", 0, 58, 77.8));
    IntSummaryStatistics stats = personList.stream()
                    .collect(Collectors.summarizingInt(Person::getDependents));
    System.out.println("Total count of person- " + stats.getCount());
    System.out.println("Max dependents- " + stats.getMax());
    System.out.println("Min dependents- " + stats.getMin());
    System.out.println("Total number of dependents- " + stats.getSum());
    System.out.println("Average number of dependents- " + stats.getAverage());
  }
}
Output
Total count of person- 5
Max dependents- 4
Min dependents- 0
Total number of dependents- 10
Average number of dependents- 2.0

Collectors.summarizingLong() and Collectors.summarizingDouble() Java example

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

public class SummarizingCollectorDemo {
  public static void main(String[] args) {
    List<Person> personList = Arrays.asList(new Person("Peter", 2, 45, 75.6),
              new Person("Ram", 3, 34, 80),
              new Person("Priscilla", 1, 26, 68),
              new Person("Ajay", 4, 35, 71.5),
              new Person("Dan", 0, 58, 77.8));

    LongSummaryStatistics longSummaryStats = personList.stream()
        .collect(Collectors.summarizingLong(Person::getAge));
    
    DoubleSummaryStatistics doubleSummaryStats = personList.stream()
        .collect(Collectors.summarizingDouble(Person::getWeight));
    System.out.println("Summary statistics for age- " + longSummaryStats);
    System.out.println("Summary statistics for weight- " + doubleSummaryStats);
  }
}
Output
Summary statistics for age- LongSummaryStatistics{count=5, sum=198, min=26, average=39.600000, max=58}
Summary statistics for weight- DoubleSummaryStatistics{count=5, sum=372.900000, min=68.000000, average=74.580000, max=80.000000}

That's all for the topic Summarizing Collectors in Java Stream. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment