Map嵌套和ArrayList嵌套Map(转)

集合嵌套之HashMap嵌套HashMap

基础班
张三 20
李四 22
就业班
王五 21
赵六 23

public class Test5 {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("张三",20);
        map.put("李四",22);
        HashMap<String, Integer> map1 = new HashMap<>();
        map1.put("王五",21);
        map1.put("赵六",23);
        HashMap<String, HashMap<String, Integer>> mapmax = new HashMap<>();
        mapmax.put("基础班",map);
        mapmax.put("就业班",map1);
        Set<Map.Entry<String, HashMap<String, Integer>>> entries = mapmax.entrySet();
        for (Map.Entry<String, HashMap<String, Integer>> entry : entries) {
            System.out.println(entry.getKey());
            Set<Map.Entry<String, Integer>> entries1 = entry.getValue().entrySet();
            for (Map.Entry<String, Integer> stringIntegerEntry : entries1) {
                System.out.println("\t"+stringIntegerEntry.getKey()+"     "+stringIntegerEntry.getValue());
            }
        }
    }
}

Arraylist嵌套HashMap

public class Test3 {
    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<>();
        map.put("周瑜","小乔");
        map.put("吕布","貂蝉");
        HashMap<String, String> map1 = new HashMap<>();
        map1.put("郭靖","黄蓉");
        map1.put("杨过","小龙女");
        HashMap<String, String> map2 = new HashMap<>();
        map2.put("令狐冲","任盈盈");
        map2.put("林平之","岳灵珊");
        ArrayList<HashMap<String, String>> list = new ArrayList<>();
        list.add(map);
        list.add(map1);
        list.add(map2);
        for (HashMap<String, String> s : list) {
            Set<String> strings = s.keySet();
            for (String s1 : strings) {
                String s2 = s.get(s1);
                System.out.println("\t"+s1+"---"+s2);
            }
            System.out.println();
        }
    }
}

————————————————
版权声明:本文为CSDN博主「taraex」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/taraex/article/details/90243965

posted @ 2022-01-14 09:43  freedomAndWind  阅读(168)  评论(0编辑  收藏  举报