- package fuxi1;
- import java.util.HashMap;
- import java.util.Iterator;
- /**
- * Map集合扩展知识:
- * Map集合中可以嵌套Map。
- *@author XiaLei
- */
- public class Day16Test7 {
- public static void main(String[] args) {
- HashMap<String,HashMap<Integer,String>> school = new HashMap<String,HashMap<Integer,String>>();
- HashMap<Integer,String> room_1 = new HashMap<Integer,String>();
- HashMap<Integer,String> room_2 = new HashMap<Integer,String>();//学校里面有两个班级,每个班级里面有学生。
- school.put("room_1", room_1);
- school.put("room_2", room_2);
-
- room_1.put(01, "zhansan");
- room_1.put(02, "lisi");
- room_2.put(01, "wangwu");
- room_2.put(02, "zhaoliu");
-
- Iterator<String> it = school.keySet().iterator();//遍历学校集合的键
- while(it.hasNext()){
- String roomName = it.next();//获得班级的名字
- HashMap<Integer,String> room = school.get(roomName);//通过班级的名字获得班级位置
- System.out.println(roomName);
- getStudent(room);//通过班级位置获得每个学生的信息
- }
-
- }
- private static void getStudent(HashMap<Integer, String> room) {
- //获得学生信息方法
- Iterator<Integer> it = room.keySet().iterator();
- while(it.hasNext()){
- Integer num = it.next();
- String name = room.get(num);
- System.out.println(num+":"+name);
- }
- }
- }
复制代码 |
|