java创建一个存储多个HashMap的遍历
时间: 2023-05-21 17:02:39 浏览: 94
Java HashMap的三种遍历方法及优缺点含示例
您好,以下是一个示例代码,用于创建一个存储多个HashMap的遍历:
```java
Map<String, Map<String, String>> mapOfMaps = new HashMap<>();
// 添加第一个HashMap
Map<String, String> map1 = new HashMap<>();
map1.put("key1", "value1");
map1.put("key2", "value2");
mapOfMaps.put("map1", map1);
// 添加第二个HashMap
Map<String, String> map2 = new HashMap<>();
map2.put("key3", "value3");
map2.put("key4", "value4");
mapOfMaps.put("map2", map2);
// 遍历所有的HashMap
for (Map.Entry<String, Map<String, String>> entry : mapOfMaps.entrySet()) {
String mapName = entry.getKey();
Map<String, String> map = entry.getValue();
System.out.println("Map name: " + mapName);
for (Map.Entry<String, String> innerEntry : map.entrySet()) {
String key = innerEntry.getKey();
String value = innerEntry.getValue();
System.out.println("Key: " + key + ", Value: " + value);
}
}
```
希望这个示例能够帮助您!
阅读全文