package cn.lijun.demo2; import java.util.HashMap; import java.util.Iterator; import java.util.Set; public class MapMapDemo { /*Map 嵌套存储Map * aaa * java97 班 * 001 小明 * 002 小红 * javaHoodp 班 * 001 张三 * 002 李四 * * java班 :存学号和名字 * javahoodp:存学号和名字 * aaa 存班级 * */ //方法一 :keySet方法 迭代 public static void main(String[] args) { //存放java97班 HashMap<String,String> java=new HashMap<String,String>(); java.put("001", "小明"); java.put("002","小红"); //存放hashhoodp HashMap<String,String> javahoodp=new HashMap<String,String>(); javahoodp.put("001", "张三"); javahoodp.put("002", "小红"); //存放aaa HashMap<String,HashMap<String,String>> aaa=new HashMap<String,HashMap<String,String>>(); aaa.put("java班", java); aaa.put("javahoodp", javahoodp); //用keySet方法把键值对放在set集合 Set<String> k = aaa.keySet(); Iterator<String> it = k.iterator(); while(it.hasNext()){ String akey = it.next(); System.out.println(akey); HashMap<String, String> aValue = aaa.get(akey); System.out.println(aValue); Set<String> skey = aValue.keySet(); System.out.println(skey); Iterator<String> sit = skey.iterator(); while(sit.hasNext()){ String studentkey = sit.next(); String svalue = aValue.get(studentkey); System.out.println(studentkey+" "+svalue); } } } } //方法二 利用entrySet public class MapMapDemo3 { public static void main(String[] args) { HashMap<String,String> java=new HashMap<String,String>(); java.put("001", "小明"); java.put("002", "小红"); HashMap<String,String> javahoodp=new HashMap<String,String>(); javahoodp.put("001","张三"); javahoodp.put("002", "李四"); HashMap<String,HashMap<String,String>> aaa=new HashMap<String,HashMap<String,String>>(); aaa.put("java班",java); aaa.put("javahoodp", javahoodp); entrySet1(aaa); } private static void entrySet1(HashMap<String, HashMap<String, String>> aaa) { Set<Entry<String, HashMap<String, String>>> entrySet = aaa.entrySet(); Iterator<Entry<String, HashMap<String, String>>> it = entrySet.iterator(); while(it.hasNext()){ Entry<String, HashMap<String, String>> next = it.next(); String akey = next.getKey(); HashMap<String, String> avalue = next.getValue(); System.out.println(akey); System.out.println(avalue); Set<Entry<String, String>> student = avalue.entrySet(); Iterator<Entry<String, String>> sit = student.iterator(); while(sit.hasNext()){ Entry<String, String> next2 = sit.next(); String skey = next2.getKey(); String svalue = next2.getValue(); System.out.println(skey+" "+svalue); //System.out.println(svalue); } } //方法三 用增强for循环遍历 public class MapMapDemo4 { public static void main(String[] args) { HashMap<String,String> java=new HashMap<String,String>(); java.put("001","小明"); java.put("002","小红"); HashMap<String,String> javahoodp=new HashMap<String,String>(); javahoodp.put("001", "张三"); javahoodp.put("002","李四"); HashMap<String,HashMap<String,String>> aaa=new HashMap<String,HashMap<String,String>>(); aaa.put("java", java); aaa.put("javahoodp",javahoodp); Set<Entry<String, HashMap<String, String>>> entrySet = aaa.entrySet(); for(Entry<String, HashMap<String, String>> i : entrySet){ String akey = i.getKey(); HashMap<String, String> avalue= i.getValue(); //System.out.println(akey); //System.out.println(avalue); System.out.println(i); Set<Entry<String, String>> entrySet2 = avalue.entrySet(); for(Entry<String, String> s : entrySet2){ System.out.println(s); } }