Java8 - What is the average age of male and female employees?

Category : Java | Sub Category : Java8 Features | By Prasad Bonam Last updated: 2023-11-13 08:41:27 Viewed : 258


To calculate the average age of male and female employees in Java 8, you would typically need an additional attribute in your Employee class for the age. I will modify the Employee class to include an age attribute and demonstrate how to calculate the average age for male and female employees:

java
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Employee { private String name; private String gender; private int age; public Employee(String name, String gender, int age) { this.name = name; this.gender = gender; this.age = age; } public String getGender() { return gender; } public int getAge() { return age; } public static void main(String[] args) { // Sample data List<Employee> employees = Arrays.asList( new Employee("John", "Male", 30), new Employee("Jane", "Female", 28), new Employee("Bob", "Male", 35), new Employee("Alice", "Female", 32), new Employee("Charlie", "Male", 40) ); // Calculate average age for male and female employees using Java 8 streams double averageMaleAge = employees.stream() .filter(employee -> "Male".equals(employee.getGender())) .mapToInt(Employee::getAge) .average() .orElse(0.0); // Default value if there are no male employees double averageFemaleAge = employees.stream() .filter(employee -> "Female".equals(employee.getGender())) .mapToInt(Employee::getAge) .average() .orElse(0.0); // Default value if there are no female employees // Output the results System.out.println("Average age of male employees: " + averageMaleAge); System.out.println("Average age of female employees: " + averageFemaleAge); } }

Output:

yaml
Average age of male employees: 35.0 Average age of female employees: 30.0

In this example:

  • The Employee class is modified to include an age attribute.
  • The average age for male and female employees is calculated using Java 8 streams with the filter, mapToInt, and average operations.
  • The results are then printed to the console.
  • To calculate the average age of male and female employees using the groupingBy collector in Java 8, you would need an additional attribute in the Employee class for the age. Here is an example:

    java
    import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Employee { private String name; private String gender; private int age; public Employee(String name, String gender, int age) { this.name = name; this.gender = gender; this.age = age; } public String getGender() { return gender; } public int getAge() { return age; } public static void main(String[] args) { // Sample data List<Employee> employees = Arrays.asList( new Employee("John", "Male", 30), new Employee("Jane", "Female", 28), new Employee("Bob", "Male", 35), new Employee("Alice", "Female", 32), new Employee("Charlie", "Male", 40) ); // Calculate average age for each gender using Java 8 groupingBy Map<String, Double> averageAgeByGender = employees.stream() .collect(Collectors.groupingBy( Employee::getGender, Collectors.averagingDouble(Employee::getAge) )); // Output the results averageAgeByGender.forEach((gender, averageAge) -> System.out.println("Average age of " + gender.toLowerCase() + " employees: " + averageAge)); } }

    Output:

    yaml
    Average age of male employees: 35.0 Average age of female employees: 30.0

    In this example:

    • The Employee class is modified to include an age attribute.
    • Java 8 streams are used with the Collectors.groupingBy collector to group employees by gender.
    • The Collectors.averagingDouble downstream collector calculates the average age for each group.
    • The results are stored in a Map<String, Double>, where the keys are gender ("Male" or "Female") and the values are the corresponding average ages.
    • The results are then printed to the console.

Search
Related Articles

Leave a Comment: