0

我有一个名为 Employee 的 pojo,现在我想创建一个用户定义的集合,并将其放在地图上。请告诉我如何实现这一目标。

public class Employee {
    String name,job;
    int salary;

    public Employee(String n , String j, int t) {
        this.name= n;
        this.job=j;
        this.salary= t;          
    }

    @Override
    public int hashCode() {      
        return name.hashCode()+job.hashCode()+salary;        
    }

    @Override
    public boolean equals(Object obj) {  
        Employee e = (Employee) obj;    
        return this.name.equals(e.name) && this.job.equals(e.job)
                   && this.salary == e.salary;
    }   
}
4

2 回答 2

0

不确定你在找什么。如果您想将员工与键(例如,他们的姓名)相关联,您可以执行以下操作:

Map<String, Employee> employeeMap = new HashMap<String, Employee>();

如果您想将员工映射到其他值,您可以执行以下操作:

Map<Employee, MyOtherType> employeeMap = new HashMap<Employee, MyOtherType>();
于 2012-04-15T12:50:26.133 回答
0

类似于以下内容:(?)

Collection<Employee> employees = new LinkedList<Employee>();
Map<String, Collection<Employee>> map = new HashMap<String, Collection<Employee>>();

...........

//Now fill the map as following:
List<Employee> employeesOfTheDepartment = new LinkedList<Employee>();
employeesOfTheDepartment.add(employee);
map.put(employee.getDepartment(), employeesOfTheDepartment);
于 2012-04-15T12:52:28.200 回答