Java Program To Find the Average Salary of Each Department

1. Introduction

This Java tutorial focuses on how to calculate the average salary of employees in each department. This program can be crucial for HR and finance departments to monitor and plan budget allocations based on departmental salary expenditures.

Key Points

- Use of the custom Employee class with salary and deptName attributes.

- Application of Java's Stream API to group employees by department and calculate the average salary.

- Handling data aggregation and summarization effectively.

2. Program Steps

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

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

3. Use the Stream API to calculate the average salary for each department.

4. Print the results for each department.

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 long getSalary() {
        return salary;
    }

    public String getDeptName() {
        return deptName;
    }
}

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

public class CalculateAverageSalary {
    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, Double> averageSalaries = employees.stream()
            .collect(Collectors.groupingBy(Employee::getDeptName,
                                           Collectors.averagingDouble(Employee::getSalary)));

        averageSalaries.forEach((dept, avgSalary) ->
            System.out.println("Department: " + dept + ", Average Salary: " + avgSalary));
    }
}

Output:

Department: HR, Average Salary: 125000.0
Department: Engineering, Average Salary: 120000.0
Department: Marketing, Average Salary: 90000.0

Explanation:

1. The Employee class includes a salary field that is crucial for calculating the average salary.

2. CalculateAverageSalary initializes a list of Employee objects and uses Java Streams to map each department to its average salary.

3. The Collectors.groupingBy method is used to group employees by department, and Collectors.averagingDouble calculates the average salary for each group.

4. Finally, the average salary for each department is printed, showing how salary distributions vary across the organization.

Comments