- package com.itcast.mapTest;
- import java.util.*;
- public class Test {
- //此方法将传入的HashMap<String,String>对象的键和值打印在控制台上
- public static void getStudentInfo(HashMap<String,String> roomMap)
- {
- Iterator<String> it=roomMap.keySet().iterator();
- while(it.hasNext())
- {
- String id=it.next();
- String name=roomMap.get(id);
- sop(id+":"+name);
- }
- }
- public static void sop(Object obj){
- System.out.println(obj);
- }
-
- public static void main(String[] args)
- { //创建HashMap集合,键的类型是 String, 值的类型是 HashMap<String,String>
- HashMap<String,HashMap<String,String>> hmcxy=new HashMap<String,HashMap<String,String>>();
- HashMap<String,String> yure=new HashMap<String,String>(); //创建HashMap集合,键、值都是String类型
- HashMap<String,String> jiuye=new HashMap<String,String>();
-
- hmcxy.put("yureban", yure);
- hmcxy.put("jiuyeban", jiuye);
-
- yure.put("01", "zhangsan");
- yure.put("02", "zhaoliu");
- jiuye.put("01", "wangwu");
- jiuye.put("02", "chenqi");
- //遍历hmcxy集合获取所有的教室
- Iterator<String> it=hmcxy.keySet().iterator();
- //此处是key在使用迭代器对吗? -- 建立hmcxy的键集的迭代器
- while(it.hasNext())
- //表示判断key还存在不?如果在继续
- {
- String roomname=it.next();//这个获取key?
- HashMap<String,String> room=hmcxy.get(roomname); //获取key对应的班级
-
- //那既然是班级那不应该是HashMap<String,HashMap<String,String>> 类型吗? 你的班级类型是HashMap<String,String>
- //怎么变成HashMap<String,String>类型的啦? HashMap<String,HashMap<String,String>>的意思是:HashMap<班级名, 班级<Id,name>>
-
- sop(roomname);//这是一个输出语句 输出班级的那既然是输出班级不是应该输出 room? //roomname的值是String类型,它又不是room,为什么要输出room?
- getStudentInfo(room);
- }
-
- }
-
- }
复制代码 |