Java-集合的嵌套案例

一.集合嵌套之HashMap嵌套HashMap
集合嵌套之HashMap嵌套HashMap

 软件
			基础班
				张三		20
				李四		22
			就业班
				王五		21
				赵六		23
				
public class MyDemo2 {
public static void main(String[] args) {
    //基础班
    //    张三 20
    //    李四 22
    //就业班
    //    王五 21
    //    赵六 23
    //HashMap 嵌套 HashMap

    HashMap<String, Integer> jc = new HashMap<>();
    jc.put("张三", 20);
    jc.put("李四", 22);
    HashMap<String, Integer> jy = new HashMap<>();
    jy.put("王五", 21);
    jy.put("赵六", 23);
    HashMap<String, HashMap<String, Integer>> maxMap = new HashMap<>();
    maxMap.put("基础班", jc);
    maxMap.put("就业班", jy);
    //遍历这个大集合
    Set<String> keys = maxMap.keySet();
    for(String key:keys){
        System.out.println(key);
        HashMap<String, Integer> map = maxMap.get(key);
        Set<String> strings = map.keySet();
        for(String k:strings){
            System.out.println("\t"+k+"  "+map.get(k));
        }
        System.out.println();
    }


}

}
在这里插入图片描述
二.集合嵌套之HashMap嵌套ArrayList
案例演示
集合嵌套之HashMap嵌套ArrayList

	三国演义
	 	吕布
	 	周瑜
	 笑傲江湖
	 	令狐冲
	 	林平之
	 神雕侠侣
	 	郭靖
	 	杨过  
public class MapDemo3 {
public static void main(String[] args) {
            //三国演义
            //    吕布
            //    周瑜
            //笑傲江湖
            //    令狐冲
            //    林平之
            //神雕侠侣
            //     郭靖
            //     杨过

    //HashMap 嵌套List
    ArrayList<String> sg = new ArrayList<>();
    sg.add("吕布");
    sg.add("周瑜");
    ArrayList<String> xa = new ArrayList<>();
    xa.add("令狐冲");
    xa.add("林平之");
    ArrayList<String> sd = new ArrayList<>();
    sd.add("郭靖");
    sd.add("杨过");
    HashMap<String, ArrayList<String>> map = new HashMap<>();
    map.put("三国演义",sg);
    map.put("笑傲江湖",xa);
    map.put("神雕侠侣",sd);

    //遍历
    Set<Map.Entry<String, ArrayList<String>>> entries = map.entrySet();
    for (Map.Entry<String, ArrayList<String>> entry : entries) {
        System.out.println(entry.getKey());
        ArrayList<String> value = entry.getValue();
        for (String s : value) {
            System.out.println("\t"+s);
        }
        System.out.println();
    }


}

}
在这里插入图片描述
三.集合嵌套之ArrayList嵌套HashMap
案例演示
假设ArrayList集合的元素是HashMap。有3个。
每一个HashMap集合的键和值都是字符串。

	 周瑜---小乔
	 吕布---貂蝉

	 郭靖---黄蓉
	 杨过---小龙女

	 令狐冲---任盈盈
	 林平之---岳灵珊
public class MapDemo4 {
public static void main(String[] args) {
    //周瑜-- - 小乔
    //吕布-- - 貂蝉
    //
    //郭靖-- - 黄蓉
    //杨过-- - 小龙女
    //
    //令狐冲-- - 任盈盈
    //林平之-- - 岳灵珊
    // List 嵌套 HashMap
   HashMap<String, String> one = new HashMap<>();
    one.put("周瑜", "小乔");
    one.put("吕布", "貂蝉");
    HashMap<String, String> two = new HashMap<>();
    two.put("郭靖", "黄蓉");
    two.put("杨过", "小龙女");
    HashMap<String, String> three = new HashMap<>();
    three.put("令狐冲", "任盈盈");
    three.put("林平之", "岳灵珊");

    ArrayList<HashMap<String, String>> list = new ArrayList<>();
    list.add(one);
    list.add(two);
    list.add(three);

    for (HashMap<String, String> map : list) {
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for(Map.Entry<String, String> en:entries){

            System.out.println(en.getKey()+"-----"+en.getValue());
        }
        System.out.println();

    }


}

}
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/PreyHard/article/details/83628669