Java 8 – Find Maximum Age of Employee

Introduction

Java 8 introduced the Stream API, which provides a powerful and functional way to process collections of data. One common operation is finding the maximum value based on a particular field in a collection of objects. For instance, you might need to find the maximum age of employees in an organization. The Stream API makes this operation simple and efficient.

In this guide, we will explore how to use Java 8 streams to find the maximum age of an employee in a list of employees.

Table of Contents

  • Problem Statement
  • Solution Steps
  • Java Program
    • Example: Finding the Maximum Age of an Employee
  • Conclusion

Problem Statement

You need to find the maximum age of employees from a list of Employee objects. This operation is common in scenarios where you need to identify the oldest employee or the highest value in a dataset.

Example:

  • Problem: Given a list of Employee objects, find the maximum age among all employees.
  • Goal: Use Java 8’s Stream API to efficiently find the maximum age.

Solution Steps

  1. Create an Employee Class: Define an Employee class with fields such as name, age, and department.
  2. Find the Maximum Age: Use the max method from the Stream API to find the employee with the maximum age.
  3. Handle the Result: Properly handle the result, including potential empty lists.

Java Program

Example: Finding the Maximum Age of an Employee

First, define the Employee class with the necessary fields.

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

/**
 * Java 8 - Find Maximum Age of Employee
 * Author: https://www.rameshfadatare.com/
 */
public class MaxAgeEmployeeExample {

    public static void main(String[] args) {
        List<Employee> employees = Arrays.asList(
            new Employee("Amit", 30, "IT"),
            new Employee("Priya", 25, "HR"),
            new Employee("Raj", 28, "Finance"),
            new Employee("Suman", 24, "IT"),
            new Employee("Kiran", 35, "HR")
        );

        // Find the employee with the maximum age
        Optional<Employee> maxAgeEmployee = employees.stream()
                                                     .max(Comparator.comparingInt(Employee::getAge));

        // Print the details of the employee with the maximum age
        if (maxAgeEmployee.isPresent()) {
            Employee employee = maxAgeEmployee.get();
            System.out.println("Employee with maximum age: " + employee.getName() +
                               ", Age: " + employee.getAge() +
                               ", Department: " + employee.getDepartment());
        } else {
            System.out.println("No employees found.");
        }
    }
}

class Employee {
    private String name;
    private int age;
    private String department;

    public Employee(String name, int age, String department) {
        this.name = name;
        this.age = age;
        this.department = department;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getDepartment() {
        return department;
    }
}

Output

Employee with maximum age: Kiran, Age: 35, Department: HR

Explanation

  • Employee Class: The Employee class includes fields name, age, and department. The getAge() method is used to retrieve the age of the employee.
  • max(Comparator.comparingInt(Employee::getAge)): The Stream API finds the employee with the maximum age using the max method with a comparator that compares employees based on their age.
  • Optional<Employee>: The result is wrapped in an Optional to handle the case where the list might be empty. If an employee is found, their details are printed.

Conclusion

Using Java 8’s Stream API, finding the maximum age of an employee is both efficient and straightforward. The combination of the max method and a comparator allows you to quickly identify the employee with the highest age. This approach can be adapted to find the maximum value based on other fields or criteria, making the Stream API used for data processing tasks in Java.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top