Java基础——常用对象API(4):集合框架6:集合的嵌套

本文详细解析了Java中HashMap和ArrayList的嵌套使用方法,包括HashMap嵌套HashMap、HashMap嵌套ArrayList以及ArrayList嵌套HashMap的实例代码和遍历方式,帮助读者深入理解集合嵌套的运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、HashMap集合 嵌套 HashMap集合:

        HashMap<String,HashMap<String,Integer>> gaoyiMap = new HashMap<String,HashMap<String,Integer>>();

        //1班名单
        HashMap<String,Integer> class1Map = new HashMap<String, Integer>();
        class1Map.put("张三",21);
        class1Map.put("李四",21);
        class1Map.put("王五",20);
        class1Map.put("赵六",22);

        //2班名单
        HashMap<String,Integer> class2Map = new HashMap<String, Integer>();
        class2Map.put("张三三",21);
        class2Map.put("李四四",21);
        class2Map.put("王五五",20);
        class2Map.put("赵六六",22);

        //往gaoyiMap加入1班和2班名单
        gaoyiMap.put("class1",class1Map);
        gaoyiMap.put("class2",class2Map);

        Set<Map.Entry<String, HashMap<String, Integer>>> entries = gaoyiMap.entrySet();
        
        for (Map.Entry<String,HashMap<String,Integer>> me : entries) {
            
            //gaoyiMap的值还是HashMap类型,所以对值再进行一次entrySet操作
            Set<Map.Entry<String, Integer>> entrySet2 = me.getValue().entrySet();
            
            for (Map.Entry<String,Integer> me2 : entrySet2) {
                
                System.out.println(me.getKey()+"——"+me2.getKey()+":"+me2.getValue());
            }
        }

————————————————————————————
二、HashMap集合 嵌套 ArrayList集合:

        HashMap<String, ArrayList<String>> bookMap = new HashMap<String,ArrayList<String>>();

        ArrayList<String> book_1 = new ArrayList<String>();
        book_1.add("刘备");
        book_1.add("关羽");
        book_1.add("张飞");

        ArrayList<String> book_2 = new ArrayList<String>();
        book_2.add("宋江");
        book_2.add("武松");
        book_2.add("李逵");

        ArrayList<String> book_3 = new ArrayList<String>();
        book_3.add("孙悟空");
        book_3.add("猪八戒");
        book_3.add("沙和尚");

        bookMap.put("三国演义",book_1);
        bookMap.put("水浒传",book_2);
        bookMap.put("西游记",book_3);

        Set<Map.Entry<String,ArrayList<String>>> entries = bookMap.entrySet();

        for (Map.Entry<String,ArrayList<String>> me : entries) {

            for (String s : me.getValue()) {

                System.out.println(me.getKey()+"-"+s);
            }
        }

——————————————————————————————————
三、 ArrayList集合 嵌套 HashMap集合:

        ArrayList<HashMap<String,Integer>> array = new ArrayList<HashMap<String, Integer>>();

        HashMap<String,Integer> h_1 = new HashMap<String, Integer>();
        h_1.put("孙悟空",19);
        h_1.put("猪八戒",30);
        h_1.put("沙和尚",30);

        HashMap<String,Integer> h_2 = new HashMap<String, Integer>();
        h_2.put("宋江",34);
        h_2.put("李逵",29);
        h_2.put("武松",28);

        array.add(h_1);
        array.add(h_2);

        for (HashMap<String,Integer> hashMap: array) {

            Set<Map.Entry<String,Integer>> entries = hashMap.entrySet();

            for (Map.Entry<String,Integer> me : entries) {

                System.out.println(me.getKey()+":"+me.getValue());
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值