JAVA基础(HashMap嵌套HashMap)

1,集合嵌套之HashMap嵌套HashMap


 

public class Demo8_HashMapHashMap {





    /**

     * * A:案例演示

     * 集合嵌套之HashMap嵌套HashMap

     *

     * 需求:

     * A班定义成一个双列集合,键是学生对象,值是学生的归属地

     *B班定义成一个双列集合,键是学生对象,值是学生的归属地

     *

  

     */

    public static void main(String[] args) {

  

        HashMap xiaoshuai1 = new HashMap<>();

        xiaoshuai1 .put(new Student("张三", 23), "北京");

        xiaoshuai1 .put(new Student("李四", 24), "北京");

        xiaoshuai1 .put(new Student("王五", 25), "上海");

        xiaoshuai1 .put(new Student("赵六", 26), "广州");

        



        HashMap xiaoshuai2= new HashMap<>();

        xiaoshuai2.put(new Student("唐僧", 1023), "北京");

        xiaoshuai2.put(new Student("孙悟空",1024), "北京");

        xiaoshuai2.put(new Student("猪八戒",1025), "上海");

        xiaoshuai2.put(new Student("沙和尚",1026), "广州");

        

    

        HashMap, String> xiaoshuai3= new HashMap<>();

        xiaoshuai3.put(xiaoshuai1, "A班");

        xiaoshuai3.put(xiaoshuai2, "B班");

        

        //遍历双列集合

        for(HashMap h : xiaoshuai3.keySet()) {        //hm.keySet()代表的是双列集合中键的集合

            String value = hm.get(h);                        //get(h)根据键对象获取值对象

            //遍历键的双列集合对象

            for(Student key : h.keySet()) {                    //h.keySet()获取集合总所有的学生键对象

                String value2 = h.get(key);

                

                System.out.println(key + "=" + value2 + "=" + value);

            }

        }

        

    }





}

 

你可能感兴趣的:(JAVA基础,JAVA基础)