Java 8 - Count Male and Female Employees in Organization

1. Introduction to the Problem

Hello everyone, welcome back! In this blog post, we’ll learn how to use Java 8 Streams API to count the number of male and female employees in an organization. We’ll leverage the power of streams and the Collectors.groupingBy() and Collectors.counting() methods, which make it easy to group data and count occurrences efficiently. Let’s dive in!"

The objective:

  • Goal: Count the number of male and female employees in an organization.
  • Java 8 Streams API: Efficiently handle and group data.
  • Methods Used:
    • Collectors.groupingBy()
    • Collectors.counting()

2. Setting Up the Employee Class

First, we need a simple Employee class to represent each employee in our organization. This class will include basic details like id, name, gender, and department. We’ll use this class to create a list of employees that we’ll count by gender.

public class Employee {
    private int id;
    private String name;
    private String gender;
    private String department;

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

    // Getters
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getGender() {
        return gender;
    }

    public String getDepartment() {
        return department;
    }
}

Explanation:

  • We’ve created an Employee class with properties such as id, name, gender, and department.
  • The getter methods provide access to these fields, which we’ll use in our counting logic.

3. Creating a List of Employees

Let’s now create a list of Employee objects. This list will act as our dataset for counting the number of male and female employees.

import java.util.Arrays;
import java.util.List;

public class EmployeeList {

    public static List<Employee> getEmployees() {
        return Arrays.asList(
            new Employee(1, "Amit", "Male", "IT"),
            new Employee(2, "Sita", "Female", "Finance"),
            new Employee(3, "Rahul", "Male", "HR"),
            new Employee(4, "Priya", "Female", "IT"),
            new Employee(5, "Vijay", "Male", "Finance"),
            new Employee(6, "Anjali", "Female", "HR")
        );
    }
}

Explanation:

  • The getEmployees() method returns a list of Employee objects with various genders and departments. Sample data for counting employees by gender.
  • This list simulates a small dataset for our example.

4. Counting Male and Female Employees using Streams

Now that we have our data. Next, let’s use Java 8 Streams to count the number of male and female employees. The Collectors.groupingBy() method will group employees by gender and Collectors.counting() will count the number of employees in each group.

import java.util.Map;
import java.util.stream.Collectors;

public class GenderCount {

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

        // Group and count by gender
        Map<String, Long> genderCount = employees.stream()
                .collect(Collectors.groupingBy(Employee::getGender, Collectors.counting()));

        // Print the results
        genderCount.forEach((gender, count) -> System.out.println(gender + ": " + count));
    }
}

Output:

Male: 3
Female: 3

Explanation:

  • Grouping and Counting:
    • The groupingBy(Employee::getGender, Collectors.counting()) groups employees by gender and counts each group.
  • Result:
    • The output shows the number of male and female employees in the organization.

5. Explanation of Key Methods

Let’s break down the two main methods we used for grouping and counting: Collectors.groupingBy() and Collectors.counting().

  • Collectors.groupingBy():
    • Groups data based on a classifier function.
    • Example: groupingBy(Employee::getGender) – groups by gender.
  • Collectors.counting():
    • Counts the number of elements in each group.
    • Used with groupingBy() to count employees per gender.

6. Real-World Use Cases

This approach can be applied in various real-world scenarios, such as:

  1. Counting employees by different attributes, like department or location.
  2. Summarizing employee data based on different metrics for reporting and analysis.
  3. Grouping and counting any data by specific characteristics.

7. Conclusion

In this blog post, we learned how to use Java 8 Streams to efficiently count male and female employees in an organization. By using Collectors.groupingBy() and Collectors.counting(), we can easily group and summarize data based on specific attributes. This approach is powerful for data analysis and reporting in enterprise applications.

Comments