Java Program To Find Maximum Age of Employee

1. Introduction

In this Java tutorial, we will learn how to find the maximum age of an employee using a simple Java program. This functionality is often used in HR applications to determine the oldest employee for planning retirement benefits and other age-related policies.

2. Program Steps

1. Create an Employee class with the necessary attributes.

2. Write a main class named FindMaxAge that processes a list of Employee objects to find the maximum age.

3. Utilize the Stream API's max method combined with Comparator to determine the oldest employee.

4. Print the maximum age found.

3. Code Program

// File:
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) { = id; = name;
        this.age = age;
        this.salary = salary;
        this.gender = gender;
        this.deptName = deptName; = city;

    public int getAge() {
        return age;

// File:
import java.util.*;

public class FindMaxAge {
    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", "Tech", "Bangalore"),
            new Employee(3, "Vishal", 34, 110000, "M", "Tech", "Mumbai"),
            new Employee(4, "Lakshmi", 45, 150000, "F", "HR", "Bangalore")

        Optional<Integer> maxAge =

        maxAge.ifPresent(age -> System.out.println("Maximum Age: " + age));


Maximum Age: 45


1. The Employee class is structured to include an age attribute among other employee details.

2. The FindMaxAge class uses a list of Employee objects and employs the Stream API to extract ages and find the maximum.

3. The max function from Stream API uses Integer::compare to determine the highest age.

4. The result is wrapped in an Optional to safely handle cases where the list might be empty, ensuring robust code behavior.