Collectors: Aggregation Operations

In this lesson, we will learn about methods of the Collectors class, which are used for aggregation.

In this lesson, we will look at some of the methods of the Collectors class that help us aggregate the data in streams, e.g., sum, average, etc.

1) counting()

This function returns a Collector that counts the number of the input elements.

Suppose we have a list of employees, and we need the count of employees with an age more than 30.

In this case, we can use the counting() method as shown below.

Press + to interact
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class CollectorsDemo {
public static void main(String args[]) {
List<Employee> employeeList = new ArrayList<>();
employeeList.add(new Employee("Alex", 23, 23000));
employeeList.add(new Employee("Ben", 63, 25000));
employeeList.add(new Employee("Dave", 34, 56000));
employeeList.add(new Employee("Jodi", 43, 67000));
employeeList.add(new Employee("Ryan", 53, 54000));
long count = employeeList.stream()
.filter(emp -> emp.getAge() > 30)
.collect(Collectors.counting()); // Using the counting() method to get count of employees.
System.out.println(count);
}
}
class Employee {
String name;
int age;
int salary;
Employee(String name) {
this.name = name;
}
Employee(String name, int age, int salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getSalary() {
return salary;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
}

2) Collectors.summingInt(ToIntFunction<? super T> mapper)

...