Java Program To Find Oldest Employee

1. Introduction

In this Java tutorial, we'll create a program to find the oldest employee in a list. This task is useful in various business scenarios, such as determining eligibility for certain benefits based on age or recognizing long-serving employees.

Key Points

- Use of a custom Employee class with necessary attributes including age.

- Application of Java Streams to efficiently find the employee with the maximum age.

- Handling potentially null values gracefully using Java's Optional.

2. Program Steps

1. Define the Employee class with attributes such as age.

2. Populate a list with instances of Employee.

3. Use the Stream API to find the employee with the highest age.

4. Print the details of the oldest employee.

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;
    }

    public String toString() {
        return "Employee{" +
               "id=" + id +
               ", name='" + name + '\'' +
               ", age=" + age +
               ", salary=" + salary +
               ", gender='" + gender + '\'' +
               ", deptName='" + deptName + '\'' +
               ", city='" + city + '\'' +
               '}';
    }
}

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

public class FindOldestEmployee {
    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", 45, 150000, "F", "HR", "Bangalore")
        );

        Employee oldest = employees.stream()
                                   .max(Comparator.comparingInt(Employee::getAge))
                                   .orElse(null);

        if (oldest != null) {
            System.out.println("Oldest employee: " + oldest);
        } else {
            System.out.println("No employees found.");
        }
    }
}

Output:

Oldest employee: Employee{id=4, name='Lakshmi', age=45, salary=150000, gender='F', deptName='HR', city='Bangalore'}

Explanation:

1. The Employee class includes an age field which is critical for determining the oldest employee.

2. A list of Employee objects is created in the FindOldestEmployee class.

3. The stream().max() method along with a comparator for age is used to find the employee with the maximum age.

4. The orElse method handles the scenario where the list might be empty, avoiding a NullPointerException.

5. Finally, the details of the oldest employee are printed. If no employees are found, an appropriate message is displayed.

Comments