尚硅谷Java笔记-集合嵌套

一、List嵌套List

    public static void main(String[] args) {
        List<List<String>> listList = new ArrayList<>();
        ArrayList<String> strings1 = new ArrayList<>();
        ArrayList<String> strings2 = new ArrayList<>();
        Collections.addAll(strings2,"nihao","wohao","dajiahao");
        Collections.addAll(strings1,"xiaohua","xiaocao","pugongying");
        Collections.addAll(listList,strings2,strings1);
        System.out.println(listList);
        for (List<String> strings : listList) {
            for (String string : strings) {
                System.out.println(string);
            }
        }
    }

二、List嵌套Map

    private static void fun3() {
        HashMap<Integer, String> map1 = new HashMap<>();
        HashMap<Integer, String> map2 = new HashMap<>();
        map1.put(1,"张三");
        map1.put(2,"李四");
        map1.put(3,"王五");
        map2.put(1,"Hyunjin");
        map2.put(2,"Felix");
        map2.put(3,"Leeknow");
        List<HashMap<Integer,String>> list=new ArrayList<>();
        list.add(map1);
        list.add(map2);
        System.out.println(list);
        for (HashMap<Integer, String> map : list) {
            Set<Map.Entry<Integer, String>> entries = map.entrySet();
            for (Map.Entry<Integer, String> entry : entries) {
                Integer key = entry.getKey();
                String value = entry.getValue();
                System.out.println(key+"="+value);
            }
        }
    }

 

三、Map嵌套Map

    private static void fun4() {
        HashMap<Integer, String> map1 = new HashMap<>();
        HashMap<Integer, String> map2 = new HashMap<>();
        map1.put(1,"张三");
        map1.put(2,"李四");
        map1.put(3,"王五");
        map2.put(1,"Hyunjin");
        map2.put(2,"Felix");
        map2.put(3,"Leeknow");
        HashMap<String, HashMap<Integer, String>> stringHashMapHashMap = new HashMap<>();
        stringHashMapHashMap.put("javase",map1);
        stringHashMapHashMap.put("javaee",map2);
        Set<Map.Entry<String, HashMap<Integer, String>>> entries = stringHashMapHashMap.entrySet();
        for (Map.Entry<String, HashMap<Integer, String>> entry : entries) {
            String key = entry.getKey();
            HashMap<Integer, String> value = entry.getValue();
            Set<Map.Entry<Integer, String>> entries1 = value.entrySet();
            for (Map.Entry<Integer, String> integerStringEntry : entries1) {
                Integer key1 = integerStringEntry.getKey();
                String value1 = integerStringEntry.getValue();
                System.out.println(key+"..."+key1+"..."+value1);
            }
        }
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值