假设我有一个List
对象Employee
。Employee
对象有一个getDepartment
返回对象的方法Department
。我想遍历该列表以找到具有最多Employee
s 的部门(即Department
最常从 s 返回的对象getDepartment
)。最快的方法是什么?
public class Employee{
static allEmployees = new ArrayList<Employee>();
int id;
Department department;
public Employee(int id, Department department){
this.id = id;
this.department = department;
allEmployees.add(this);
}
public Department getDepartment(){
return department;
}
public static List<Employee> getAllEmployees(){
return allEmployees;
}
}
public class Department{
int id;
String name;
public Department(int id){
this.id = id;
}
public String getName(){
return name;
}
}
如果有两个部门的员工人数相等,则返回哪个部门并不重要。
谢谢!