本帖最后由 陶圣虎 于 2014-8-10 13:17 编辑
Map 集合使用是因为具有映射关系
特殊情况:映射关系中嵌套Map集合
yureban 01 zhangshan
yureban 02 lisi
jiuyeban 01 wangwu
jiuyeban 02 zhaoliu
学校有教室,教室有学生,学生有编号
毕老师视频里是先存的String,在存的Map,我这里反过来了
但是出现的问题很不解,就是里面注释的部位,换了下位置,输出结果不一样,我不知道czbk.put()的位置还影响结果,希望大家帮我看下。
*/
- import java.util.*;
- class MapDemo3
- {
- public static void main(String []args)
- {
-
- HashMap<HashMap<String,String>,String> czbk = new HashMap<HashMap<String,String>,String>();
- HashMap<String,String> yure = new HashMap<String,String>();
- HashMap<String,String> jiuye = new HashMap<String,String>();
-
- czbk.put(yure,"yureban"); //在上面时只输出 jiuyeban
- czbk.put(jiuye,"jiuyeban"); // 01:zhangsan
- yure.put("01","zhangsan"); // 02:lisi
- yure.put("02","lisi");
- jiuye.put("01","wangwu");
- jiuye.put("02","zhaoliu");
- //czbk.put(yure,"yureban"); //在下面就可以输出完整信息
- //czbk.put(jiuye,"jiuyeban"); //为什么这里变换位置会影响结果
-
-
- Set<Map.Entry<HashMap<String,String>,String>> entryset = czbk.entrySet();
- Iterator<Map.Entry<HashMap<String,String>,String>> it = entryset.iterator();
- while (it.hasNext())
- {
- Map.Entry<HashMap<String,String>,String> me = it.next();
- HashMap<String,String> room = me.getKey();
- String roomname = me.getValue();
- System.out.println(roomname);
- getStuInfo(room);
-
- }
-
- }
- public static void getStuInfo(HashMap<String,String> room)
- {
- Set<Map.Entry<String,String>> entrySet = room.entrySet();
- Iterator <Map.Entry<String,String>> iter = entrySet.iterator();
- while (iter.hasNext())
- {
- Map.Entry<String,String> me2 = iter.next();
- String id = me2.getKey();
- String name = me2.getValue();
- System.out.println(id+":"+name);
- }
- }
- }
复制代码 |
|