Java Program To Find Youngest Female Employee

1. Introduction

This tutorial demonstrates creating a Java program to find the youngest female employee from a list. This type of program can be especially useful in HR applications where gender and age-specific data are used for policy-making, benefits, and other employee engagement programs.

Key Points

- Implementation of a custom Employee class with gender and age attributes.

- Usage of Java Streams to filter and find the youngest employee based on gender and age.

- Handling potentially null values with Java's Optional.

2. Program Steps

1. Define the Employee class with appropriate attributes.

2. Create a list of Employee instances.

3. Use Java's Stream API to filter female employees and find the youngest among them.

4. Print the details of the youngest female 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 getGender() {
        return gender;
    }

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

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

public class FindYoungestFemaleEmployee {
    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", 28, 150000, "F", "HR", "Bangalore"),
            new Employee(5, "Priya", 24, 90000, "F", "Marketing", "Delhi")
        );

        Employee youngestFemale = employees.stream()
                                           .filter(e -> "F".equals(e.getGender()))
                                           .min(Comparator.comparingInt(Employee::getAge))
                                           .orElse(null);

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

Output:

Youngest female employee: Employee{id=5, name='Priya', age=24, salary=90000, gender='F', deptName='Marketing', city='Delhi'}

Explanation:

1. The Employee class is equipped with fields necessary for identifying and comparing employee details such as gender and age.

2. The FindYoungestFemaleEmployee class initializes a list of employees and applies a stream filter to isolate female employees.

3. The min function, combined with a comparator for age, determines the youngest female employee.

4. The result is printed, and in cases where no female employees are present, a default message is shown to handle the null scenario gracefully.

Comments