我有一個HashMap,它包含另一個HashMap。我想迭代第一個HashMap並使用它的Key值。然後,當我迭代第一個HashMap時,我想要啓動一個迭代第二個HashMap的內部循環,獲取所有的值。從迭代器獲取關鍵值
我到目前爲止的問題是,我無法弄清楚如何從Iterator獲取密鑰。
HashMap<String, HashMap<Integer, String>> subitems = myHashMap.get("mainitem1");
Collection c = subitems.values();
Iterator itr = c.iterator();
while(itr.hasNext())
{
// Get key somehow? itr.getKey() ???
// contains the sub items
HashMap productitem = (HashMap)itr.next();
}
,我從subitems
得到的數據是這樣的:
{Item1{0=sub1, 1=sub2}, Item2{0=sub3, 1=sub4}}
然後,在while循環productitem
包含 '子項'。但我無法找到我可以從哪裏獲得關鍵值'Item1'和'Item2'。
我怎樣才能得到那些?
除了根據下面的答案使用'entrySet()',還可以使用'keySet()'來獲得鍵。 – bdares 2012-07-25 08:38:13