Java:遍历另一个HashMap中的HashMap
我想遍历另一个HashMap
中的HashMap
Map<String, Map<String, String>> PropertyHolder
我可以在父HashMap
中迭代如下:
Iterator it = PropertyHolder.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
System.out.println("pair.getKey() : " + pair.getKey() + " pair.getValue() : " + pair.getValue());
it.remove(); // avoids a ConcurrentModificationException
}
但是无法遍历子Map
,可以通过转换pair.getValue().toString()
并使用,
和=
进行分离。还有其他的迭代方法吗
# 1 楼答案
# 2 楼答案
您可以像对父映射一样迭代子映射:
我假设您想在子映射上调用
.remove()
,如果在循环入口集时调用,这将导致ConcurrentModificationException——看起来您已经发现了这一点我还按照评论中的建议,将强制类型转换与强类型泛型交换了
# 3 楼答案
很明显,您需要两个嵌套循环: