If the order of the employees is of relevance (or if you need to be able to let one employee be represented multiple times) you need to store them in a list. (Otherwise a Set would suffice.)
I would let Employee
override the equals
method and use List.remove(Object o)
.
From the API docs of List:
boolean remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present (optional operation). If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists).
Concretely, you could do something like
public class Employee{
int empid;
String name;
public boolean equals(Object o) {
if (o == null || !(o instanceof Employee))
return false;
Employee e = (Employee) o;
return empid == e.empid && name.equals(e.name);
}
public int hashCode() {
return empid ^ name.hashCode();
}
}