Java 8 – Print the Number of Employees in Each Department

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 Employee objects, 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

  1. Create an Employee Class: Define an Employee class with fields such as name, department, and other relevant attributes.
  2. Group Employees by Department: Use Collectors.groupingBy to group employees by department.
  3. Count Employees in Each Group: Use Collectors.counting to count the number of employees in each department.
  4. 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

  • Employee Class: The Employee class includes fields name and department. The getDepartment() 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 using Collectors.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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top