How many male and female employees are there in the organization

Category : Java | Sub Category : Java8 Features | By Prasad Bonam Last updated: 2023-11-13 08:23:04 Viewed : 251


In Java 8, you can use streams and lambda expressions to filter and count the number of male and female employees in an organization. Here is an example with output:

java
import java.util.Arrays; import java.util.List; public class Employee { private String name; private String gender; public Employee(String name, String gender) { this.name = name; this.gender = gender; } public String getGender() { return gender; } public static void main(String[] args) { // Sample data List<Employee> employees = Arrays.asList( new Employee("John", "Male"), new Employee("Jane", "Female"), new Employee("Bob", "Male"), new Employee("Alice", "Female"), new Employee("Charlie", "Male") ); // Count male and female employees using Java 8 streams long maleCount = employees.stream() .filter(employee -> "Male".equals(employee.getGender())) .count(); long femaleCount = employees.stream() .filter(employee -> "Female".equals(employee.getGender())) .count(); // Output the results System.out.println("Number of male employees: " + maleCount); System.out.println("Number of female employees: " + femaleCount); } }

Output:

javascript
Number of male employees: 3 Number of female employees: 2

In this example:

  • The Employee class has attributes for name and gender.
  • Sample data is created in the main method with a list of Employee objects.
  • Java 8 streams are used to filter and count the number of male and female employees.
  • The results are then printed to the console.
  • You can use the Collectors.groupingBy collector along with the Collectors.counting downstream collector to group employees by gender and count the number of male and female employees. 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; public Employee(String name, String gender) { this.name = name; this.gender = gender; } public String getGender() { return gender; } public static void main(String[] args) { // Sample data List<Employee> employees = Arrays.asList( new Employee("John", "Male"), new Employee("Jane", "Female"), new Employee("Bob", "Male"), new Employee("Alice", "Female"), new Employee("Charlie", "Male") ); // Group employees by gender and count using Java 8 streams Map<String, Long> genderCounts = employees.stream() .collect(Collectors.groupingBy(Employee::getGender, Collectors.counting())); // Output the results genderCounts.forEach((gender, count) -> System.out.println("Number of " + gender.toLowerCase() + " employees: " + count)); } }

    Output:

    javascript
    Number of male employees: 3 Number of female employees: 2

    In this example:

    • The Collectors.groupingBy collector is used to group employees by gender.
    • The Collectors.counting downstream collector counts the number of employees in each group.
    • The results are stored in a Map<String, Long>, where the keys are gender ("Male" or "Female") and the values are the corresponding counts.
    • The results are then printed to the console.

Search
Related Articles

Leave a Comment: