Java Program To Find the Department Name Which Has the Highest Number of Employees

1. Introduction

This Java tutorial will guide you through creating a program that identifies the department with the highest number of employees. Such an analysis can be essential for resource management and operational planning in any organization.

Key Points

- Implementing a custom Employee class with a deptName attribute.

- Using Java's Stream API to group employees by department and count them.

- Identifying the department with the maximum number of employees.

2. Program Steps

1. Define the Employee class with necessary attributes including deptName.

2. Create a list of Employee instances in the main method.

3. Group employees by department and count the number of employees in each.

4. Determine which department has the most employees and print the result.

3. Code Program

// File: Employee.java
public class Employee {
    private int id;
    private String name;
    private int age;
    private long salary;
    private String gender;
    private String deptName;
    private String city;

    public Employee(int id, String name, int age, long salary, String gender, String deptName, String city) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.salary = salary;
        this.gender = gender;
        this.deptName = deptName;
        this.city = city;
    }

    public String getDeptName() {
        return deptName;
    }
}

// File: FindLargestDepartment.java
import java.util.*;
import java.util.stream.Collectors;

public class FindLargestDepartment {
    public static void main(String[] args) {
        List<Employee> employees = Arrays.asList(
            new Employee(1, "Aditi", 30, 100000, "F", "HR", "Mumbai"),
            new Employee(2, "Rahul", 25, 130000, "M", "Engineering", "Bangalore"),
            new Employee(3, "Vishal", 34, 110000, "M", "Engineering", "Mumbai"),
            new Employee(4, "Lakshmi", 28, 150000, "F", "HR", "Bangalore"),
            new Employee(5, "Priya", 24, 90000, "F", "Marketing", "Delhi")
        );

        Map<String, Long> departmentCounts = employees.stream()
            .collect(Collectors.groupingBy(Employee::getDeptName, Collectors.counting()));

        Optional<Map.Entry<String, Long>> maxDept = departmentCounts.entrySet()
            .stream()
            .max(Map.Entry.comparingByValue());

        if (maxDept.isPresent()) {
            System.out.println("Department with the most employees: " + maxDept.get().getKey() +
                               " (" + maxDept.get().getValue() + " employees)");
        } else {
            System.out.println("No departments found.");
        }
    }
}

Output:

Department with the most employees: Engineering (2 employees)

Explanation:

1. The Employee class is defined with a field for deptName, which is essential for grouping employees by department.

2. In the FindLargestDepartment class, a list of Employee objects is created.

3. The Collectors.groupingBy method groups employees by department, and Collectors.counting counts the number of employees in each department.

4. A stream of the map entries is created and the department with the maximum count is determined using max method combined with Map.Entry.comparingByValue.

5. The result, which is the department with the most employees, is printed to the console.

Comments