2017-01-29 11 views
-1

hashmultimap을 만들었습니다 iterator를 사용하여 hashmultimap 내에서 내 Student 개체에 액세스하려면 어떻게해야합니까?Google Guava MultiMap 내 개체에 액세스하는 방법을 모르겠다

Multimap<Integer, Object> myMultimap2 = HashMultimap.create(); 
Student one = new Student("Bob","Any",35); 
Student two = new Student("Tom","Johnson",22); 
Student three = new Student("Yo","Zun",42); 
myMultimap2.put(1,one); 
myMultimap2.put(2,two); 
myMultimap2.put(2,three); 
Iterator<Integer> iterator = myMultimap2.keySet().iterator(); 

while (iterator.hasNext()){ 
    int key = iterator.next(); 
    System.out.println(key); 
    Collection collection = myMultimap2.get(key); 
    Iterator iterator2 = collection.iterator(); 
    while (iterator2.hasNext()){ 
     System.out.println(iterator2.next()); 
     ??????? 
    } 
} 
+0

그래서이 클래스가 존재한다고 생각했습니다. howto 삽입에 관한 문서를 읽었지만, 값 검색에 관한 문서를 읽을 수 없습니까? 그냥 궁금해서 ... – GhostCat

답변

1

구아바의 Multimap 각 키에 대한 값의 Collection 유지합니다. 따라서 두 번째 반복자 iterator2에 의해 반환 된 Object은 이전에 넣은 Student입니다.

올바른 제네릭을 사용하는 것이 더 적합 할 수 있습니다. 예를 들어 MultimapMultimap<Integer, Student>으로 신고하는 것이 좋습니다. 그런 다음 두 번째 반복기는 Object 대신 Student을 반환합니다.