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>();

Employees を他の値にマップする場合は、次のようにします。

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 に答える