Java Program To Print Average Age of Male and Female Employees

1. Introduction

This Java tutorial will guide you through calculating the average age of male and female employees. This is a common task in data analysis for HR departments, helping to understand age demographics within the company.

2. Program Steps

1. Define an Employee class with attributes including gender and age.

2. Populate a list with instances of Employee.

3. Use the Stream API to filter employees by gender and calculate the average age for each gender.

4. Print the results.

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

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

public class CalculateAverageAge {
    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")
        );

        Double maleAverage = employees.stream()
                                      .filter(e -> "M".equals(e.getGender()))
                                      .mapToInt(Employee::getAge)
                                      .average()
                                      .orElse(0.0);

        Double femaleAverage = employees.stream()
                                        .filter(e -> "F".equals(e.getGender()))
                                        .mapToInt(Employee::getAge)
                                        .average()
                                        .orElse(0.0);

        System.out.println("Average age of male employees: " + maleAverage);
        System.out.println("Average age of female employees: " + femaleAverage);
    }
}

Output:

Average age of male employees: 29.5
Average age of female employees: 37.5

Explanation:

1. An Employee class is defined with all necessary fields, including gender for filtering and age for averaging.

2. CalculateAverageAge initializes a list of Employee objects and employs Java Streams to segregate employees by gender.

3. The filter method isolates employees of a specific gender, and mapToInt along with average computes the average age.

4. orElse is used to handle cases where there might be no employees of a gender, avoiding division by zero errors.

Comments