我有一个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