Java Program to Group the Employees by Age

1. Introduction

This tutorial demonstrates how to group employees by age using a Java 8 program. Grouping by age is useful for demographic analysis, planning targeted HR policies, or organizing data better.

2. Program Steps

1. Create an Employee class with fields such as id, name, age, salary, gender, deptName, and city.

2. Write a main class GroupEmployeesByAge that handles the creation of employee instances and groups them by age.

3. Utilize Java's Map to collect employees into age groups.

4. Print out each age group and its corresponding employees.

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 int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return String.format("ID: %d, Name: %s, Age: %d, Salary: %d, Gender: %s, Department: %s, City: %s",
                             id, name, age, salary, gender, deptName, city);
    }
}

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

public class GroupEmployeesByAge {
    public static void main(String[] args) {
        List<Employee> employees = Arrays.asList(
            new Employee(1, "Aditi", 30, 100000, "F", "HR", "Mumbai"),
            new Employee(2, "Rahul", 34, 130000, "M", "Tech", "Bangalore"),
            new Employee(3, "Vishal", 34, 110000, "M", "Tech", "Mumbai"),
            new Employee(4, "Lakshmi", 45, 150000, "F", "HR", "Bangalore")
        );

        Map<Integer, List<Employee>> groupedByAge = employees.stream()
            .collect(Collectors.groupingBy(Employee::getAge));

        groupedByAge.forEach((age, empList) -> {
            System.out.println("Age: " + age);
            empList.forEach(System.out::println);
        });
    }
}

Output:

Age: 30
ID: 1, Name: Aditi, Age: 30, Salary: 100000, Gender: F, Department: HR, City: Mumbai
Age: 34
ID: 2, Name: Rahul, Age: 34, Salary: 130000, Gender: M, Department: Tech, City: Bangalore
ID: 3, Name: Vishal, Age: 34, Salary: 110000, Gender: M, Department: Tech, City: Mumbai
Age: 45
ID: 4, Name: Lakshmi, Age: 45, Salary: 150000, Gender: F, Department: HR, City: Bangalore

Explanation:

1. The Employee class is defined to hold employee data, including a constructor to initialize these values.

2. GroupEmployeesByAge contains the main method where a list of Employee objects is created and grouped by age using the Collectors.groupingBy method from the Stream API.

3. The map groupedByAge is iterated over, and each age group along with its employees is printed, demonstrating the grouping operation.

Comments