Introduction
Java 8 introduced the Stream API, which provides a powerful and expressive way to process collections of data. One common task in data processing is counting the number of elements in specific groups. For instance, in an organization, you may want to count the number of employees in each department. The Stream API, combined with Collectors.groupingBy and Collectors.counting, makes this operation simple and efficient.
In this guide, we will explore how to use Java 8 streams to count and print the number of employees in each department.
Table of Contents
- Problem Statement
- Solution Steps
- Java Program
- Example: Counting and Printing the Number of Employees in Each Department
- Conclusion
Problem Statement
You need to count the number of employees in each department from a list of Employee objects. This operation is common in scenarios where you need to analyze data based on specific groupings, such as departments.
Example:
- Problem: Given a list of
Employeeobjects, count and print the number of employees in each department. - Goal: Use Java 8’s Stream API to efficiently count and print the number of employees in each department.
Solution Steps
- Create an Employee Class: Define an
Employeeclass with fields such as name, department, and other relevant attributes. - Group Employees by Department: Use
Collectors.groupingByto group employees by department. - Count Employees in Each Group: Use
Collectors.countingto count the number of employees in each department. - Print the Results: Display the number of employees for each department.
Java Program
Example: Counting and Printing the Number of Employees in Each Department
First, define the Employee class with the necessary fields.
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Java 8 - Print the Number of Employees in Each Department
* Author: https://www.rameshfadatare.com/
*/
public class CountEmployeesByDepartmentExample {
public static void main(String[] args) {
List<Employee> employees = Arrays.asList(
new Employee("Amit", "IT"),
new Employee("Priya", "HR"),
new Employee("Raj", "Finance"),
new Employee("Suman", "IT"),
new Employee("Kiran", "HR")
);
// Group employees by department and count them
Map<String, Long> employeeCountByDepartment = employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment, Collectors.counting()));
// Print the number of employees in each department
employeeCountByDepartment.forEach((department, count) ->
System.out.println("Department: " + department + ", Number of Employees: " + count));
}
}
class Employee {
private String name;
private String department;
public Employee(String name, String department) {
this.name = name;
this.department = department;
}
public String getName() {
return name;
}
public String getDepartment() {
return department;
}
}
Output
Department: IT, Number of Employees: 2
Department: HR, Number of Employees: 2
Department: Finance, Number of Employees: 1
Explanation
EmployeeClass: TheEmployeeclass includes fieldsnameanddepartment. ThegetDepartment()method is used to retrieve the department of the employee.groupingBy(Employee::getDepartment, Collectors.counting()): The Stream API groups employees by department and counts the number of employees in each group usingCollectors.counting.forEach(): The number of employees in each department is printed to the console.
Conclusion
Using Java 8’s Stream API, counting and printing the number of employees in each department is both efficient and easy to implement. The combination of groupingBy and counting collectors allows you to group data by specific attributes and count the number of elements in each group.