完全看需求了 下面这种情况用for那不累坏了 是吧 !具体看代码- package com.heima.collections;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- public class Test1 {
- public static void main(String[] args) {
- Map<String,Map<String ,String>> school=new HashMap<String,Map<String ,String>>();
- Map<String ,String> room1=new HashMap<String,String>();
- Map<String ,String> room2=new HashMap<String,String>();
- // 向两个字集合里面添加值
- room1.put("学生1", "好好学习呵");
- room2.put("学生2", "day day up yeah ");
- // 向学校集合里面添加值
- school.put("教室1", room1);
- school.put("教室2", room2);
- //对学校的迭代
- Iterator<String> it=school.keySet().iterator();
- while(it.hasNext()){
- String roomName=it.next();
- Map<String ,String> role= school.get(roomName);
- System.out.println(roomName);
- //对教室的迭代
- Iterator<String> it1=role.keySet().iterator();
- while(it1.hasNext()){
- String id=it1.next();
- String room=role.get(id);
- System.out.println(room);
- }
- }
- }
- }
复制代码 迭代器也就是装集合的容器。 |