Introduction
Java 8 introduced the Stream API, which provides a powerful and expressive way to process collections of data. One common task in data processing is grouping and counting elements based on certain criteria. For instance, in an organization, you may need to count the number of male and female employees. The Stream API, along with Collectors.groupingBy and Collectors.counting, makes this task straightforward and efficient.
In this guide, we will explore how to use Java 8 streams to count male and female employees in an organization.
Table of Contents
- Problem Statement
- Solution Steps
- Java Program
- Example: Counting Male and Female Employees
 
- Conclusion
Problem Statement
You need to process a list of employees and count how many of them are male and how many are female. This count can provide valuable insights into the gender distribution within the organization.
Example:
- Problem: Given a list of Employeeobjects, count how many employees are male and how many are female.
- Goal: Use Java 8’s Stream API to efficiently perform this counting operation.
Solution Steps
- Create an Employee Class: Define an Employeeclass with fields such as name, gender, and department.
- Group Employees by Gender: Use Collectors.groupingByto group employees by gender.
- Count Employees in Each Group: Use Collectors.countingto count the number of employees in each gender group.
Java Program
Example: Counting Male and Female Employees
First, define the Employee class with the necessary fields.
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * Java 8 - Count Male and Female Employees in the Organization
 * Author: https://www.rameshfadatare.com/
 */
public class EmployeeGenderCountExample {
    public static void main(String[] args) {
        List<Employee> employees = Arrays.asList(
            new Employee("Amit", "Male", "IT"),
            new Employee("Priya", "Female", "HR"),
            new Employee("Raj", "Male", "Finance"),
            new Employee("Suman", "Female", "IT"),
            new Employee("Kiran", "Male", "HR")
        );
        // Group employees by gender and count them
        Map<String, Long> genderCount = employees.stream()
            .collect(Collectors.groupingBy(Employee::getGender, Collectors.counting()));
        // Display the counts
        System.out.println("Male Employees: " + genderCount.getOrDefault("Male", 0L));
        System.out.println("Female Employees: " + genderCount.getOrDefault("Female", 0L));
    }
}
class Employee {
    private String name;
    private String gender;
    private String department;
    public Employee(String name, String gender, String department) {
        this.name = name;
        this.gender = gender;
        this.department = department;
    }
    public String getName() {
        return name;
    }
    public String getGender() {
        return gender;
    }
    public String getDepartment() {
        return department;
    }
}
Output
Male Employees: 3
Female Employees: 2
Explanation
- EmployeeClass: The- Employeeclass has fields- name,- gender, and- department. The- getGender()method is used to retrieve the gender of an employee.
- groupingBy(Employee::getGender, Collectors.counting()): The Stream API groups the employees by gender and then counts the number of employees in each group using- Collectors.counting().
- getOrDefault(): This method ensures that if no employees of a certain gender exist, a default value of- 0Lis returned.
Conclusion
Using Java 8’s Stream API, counting the number of male and female employees in an organization is both efficient and concise. The combination of groupingBy and counting makes it easy to group elements by a common attribute and count them, providing valuable insights into the data. This approach can be extended to other similar tasks, such as counting employees by department, role, or any other attribute.