HashMap嵌套HashMap之遍历

/**
 * 需求:黑马48期学生基础班定义成一个双列集合,键是学生对象,值是归属地
 *      黑马49期学生基础班定义成一个双列集合,键是学生对象,值是归属地
 *
 *      无论是哪一期都是班级对象,为了便于统一管理,将这些班级对象添加到统一的集合中去
 */
public class Demo_HashMapHashMap {
    public static void main(String args[]){

        //定义48期学生基础班
        HashMap hm48 = new HashMap<>();
        hm48.put(new Student("张三",23),"上海");
        hm48.put(new Student("李四",23),"上海");
        hm48.put(new Student("王五",23),"上海");
        hm48.put(new Student("赵六",23),"上海");

        //定义49期学生基础班
        HashMap hm49 = new HashMap<>();
        hm49.put(new Student("唐僧",30),"大唐");
        hm49.put(new Student("孙悟空",30),"大唐");
        hm49.put(new Student("猪八戒",30),"大唐");
        hm49.put(new Student("沙和尚",30),"大唐");

        //定义统一的集合对象,双列集合嵌套双列集合
        HashMap,String> hm = new HashMap<>();
        hm.put(hm48,"48期学生基础班");
        hm.put(hm49,"49期学生基础班");

        //遍历双列集合
        for(HashMap h : hm.keySet()){       //hm.keySet()代表hm_num
            String hm_value = hm.get(h);                    //hm_value代表hm的value

            //遍历键的双列集合的对象
            for(Student student : h.keySet()){              //h.keySet()代表hm_num的key
                String hm_num_value = h.get(student);       //hm_num_value代表hm_num的value

                System.out.println(student + "=="+ hm_num_value + "==" + hm_value );
            }

        }


    }
}

你可能感兴趣的:(HashMap嵌套HashMap之遍历)