Back to Stream Practice: Employee Analysis
Q28Question 28 of 43

Find the employee closest to retirement age (assuming retirement at 60)

Key Concepts
  • Compute years to retirement as (RETIREMENT_AGE - age) and find the employee with the minimum positive value
  • filter(age < 60) excludes already-retired employees, then min(comparingInt(60 - age)) finds the closest
  • Alternatively: max(comparingInt(age)) among those under 60 gives the same result
  • Handle edge case: employee exactly at retirement age (0 years remaining)
  • Use a constant for retirement age (60) for maintainability
  • For per-department closest to retirement, use groupingBy with minBy

Definition

Filter employees under retirement age, then find the one with the highest age (closest to 60) using max(comparingInt(age)).
  • RETIREMENT_AGE = 60 as a named constant.
  • filter(age < 60) excludes already-retired employees.
  • max(comparingInt(age)) among the filtered set finds the employee closest to retirement.
  • Equivalently: min(comparingInt(e -> RETIREMENT_AGE - e.getAge())) finds the smallest gap.

The bank's succession planning team asks: 'Who is about to retire?' Find the employee under 60 with the highest age, because they are the closest to the retirement line.

Understanding the Concepts

How do you find the employee closest to retirement?

JAVA
package com.bank.hr;

import java.util.*;

public class ClosestToRetirement {
    private static final int RETIREMENT_AGE = 60;

    public static void main(String[] args) {
        List<Employee> employees = Employee.getSampleEmployees();

        Optional<Employee> closest = employees.stream()
            .filter(e -> e.getAge() < RETIREMENT_AGE)
            .max(Comparator.comparingInt(Employee::getAge));

        closest.ifPresent(e ->
            System.out.printf("%s, age %d, retires in %d years%n",
                e.getName(), e.getAge(),
                RETIREMENT_AGE - e.getAge()));
    }
}
TEXT
Suresh, age 58, retires in 2 years

How do you list all employees retiring within 5 years?

JAVA
List<Employee> retiringWithin5 = employees.stream()
    .filter(e -> e.getAge() >= RETIREMENT_AGE - 5
              && e.getAge() < RETIREMENT_AGE)
    .sorted(Comparator.comparingInt(Employee::getAge).reversed())
    .toList();

What are common mistakes?

Mistake: not excluding employees already at or past retirement age.

JAVA
// WRONG: includes employees already aged 60+
Optional<Employee> wrong = employees.stream()
    .max(Comparator.comparingInt(Employee::getAge));
// Could return someone aged 62 who is already retired!

// CORRECT: filter first
Optional<Employee> correct = employees.stream()
    .filter(e -> e.getAge() < RETIREMENT_AGE)
    .max(Comparator.comparingInt(Employee::getAge));

What about finding the closest to retirement per department?

JAVA
Map<String, Optional<Employee>> closestPerDept = employees.stream()
    .filter(e -> e.getAge() < RETIREMENT_AGE)
    .collect(Collectors.groupingBy(
        Employee::getDepartment,
        Collectors.maxBy(
            Comparator.comparingInt(Employee::getAge))));

What about listing employees retiring within N years?

JAVA
public static List<Employee> retiringWithin(
        List<Employee> employees, int years) {
    return employees.stream()
        .filter(e -> e.getAge() >= RETIREMENT_AGE - years
                  && e.getAge() < RETIREMENT_AGE)
        .sorted(Comparator.comparingInt(Employee::getAge).reversed())
        .toList();
}

What about variable retirement age by role?

JAVA
Map<String, Integer> retirementAges = Map.of(
    "TRADING", 60, "OPERATIONS", 62, "RISK", 58);

Optional<Employee> closest = employees.stream()
    .filter(e -> e.getAge() < retirementAges
        .getOrDefault(e.getDepartment(), 60))
    .max(Comparator.comparingInt(e ->
        e.getAge() - retirementAges
            .getOrDefault(e.getDepartment(), 60)));

What are the edge cases?

If all employees are already past retirement age, the filter produces an empty stream and the result is Optional.empty(). An employee exactly at retirement age (age == 60) is excluded by the strict less-than filter.

What are common interview follow-up questions?

  • "How do you find the closest to retirement per department?" Use groupingBy(dept, collectingAndThen(filtering(age < 60), maxBy(comparingInt(age)))).
  • "What if retirement age varies by role?" Accept a Function that maps each employee to their retirement age.

Visual Overview

DIAGRAM
  RETIREMENT_AGE = 60

  filter(age < 60) then max(age):
  ┌──────────────────────────────────────────┐
  │  Rajesh  45  → 60-45 = 15 years left     │
  │  Suresh  58  → 60-58 = 2 years left ← min│
  │  Deepak  55  → 60-55 = 5 years left      │
  │  Vikram  52  → 60-52 = 8 years left      │
  │  Priya   28  → 60-28 = 32 years left     │
  └──────────────────────────────────────────┘
  → Suresh (58) is closest to retirement

Key Takeaway

Finding the employee closest to retirement filters for those under the retirement age, then finds the maximum age in the remaining set. The employee with the highest age below 60 has the fewest years until retirement.

Remember one sentence: filter for age under retirement, then max by age to find who is closest to the retirement line.

---

Share
Use ← → arrow keys to navigate