集合间嵌套使用

ArrayList嵌套HashMap

public class Include {
    public static void main(String[] args) {
        HashMap<String, String> hm1 = new HashMap<String, String>();
        hm1.put("周瑜", "小乔");
        hm1.put("吕布", "貂蝉");

        HashMap<String, String> hm2 = new HashMap<String, String>();
        hm2.put("郭靖", "黄蓉");
        hm2.put("杨过", "小龙女");

        ArrayList<HashMap<String, String>> array = new ArrayList<HashMap<String, String>>();
        array.add(hm1);
        array.add(hm2);
        
        // 遍历
        for (HashMap<String, String> hm : array) {
            Set<String> set = hm.keySet();
            for (String key : set) {
                String value = hm.get(key);
                System.out.println(key + "---" + value);
            }
        }
    }
}

输出:
吕布---貂蝉
周瑜---小乔
杨过---小龙女
郭靖---黄蓉

ArrayList 嵌套 HashMap 一般用于将多个键值对集合进行汇总。

HashMap嵌套ArrayList

public class Include {
    public static void main(String[] args) {// 创建集合对象
        ArrayList<String> list1 = new ArrayList<String>();
        list1.add("关羽");
        list1.add("吕布");

        ArrayList<String> list2 = new ArrayList<String>();
        list2.add("孙悟空");
        list2.add("猪八戒");

        HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>();
        hm.put("三国演义", list1);
        hm.put("西游记", list2);

        // 遍历
        Set<String> set = hm.keySet();
        for(String key : set){
            System.out.println(key);
            ArrayList<String> array = hm.get(key);
            for(String value : array){
                System.out.println("\t" + value);
            }
        }
    }
}

输出:
三国演义
 关羽
 吕布
西游记
 孙悟空
 猪八戒

HashMap 嵌套 ArrayList 一般用于将单列集合进行分类,并设置分类的属性。

HashMap嵌套HashMap

public class Include {
    public static void main(String[] args) {
        HashMap<String, String> xyMap = new HashMap<String, String>();
        xyMap.put("孙悟空", "金箍棒");
        xyMap.put("猪八戒", "九齿钉耙");

        HashMap<String, String> sgMap = new HashMap<String, String>();
        sgMap.put("吕布", "方天画戟");
        sgMap.put("关羽", "青龙刀");

        // 把子集合添加到四大名著集合
        HashMap<String, HashMap<String, String>> sdmzMap = new HashMap<String, HashMap<String, String>>();
        sdmzMap.put("西游记", xyMap);
        sdmzMap.put("三国演义", sgMap);

        //遍历集合
        Set<String> sdmzMapSet = sdmzMap.keySet();
        for(String sdmzMapKey : sdmzMapSet){
            System.out.println(sdmzMapKey);
            HashMap<String, String> subHashMap = sdmzMap.get(sdmzMapKey);
            Set<String> keySet = subHashMap.keySet();
            for(String key : keySet){
                String value = subHashMap.get(key);
                System.out.println("\t"+key+":"+value);
            }
        }
    }
}

输出:
三国演义
 关羽:青龙刀
 吕布:方天画戟
西游记
 孙悟空:金箍棒
 猪八戒:九齿钉耙

HashMap 嵌套 HashMap 一般用于将多个键值对集合进行分类,并设置分类的属性。

多层嵌套-HashMap嵌套HashMap嵌套ArrayList

public class HashMapDemo {
    public static void main(String[] args) {
        // 北京地区
        ArrayList<Student> czArray = new ArrayList<Student>();
        czArray.add(new Student("小明", 11));
        czArray.add(new Student("小花", 13));

        ArrayList<Student> gzArray = new ArrayList<Student>();
        gzArray.add(new Student("赵磊", 17));
        gzArray.add(new Student("武鑫", 16));

        HashMap<String, ArrayList<Student>> bjXxMap = new HashMap<String, ArrayList<Student>>();
        bjXxMap.put("初中", czArray);
        bjXxMap.put("高中", gzArray);

        // 上海地区
        ArrayList<Student> czArray2 = new ArrayList<Student>();
        czArray2.add(new Student("小刚", 12));
        czArray2.add(new Student("小强", 14));

        ArrayList<Student> gzArray2 = new ArrayList<Student>();
        gzArray2.add(new Student("安琪", 15));
        gzArray2.add(new Student("博宇", 16));

        HashMap<String, ArrayList<Student>> shXxMap = new HashMap<String, ArrayList<Student>>();
        shXxMap.put("初中", czArray2);
        shXxMap.put("高中", gzArray2);

        // 创建大集合
        HashMap<String, HashMap<String, ArrayList<Student>>> xxMap = new HashMap<String, HashMap<String, ArrayList<Student>>>();
        xxMap.put("北京地区", bjXxMap);
        xxMap.put("上海地区", shXxMap);

        // 遍历集合
        Set<String> xxMapSet = xxMap.keySet();
        for (String xxMapKey : xxMapSet) {
            System.out.println(xxMapKey);
            HashMap<String, ArrayList<Student>> xxMapValue = xxMap
                    .get(xxMapKey);

            Set<String> xxMapValueSet = xxMapValue.keySet();
            for (String xxMapValueKey : xxMapValueSet) {
                System.out.println("\t" + xxMapValueKey);
                ArrayList<Student> xxMapValueValue = xxMapValue
                        .get(xxMapValueKey);
                for (Student s : xxMapValueValue) {
                    System.out.println("\t\t" + s.getName() + "---"
                            + s.getAge());
                }
            }
        }
    }
}

输出:
北京地区
	高中
		赵磊---17
		武鑫---16
	初中
		小明---11
		小花---13
上海地区
	高中
		安琪---15
		博宇---16
	初中
		小刚---12
		小强---14	

多层嵌套一般用于需要多层分类归纳的情况。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会叫的狼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值