HashMap嵌套HashMap
1、示例代码如下:
package com.jh.www;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class Test6 {
public static void main(String[] args) {
HashMap<String,Integer> hm1 = new HashMap<>();
hm1.put("张三", 18);
hm1.put("李四", 20);
hm1.put("王五", 22);
HashMap<String,Integer> hm2 = new HashMap<>();
hm2.put("西施", 16);
hm2.put("貂蝉", 18);
hm2.put("杨贵妃", 20);
HashMap<String,HashMap<String,Integer>> hmAll = new HashMap<>();
hmAll.put("男",hm1 );
hmAll.put("女", hm2);
System.out.println(hmAll);
Set<Map.Entry<String,HashMap<String,Integer>>> set = hmAll.entrySet();
for(Map.Entry<String,HashMap<String,Integer>> hmCh: set) {
String mapKe = hmCh.getKey();
HashMap<String,Integer> mapVa = hmCh.getValue();
for( String key: mapVa.keySet()) {
Integer value = mapVa .get(key);
System.out.println(mapKe+"---"+key+"="+value);
}
}
}
}