【发布时间】:2011-02-22 22:29:47
【问题描述】:
假设我有一个 List 的 Employee 对象。 Employee 对象有一个 getDepartment 方法,它返回一个 Department 对象。我想遍历该列表以找到具有最多Employees 的部门(即最常从getDepartment 返回的Department 对象)。最快的方法是什么?
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;
}
}
如果有两个部门的员工人数相等,则返回哪个并不重要。
谢谢!
【问题讨论】:
标签: java list data-structures performance counting