Given the following task. We have an Employee
and a Company
classes. Each instance of Employee
class is stored in array Employee[] employees
in the Company
class. I need a method which removes an instance of Employee
in the array Employee[] employees
by id
.
I managed to write the following code:
public class Employee {
protected final int id;
protected String name;
public Employee(int id, String name) {
this.id = id;
this.name= name;
}
public int getId() {
return id;
}
}
public class Company {
private Employee[] employees;
private int size;
private static final int defaultCapacity = 5;
public Company() {
this(defaultCapacity);
}
public Company(int capacity) {
if (capacity <= 0)
throw new RuntimeException("capacity is required");
employees = new Employee[capacity];
}
public Employee removeEmployee(int id) {
Collection<Employee> employeeList = Arrays.asList(employees)
.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
Employee[] employeeArray = employeeList.toArray(Employee[]::new);
for (int i = 0; i < size; i++) {
if(employeeArray[i].getId() == id) {
Employee removedEmployee = employees[i];
employeeList.remove(employeeArray[i]);
employees = employeeList
.stream()
.filter(Objects::nonNull)
.toArray(Employee[]::new);
return removedEmployee;
}
}
return null;
}
}
The problem is that my method public Employee removeEmployee(int id)
throws NullPointerException
if an element for removal is not found.
Question:
- How can I rewrite the method
public Employee removeEmployee(int id)
using, for instance, Streams API and Optional in oder to get rid of NullPointerException in the methodpublic Employee removeEmployee(int id)
?
N.B.: The length of the array Employee[] employees
declared in the class Company
must be reduced after the element has been successfully removed.