package lianxi;
/**
* 传智播客旗下有传智播客和黑马,他们都有基础班和就业班,每个班上都有学员姓名和学号,存储各个学员信息并且遍历。
*/
import java.util.HashMap;
import java.util.Set;
public class HashMapDemo1 {
public static void main(String[] args) {
HashMap<String, HashMap<String, HashMap<String, String>>> czbk = new HashMap();
//将传智和黑马TreeMap中,增加元素
HashMap<String, HashMap<String, String>> cz = new HashMap();
czbk.put("传智播客", cz);
HashMap<String, HashMap<String, String>> hm = new HashMap();
czbk.put("黑马", hm);
//将学员分别存储到基础班和就业班中
HashMap<String, String> czBasicClass = new HashMap();
czBasicClass.put("zhangsan", "12");
czBasicClass.put("liss", "15");
//传智中有基础班和就业班,让他们产生关系,将他们添加进去
cz.put("基础班", czBasicClass);
HashMap<String, String> czWorkClass = new HashMap();
czWorkClass.put("wangwu", "14");
czWorkClass.put("qiqi", "15");
//传智中有基础班和就业班,让他们产生关系,将他们添加进去
cz.put("就业班", czWorkClass);
//将学员分别存储到基础班和就业班中
HashMap<String, String> hmBasicClass = new HashMap();
hmBasicClass.put("yiyi", "1");
hmBasicClass.put("erer", "2");
//黑马中有基础班和就业班,让他们产生关系,将他们添加进去
hm.put("基础班", hmBasicClass);
HashMap<String, String> hmWorkClass = new HashMap();
hmWorkClass.put("sansan", "3");
hmWorkClass.put("sisi", "4");
//黑马中有基础班和就业班,让他们产生关系,将他们添加进去
hm.put("就业班", hmWorkClass);
// 开始遍历,先拿到传智和黑马的键的集合
Set<String> czbkKeys = czbk.keySet();
for (String czbkKey : czbkKeys) {
System.out.println(czbkKey);
//通过键的得到相对应的值
HashMap<String, HashMap<String, String>> czhm = czbk.get(czbkKey);
//一层一层的进去,将得到的值作为一个整体对象,再得到内部的键的集合
Set<String> czhmKeys = czhm.keySet();
for (String czhmKey : czhmKeys) {
System.out.println("\t" + czhmKey);
HashMap<String, String> student = czhm.get(czhmKey);
Set<String> stuKeys = student.keySet();
for (String stuKey : stuKeys) {
System.out.println("\t\t"+ stuKey + "---" + student.get(stuKey));
}
}
}
}
}
|