42-8集合嵌套

29 阅读5分钟

List嵌套List

  需求:创建3List集合,每个集合中分别存储一些字符串,将3List集合存储到另一个List集合中。

    实现步骤:
        1.创建List集合对象bigList,代表楼层(存储多间教室)(小容器),泛型: List<String>
        2.创建三个List集合对象,代表三个教室(小容器),泛型: String
        3.分别向三个小List集合对象中添加数据
        4.把三个小List集合存储到大的List集合对象中
        5.遍历(迭代器/增强for)大的List集合对象bigList
        6.获取到大List集合对象中的当前的小List集合对象smallList
        7.遍历(迭代器/增强for)小的List集合对象smallList
*/
public class Demo03ListList {
    public static void main(String[] args) {
        //1.创建List集合对象bigList,代表楼层(存储多间教室)(小容器),泛型: List<String>
        List<List<String>> bigList = new ArrayList<>();

        //2.创建三个List集合对象,代表三个教室(小容器),泛型: String
        List<String> smallList01 = new ArrayList<>();
        List<String> smallList02 = new ArrayList<>();
        List<String> smallList03 = new ArrayList<>();

        //3.分别向三个小List集合对象中添加数据
        Collections.addAll(smallList01,"张三","李四","王五");
        Collections.addAll(smallList02,"赵六","孙七","马伊琍");
        Collections.addAll(smallList03,"宝强","柳岩","小涛子");

        //4.把三个小List集合存储到大的List集合对象中
        bigList.add(smallList01);
        bigList.add(smallList02);
        bigList.add(smallList03);

        //Collections.addAll(bigList,smallList01,smallList02,smallList03);

        //5.遍历(迭代器)大的List集合对象bigList
        Iterator<List<String>> bigIt = bigList.iterator();//获取大集合的迭代器对象

        while(bigIt.hasNext()) {
            //6.获取到大List集合对象中的当前的小List集合对象smallList
            List<String> smallList = bigIt.next();
            //7.遍历(迭代器)小的List集合对象smallList
            Iterator<String> smallIt = smallList.iterator();//获取小集合的迭代器对象
            while (smallIt.hasNext()) {
                System.out.println(smallIt.next());
            }
            System.out.println("-----------------");
        }
    }
}

List嵌套Map

List嵌套Map
        需求:
        	1班级有第三名同学,学号和姓名分别为:
        		001=张三,002=李四,003=王五
        	2班有三名同学,学号和姓名分别为:
        		001=黄晓明,002=杨颖,003=刘德华,004=朱丽倩
        请将同学的信息以键值对的形式存储到2Map集合中,在将2Map集合存储到List集合中。
        
    实现步骤:
        1.创建List集合list,代表楼层(多间教室,大容器),泛型: Map<String,String>
        2.创建三个Map集合对象,分别代表教室(小容器),泛型: String,String
        3.分别向三个Map集合对象中添加键值对
        4.把三个Map集合对象添加到List集合对象中(把三间教室,装入楼层中)
        5.遍历(迭代器/增强for)List集合对象
        6.获取到当前的Map集合对象
 public class Demo04ListMap {
    public static void main(String[] args) {
        //1.创建List集合list,代表楼层(多间教室,大容器),泛型: Map<String,String>
        List<Map<String,String>> list = new ArrayList<>();
        //2.创建三个Map集合对象,分别代表教室(小容器),泛型: String,String
        Map<String,String> map01 = new HashMap<>();
        Map<String,String> map02 = new HashMap<>();
        Map<String,String> map03 = new HashMap<>();
        //3.分别向三个Map集合对象中添加键值对
        map01.put("001","张三");
        map01.put("002","李四");
        map01.put("003","王五");
        map02.put("001","黄晓明");
        map02.put("002","杨颖");
        map02.put("003","刘德华");
        map03.put("001","小涛子");
        map03.put("002","小男子");
        map03.put("003","小程子");
        //4.把三个Map集合对象添加到List集合对象中(把三间教室,装入楼层中)
        list.add(map01);
        list.add(map02);
        list.add(map03);
        //Collections.addAll(list,map01,map02,map03);
        //5.遍历(迭代器)List集合对象
        Iterator<Map<String, String>> bigIt = list.iterator();
        while (bigIt.hasNext()) {
            //6.获取到当前的Map集合对象
            Map<String, String> map = bigIt.next();
            //7.遍历(迭代器)当前的Map集合对象(keySet方法)
            Set<String> set = map.keySet();
            Iterator<String> smallIt = set.iterator();
            while (smallIt.hasNext()) {
                String key = smallIt.next();//键
                //根据键获取值
                String value = map.get(key);
                System.out.println(key+"::::"+value);
            }
            System.out.println("----------------------");
        }
    }
}       7.遍历(迭代器/增强for)当前的Map集合对象(keySet/entrySet方法)
        

