日期:2014-05-20  浏览次数:20727 次

Map 集合明明使用了泛型,为什么说我不安全操作,
以下情况有多两种以上映射关系,该如何办???
预热班和就业班,每个班里都有 01,02号学生,这是该怎么存

"yurenban" "01" "zhangsan"
"yurenban" "02" "lisi"
"yurenban" "03" "wangwu"

"jiuyeban" "01" "zhaoliu"
"jiuyeban" "02" "huagang"
"jiuyeban" "03" "pingang"

*/



Java code
import java.util.*;
class Student {
        private String id;
        private String name;
        public Student(String id, String name) {
                this.id = id;
                this.name = name;
        }
}
class MapTest03 {
        public static void main(String[] args) {
                Map<String, String> yuren = new HashMap<String, String>();        //预热班 Map,有好多学生映射
                yuren.put("01", "zhangsa");
                yuren.put("02", "lisijin");
                yuren.put("03", "wangcai");

                Map<String, String> jiuye = new HashMap<String, String>();        //就业班 Map,有好多学生映射
                jiuye.put("01", "zhaoliu");
                jiuye.put("02", "huagang");
                jiuye.put("03", "pingang");

                Map<String, Map<String, String>> czbk = new HashMap<String, Map<String, String>>();                        //czbk 是个大 Map,有好多班级映射
                czbk.put("yurenban", yuren);
                czbk.put("jiuyeban", jiuye);

                Set<Map.Entry<String, Map<String, String>>> entryset01 = czbk.entrySet();        //班级集合
                Iterator<Map.Entry<String, Map<String, String>>> it01 = entryset01.iterator();        //班级迭代器
                while(it01.hasNext()) {
                        Map.Entry<String, Map<String, String>> me01 = it01.next();
                        String banid = me01.getKey();
                        Map ban = me01.getValue();
                        Set<Map.Entry<String, String>> entryset02 = ban.entrySet();                        //学生集合
                        Iterator<Map.Entry<String, String>> it02 = entryset02.iterator();        //学生迭代器
                        while(it02.hasNext()) {
                                Map.Entry<String, String> me02 = it02.next();
                                String id = me02.getKey();
                                String name = me02.getValue();
                                System.out.println(banid + "---" + id + "---" + name);
                        }
                }
        }
}


------解决方案--------------------
Map ban = me01.getValue(); //这里没有用泛型
改成
Map<String, String> ban = me01.getValue();