2017-01-29 9 views
-1

hashmultimapを作成しました イテレータを使って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の2番目のイテレータで返されたObjectは、実際には先に入れたStudentです。

適切なジェネリックスを使用すること、つまりMultimapMultimap<Integer, Student>と宣言することが適切なのでしょうか。次に、2番目のイテレータはObjectの代わりにStudentを返します。

関連する問題