package itcast05;
import java.util.HashMap;
import java.util.Set;
/**
* HashMap嵌套HashMap
*
* @author lgt
*
*/
public class HashMapDemo {
public static void main(String[] args) {
HashMap<String, HashMap<String, Integer>> czbkMap = new HashMap<String, HashMap<String, Integer>>();
HashMap<String, Integer> jcMap = new HashMap<String, Integer>();
jcMap.put("石乐志", 22);
jcMap.put("芜湖大司马", 39);
czbkMap.put("jc", jcMap);
HashMap<String, Integer> jyMap = new HashMap<String, Integer>();
jyMap.put("怎么说", 28);
jyMap.put("不存在的", 12);
czbkMap.put("jy", jyMap);
Set<String> czbkMapSet = czbkMap.keySet();
for (String czbkMapKey : czbkMapSet) {
System.out.println("班级类型:" + czbkMapKey);
HashMap<String, Integer> czbkMapValue = czbkMap.get(czbkMapKey);
Set<String> jxMapSet = czbkMapValue.keySet();
for (String jxMapSetKey : jxMapSet) {
int age = czbkMapValue.get(jxMapSetKey);
System.out.println("\t\t" + "姓名:" + jxMapSetKey + ", 年龄:" + age);
}
}
}
}
package itcast05;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
public class HashMapIncludeArrayListDemo {
public static void main(String[] args) {
ArrayList<HashMap<String, String>> array = new ArrayList<HashMap<String, String>>();
HashMap<String, String> hm1 = new HashMap<String, String>();
hm1.put("周瑜", "小乔");
hm1.put("吕布", "貂蝉");
array.add(hm1);
HashMap<String, String> hm2 = new HashMap<String, String>();
hm2.put("郭靖", "黄蓉");
hm2.put("杨过", "小龙女");
array.add(hm2);
HashMap<String, String> hm3 = new HashMap<String, String>();
hm3.put("令狐冲", "任盈盈");
hm3.put("林平之", "岳灵珊");
array.add(hm3);
for (HashMap<String, String> hm : array) {
Set<String> set = hm.keySet();
for (String key : set) {
String value = hm.get(key);
System.out.println(key + "---" + value);
}
}
}
}