2012-04-15 4 views
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; 
    } 
} 
+0

구현을 위해 [항목] 7 및 [여기] (http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf)을 참조하십시오. 같음 및 해시 코드 – luketorjussen

답변

0

찾고 계신 것이 확실하지 않습니다. 당신이 키 (예를 들어, 이름) 직원을 연결하려는 경우, 당신은 할 수 있습니다 : 당신이 다른 값으로 종업원을 매핑 할 경우

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

, 당신이 할 수 있습니다

Map<Employee, MyOtherType> employeeMap = new HashMap<Employee, MyOtherType>(); 
+0

다음 위치에 직원을 저장하고 싶습니다. 지도 ...이게 뭔가 .. 맵 employeeMap = new HashMap (); employeeMap.put (a, "Saral", "Trainer", "34000"); 이걸 어떻게 달성 할 수 있는지 조언 해주세요 .. !!!! – Neera

+0

@ user1334074 많은 느낌표가 무례하게 보일 수 있습니다. 멋지게 부탁하십시오 – luketorjussen

+1

종업원 e = 신입 사원 ("Saral", "Trainer", 34000); employeeMap.put (e.getName(), e); – Attila

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); 
+0

지도에 직원을 저장하고 싶습니다. 다음과 같습니다. Map employeeMap = new HashMap (); employeeMap.put (a, "Saral", "Trainer", "34000"); 이걸 어떻게 달성 할 수 있는지 조언 해주세요 .. !!! – Neera

관련 문제