我使用此代码添加到hashmap
:
static HashMap<Integer, ArrayList<String>> arrayMap = new HashMap<Integer, ArrayList<String>>();
ArrayList<String> list = new ArrayList<String>();
list.add("t");
list.add("b");
arrayMap.put(12, list);
ArrayList<String> list = new ArrayList<String>();
list.add("y");
list.add("x");
arrayMap.put(18, list);
这段代码工作正常
但我无法在 foreach 中获取 arrayMap
的子元素
for(arrayMap .... ){
return // 12:t,b 18:y,x
}
我的尝试:
for (int a = 0; a < arrayMap.size(); a++)
{
HashMap<String, Integer> tmpData = (HashMap<String, Integer>) arrayMap.get(a);
Set<String> key = tmpData.keySet();
Iterator it = key.iterator();
while (it.hasNext()) {
String hmKey = (String) it.next();
Integer hmData = (Integer) tmpData.get(hmKey);
System.out.println("Key: " + hmKey + " & Data: " + hmData);
it.remove(); // avoids a ConcurrentModificationException
}
}
最佳答案
关于java - 获取 HashMap<Integer, ArrayList<String>> 的所有子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41385502/