Problem Statement: Employee Management System using Stream
You are given a list of Employee objects representing employees in a company. Each Employee object contains the following attributes:
id (integer): Unique identifier for the employee.
name (String): Name of the employee.
department (String): Department in which the employee works.
salary (double): Salary of the employee.
Write a function called filterEmployees that takes in the following parameters:
employees: A list of Employee objects.
department: A String representing the department to filter employees by.
The function should perform the following operations:
Filter out the employees who belong to the specified department.
Map the remaining employees to a String containing their name followed by a colon (:) and their salary.
Return the list of mapped strings.
Function Signature:
public static List<String> filterEmployees(List<Employee> employees, String department) {
}
Input:
employees: A list of Employee objects (1 <= employees.size() <= 1000).
department: A String representing the department to filter employees by.
Output:
A List of Strings containing the names and salaries of the employees who belong to the specified department. The format for each string should be "name: salary".
Constraints:
All salaries are positive non-zero values.
The department name is case-sensitive.
There will always be at least one employee in the given department.
Example:
Sample Input:
List<Employee> employees = Arrays.asList(
new Employee(1, "Dhoni", "Engineering", 100000.0),
new Employee(2, "Virat", "Marketing", 60000.0),
new Employee(3, "Raina", "Engineering", 55000.0),
new Employee(4, "Rishabh", "HR", 45000.0),
new Employee(5, "Rohit", "Engineering", 52000.0)
);
String department = "Engineering";
Sample Output:
Dhoni: 100000.0
Raina: 55000.0
Rohit: 52000.0
Stub Code:
import java.util.Arrays;
import java.util.List;
import java.util.stream.DoubleStream;
public class EmployeeManagementSystem {
public static void filterEmployees(List<Employee> employees, String department) {
. . .
}
static class Employee {
private int id;
private String name;
private String department;
private double salary;
public Employee(int id, String name, String department, double salary) {
this.id = id;
this.name = name;
this.department = department;
this.salary = salary;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getDepartment() {
return department;
}
public double getSalary() {
return salary;
}
}
public static void main(String[] args) {
List<Employee> employees = Arrays.asList(
new Employee(1, "Dhoni", "Engineering", 100000.0),
new Employee(2, "Virat", "Marketing", 60000.0),
new Employee(3, "Raina", "Engineering", 55000.0),
new Employee(4, "Rishabh", "HR", 45000.0),
new Employee(5, "Rohit", "Engineering", 52000.0)
);
String department = "Engineering";
filterEmployees(employees, department);
}
}
public static void filterEmployees(List<Employee> employees, String department) {
employees.stream()
.filter(employee -> employee.getDepartment().equals(department))
.forEach(employee -> System.out.println(employee.getName() + ": " + employee.getSalary()));
}
Insights:
- The program utilizes the Stream API introduced in Java 8 to process a collection of Employee objects.
- Lambda expressions are used within the stream operations (filter and forEach) to provide concise implementations of predicate and consumer functional interfaces.
- The filter operation filters the list of employees based on the specified department using the getDepartment() method of the Employee class.
- The forEach operation iterates over the filtered stream of employees and prints the name and salary of each employee who belongs to the specified department.
- The Employee class encapsulates the attributes of an employee (id, name, department, salary) and provides getter methods to access these attributes.
- The Employee class is defined as a static nested class within the EmployeeManagementSystem class, which helps in organizing related classes together.
- The program initializes a list of Employee objects using Arrays.asList() method, passing the constructor arguments for each employee.
Comments
Post a Comment