The total number of employees

Report a typo

Let's say you are developing an employee management system for your company. It has two classes:

  • Employee with the fields name (String) and salary (Long)

  • Department with the fields name (String), code (String), and employees (List<Employee>).

Both classes have getters for all fields with the corresponding names (getName(), getSalary(), getEmployees() and so on).

Using Stream API, implement a method that calculates the total number of employees with salary >= threshold for all departments whose code starts with the string "111-" (without ""). Perhaps, the flatMap method can help you to implement it.

Important. Use the given template for your method. Pay attention to type of an argument and the returned value. Please, use only Stream API without loops.

Examples: there are 2 departments (in JSON notation) below and threshold = 20 000. The result is 1 (because there is only one suitable employee).

[
  {
    "name": "dep-1",
    "code": "111-1",
    "employees": [
      {
        "name": "William",
        "salary": 20000
      },
      {
        "name": "Sophia",
        "salary": 10000
      }
    ]
  },
  {
    "name": "dep-2",
    "code": "222-1",
    "employees": [
      {
        "name": "John",
        "salary": 50000
      }
    ]
  }
]
Write a program in Java 17
import java.util.*;
import java.util.stream.Collectors;

class EmployeesCounter {

/**
* Calculates the number of employees with salary >= threshold (only for 111- departments)
*
* @param departments are list of departments
* @param threshold is lower edge of salary
*
* @return the number of employees
*/
public static long calcNumberOfEmployees(List<Department> departments, long threshold) {
return 0; // write your code here
}

// Don't change the code below
static class Employee {
private final String name;
private final Long salary;

Employee(String name, Long salary) {
this.name = name;
this.salary = salary;
}

public String getName() {
return name;
}

public Long getSalary() {
return salary;
}

@Override
___

Create a free account to access the full topic