Map嵌套Map

 需求:有以下数据结构,使用集合存储。
        java基础班 集合 存储的是 学号 键,值学生姓名
            001 张三
            002 李四
        java就业班
            001 王五
            002 赵柳

        实现步骤:
            1.创建大Map集合对象bigMap,代表楼层(大容器),
                泛型: 键 String(教室名字) 值 Map<String,String>(教室中的学生)

            2.创建两个小Map集合对象,代表教室(小容器),泛型: 键: String 值: String
            3.分别向两个小Map集合对象中存储键值对数据
            4.把两个小Map集合对象存储到大的Map集合对象中,指定键: String(教室名字)
            5.遍历(迭代器/增强for)大Map集合对象(keySet/entrySet方法)
            6.获取到当前的小Map集合对象
            7.遍历(迭代器/增强for)小Map集合对象(keySet/entrySet方法)
  public class Demo05MapMap {
    public static void main(String[] args) {
        //1.创建大Map集合对象bigMap,代表楼层(大容器),泛型: 键 String(教室名字) 值 Map<String,String>(教室中的学生)
        Map<String,Map<String,String>> bigMap = new HashMap<>();

        //2.创建两个小Map集合对象,代表教室(小容器),泛型: 键: String 值: String
        Map<String,String> smallMap01 = new HashMap<>();
        Map<String,String> smallMap02 = new HashMap<>();

        //3.分别向两个小Map集合对象中存储键值对数据
        smallMap01.put("001","黄晓明");
        smallMap01.put("002","杨颖");
        smallMap02.put("001","张三");
        smallMap02.put("002","李四");

        //4.把两个小Map集合对象存储到大的Map集合对象中,指定键: String(教室名字)
        bigMap.put("JavaSE",smallMap01);
        bigMap.put("JavaEE",smallMap02);

        //System.out.println(bigMap);

        //5.遍历(迭代器)大Map集合对象(entrySet方法)
        //获取大Map集合对象的所有键值对组成的Set集合
        Set<Map.Entry<String,Map<String,String>>> bigSet = bigMap.entrySet();        
        Iterator<Map.Entry<String,Map<String,String>>> bigIt = bigSet.iterator();
        while(bigIt.hasNext()) {
            //获取到大Map集合中的一个Entry对象
            Map.Entry<String,Map<String,String>> bigEntry = bigIt.next();            
            String className = bigEntry.getKey();//获取键,班级名称
            System.out.println(className+" 班级有如下学生: ");
            //6.获取到当前的小Map集合对象
            Map<String,String> smallMap = bigEntry.getValue();
            //7.遍历(迭代器)小Map集合对象(entrySet方法)
            //获取到小Map集合中的一个Entry对象组成的Set集合
            Set<Map.Entry<String,String>> smallSet = smallMap.entrySet();            
            //获取到小Map集合中的一个Entry对象组成的Set集合的迭代器对象
            Iterator<Map.Entry<String, String>> smallIt = smallSet.iterator();
            while(smallIt.hasNext()) {
                //获取到小Map集合中的一个Entry对象
                Map.Entry<String, String> smallEntry = smallIt.next();
                String id = smallEntry.getKey();
                String name = smallEntry.getValue();
                System.out.println(id+"="+name);
            }
            System.out.println("-----------------------");
        }
    }
}