• List,Map多层循环嵌套Demo及其理解



    博主 默语带您 Go to New World.
    个人主页—— 默语 的博客👦🏻
    《java 面试题大全》
    🍩惟余辈才疏学浅,临摹之作或有不妥之处,还请读者海涵指正。☕🍭
    《MYSQL从入门到精通》数据库是开发者必会基础之一~
    🪁 吾期望此文有资助于尔,即使粗浅难及深广,亦备添少许微薄之助。苟未尽善尽美,敬请批评指正,以资改进。!💻⌨

    我这边在之前写代码遇到多层(n*n)层循环嵌套逻辑的时候很头疼今天写一些demo来帮自己更好的理解相关内容

    我这边直接上代码:
    希望大家可以通过demo更好的理解他们之间的转换循环与嵌套逻辑;


    ☀️相关笔记章节:
    🌹java 1.8 stream使用总结(个人总结有一些经典文章的集合)
    🌹遍历 ArrayList和遍历 Map的几种方式
    🌹Java对象,Map,List,Set数组等相互转换大全(详细讲解,附代码,讲解案例)
    🌹List,Map多层循环嵌套Demo及其理解
    🌹List,Map 三种遍历方式:(总结理解)
    🌹List<Map<String, Object>>,Map<String,List<Map<String, Object>>>多方式循环遍历

    在这里插入图片描述
    来个小姐姐赏心悦目下 哈哈 ;


    先简答贴下POM

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.7.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example.DemoCrud</groupId>
        <artifactId>democurd</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>democurd</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <!-- mybatis整合Springboot -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.0</version>
            </dependency>
    
            <dependency>
                <!--           maven 添加json-->
                <groupId>net.sf.json-lib</groupId>
                <artifactId>json-lib</artifactId>
                <version>2.4</version>
            </dependency>
            <!-- 引入org.json所需依赖 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.70</version>
            </dependency>
            <dependency>
                <groupId>org.webjars</groupId>
                <artifactId>jquery</artifactId>
                <version>3.3.0</version>
            </dependency>
            <!--bootstrap-webjar-->
            <dependency>
                <groupId>org.webjars</groupId>
                <artifactId>bootstrap</artifactId>
                <version>4.0.0</version>
            </dependency>
            <!--配置文件注入时使用后会有提示-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.18</version>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.3.1</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.12</version>
                <scope>provided</scope>
            </dependency>
            <!-- https://mvnrepository.com/artifact/oracle/oracle-jdbc -->
            <dependency>
                <groupId>com.oracle.ojdbc</groupId>
                <artifactId>ojdbc8</artifactId>
                <version>19.3.0.0</version>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.47</version>
            </dependency>
    
    
            <!-- Druid 数据源依赖,集成了 Spring boot ,方便配置 druid 属性 -->
            <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>1.2.8</version>
            </dependency>
    
            <!-- Mysql 数据库驱动,spring boot 2.1.3 时,mysql 驱动版本为 8.0.15 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
    
            <!-- 引入 Spring Data JPAJPA 组件内部会依赖 JDBC 组件,JDBC 组件内部默认依赖的是 HikariCP 数据源-->
            <!--切换 druid 数据源时,HikariCP 依赖可以移除掉,不移除时也不影响-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
    
            <!--引用 log4j2 spring boot 启动器,内部依赖了 slf4j、log4j;排除项目中的 logback-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j2</artifactId>
            </dependency>
    
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169

    公共实体类

    @Data
    @Builder
    public class Student {
        private Integer age;
        private String name;
        private Boolean sex; //true:男  false:女
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    单层循环数据存取List,Map

    package com.example.democrud.democurd.Prototype.demo01;
    
    import lombok.Builder;
    import lombok.Data;
    import org.thymeleaf.expression.Lists;
    
    import java.util.*;
    import java.util.stream.Collectors;
    
    public class ListAndMapDemo {
    
        public static void main(String[] args) {
            List<Student> list = stduentInit();
            //取出年龄大于等于20岁的
             getAge20(list);
    
            //拼接字符串 马丹1,马丹2,马丹3,马丹4,马丹5
            // getStringName(list);
    
            //拼接字符串取年龄 10,15,20,25,30
            //  getAge(list);
    
            //根据年龄分组 10,10,20,20, ....
            //ageLimi(list);
        }
    
        /**
         * 根据年龄分组 10,10,20,20, ....
         *
         * @param list
         */
        public static void ageLimi(List<Student> list) {
            //根据年龄分组
            Map<String, List<Student>> map = new HashMap<>();
            for (Student student : list) {
                List<Student> students = map.get(student.getAge().toString());
                if (students == null) {
                    students = new ArrayList<>();
                }
                students.add(student);
                map.put(student.getAge().toString(), students);
            }
            System.out.println("优化前:" + map);
    
            Map<Integer, List<Student>> collect = list.stream().collect(Collectors.groupingBy(Student::getAge));
            System.out.println("优化前:" + collect);
        }
    
    
        /**
         * 拼接字符串取年龄 10,15,20,25,30
         *
         * @param list
         */
        public static void getAge(List<Student> list) {
            // 无法处理空
            //  String collect = list.stream().map(v -> v.getAge().toString()).collect(Collectors.joining(","));
            //  System.out.println("无法处理空"+collect);
    
    
            String collect1 = list.stream().filter(v -> v.getAge() != null).map(Student::getAge).map(String::valueOf).collect(Collectors.joining(","));
            System.out.println("可以处理优化前" + collect1);
    
            String collect2 = list.stream().filter(v -> !Objects.isNull(v.getAge())).map(Student::getAge).map(String::valueOf).collect(Collectors.joining(","));
            System.out.println("可以处理优化后" + collect2);
    
        }
    
    
        /**
         * 拼接字符串 马丹1,马丹2,马丹3,马丹4,马丹5
         *
         * @param list
         */
        public static void getStringName(List<Student> list) {
            StringBuffer buffer = new StringBuffer();
            for (Student student : list) {
                buffer.append(student.getName());
                buffer.append(",");
            }
            String substring = buffer.toString().substring(0, buffer.toString().length() - 1);
            System.out.println("优化前:" + substring);
    
            String collect = list.stream().map(v -> v.getName()).collect(Collectors.joining(","));
            System.out.println("优化后:" + collect);
        }
    
        /**
         * 取出年龄大于等于20岁的
         */
        public static void getAge20(List<Student> list) {
            //实现一
            int ages = 20;
            List<Student> list1 = new ArrayList<Student>();
    
            for (Student student5 : list) {
                if (student5.getAge() >= ages) {
                    list1.add(student5);
                }
            }
            System.out.println("优化前:" + list1);
    
            //实现二
            list = list.stream().filter(v -> v.getAge() >= 20).collect(Collectors.toList());
            System.out.println("优化后:" + list);
        }
    
        /**
         * 初始化list对象
         */
        public static List<Student> stduentInit() {
            List<Student> list = new ArrayList<Student>();
            //  Student student = new Student(null, "马丹1", true);
            Student student = new Student(10, "马丹1", true);
            Student student1 = new Student(20, "马丹2", false);
            Student student2 = new Student(10, "马丹3", true);
            Student student3 = new Student(10, "马丹4", false);
            Student student4 = new Student(20, "马丹5", true);
            list.add(student);
            list.add(student1);
            list.add(student2);
            list.add(student3);
            list.add(student4);
            return list;
    
        }
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130

    运行效果一一进行展示:

    1.取出年龄大于等于20岁的

     //取出年龄大于等于20岁的
            getAge20(list);     
    
    • 1
    • 2
    List原始数据==>[Student(age=10, name=马丹1, sex=true), Student(age=20, name=马丹2, sex=false), Student(age=10, name=马丹3, sex=true), Student(age=10, name=马丹4, sex=false), Student(age=20, name=马丹5, sex=true)]
    优化前:[Student(age=20, name=马丹2, sex=false), Student(age=20, name=马丹5, sex=true)]
    优化后:[Student(age=20, name=马丹2, sex=false), Student(age=20, name=马丹5, sex=true)]
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    2.拼接字符串 马丹1,马丹2…

     //拼接字符串 马丹1,马丹2,马丹3,马丹4,马丹5
             getStringName(list);
    
    • 1
    • 2
    List原始数据==>[Student(age=10, name=马丹1, sex=true), Student(age=20, name=马丹2, sex=false), Student(age=10, name=马丹3, sex=true), Student(age=10, name=马丹4, sex=false), Student(age=20, name=马丹5, sex=true)]
    优化前:马丹1,马丹2,马丹3,马丹4,马丹5
    优化后:马丹1,马丹2,马丹3,马丹4,马丹5
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    3. 拼接字符串取年龄 10,15,20,25,30

      //拼接字符串取年龄 10,15,20,25,30
             getAge(list);
    
    • 1
    • 2
    List原始数据==>[Student(age=10, name=马丹1, sex=true), Student(age=20, name=马丹2, sex=false), Student(age=10, name=马丹3, sex=true), Student(age=10, name=马丹4, sex=false), Student(age=20, name=马丹5, sex=true)]
    可以处理优化前10,20,10,10,20
    可以处理优化后10,20,10,10,20
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    4.0根据年龄分组 10,10,20,20, …

     //根据年龄分组 10,10,20,20, ....
            ageLimi(list);
    
    • 1
    • 2
    List原始数据==>[Student(age=10, name=马丹1, sex=true), Student(age=20, name=马丹2, sex=false), Student(age=10, name=马丹3, sex=true), Student(age=10, name=马丹4, sex=false), Student(age=20, name=马丹5, sex=true)]
    可以处理优化前10,20,10,10,20
    可以处理优化后10,20,10,10,20
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    多层循环嵌套

    package com.example.democrud.democurd.Prototype.demo01;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    
    import java.util.*;
    import java.util.stream.Collectors;
    
    public class ListAndMap1Demo {
    
        public static void main(String[] args) {
    
          // 循环中获取数据
        //  stduentgetName();
    
         //处理异常嵌套数据
              stduents();
    
    
    
        }
        public static void stduentgetName() {
    
            Map<String, List<Student>> map = stduentInit();
            //System.out.println(JSONObject.toJSON(map));
    
    
            for (Map.Entry<String, List<Student>> entry : map.entrySet()) {
                String key = entry.getKey();
                List<Student> list = entry.getValue();
                System.out.println("key===>"+key);
                System.out.println("list===>"+list);
    
                List<String> collect = list.stream().map(Student::getName).collect(Collectors.toList());
                System.out.println("list: 集合数组==》" + collect);
                String collect1 = list.stream().map(Student::getName).collect(Collectors.joining(","));
                System.out.println("string: String数组==》" + collect1);
    
                System.out.println("--------------------------------------1---------------------------------------------");
                List<Student> collectle = list.stream().filter(v -> v.getAge() >= 20).collect(Collectors.toList());
                System.out.println("collectle==>"+collectle);
    
    
    
                System.out.println("---------------------------------2--------------------------------------------------");
                List<String> strList = new ArrayList<>();
                for (Student stu : list) {
                    strList.add(stu.getName());
                }
                System.out.println("strList: " + strList);
    
            }
    
    
        }
    
        /**
         * 处理深层嵌套异常数据
         */
        public static void stduents() {
    
            List<Map<String, String>> list = new ArrayList<>();
            String str = "{\"sheets\":[{\"datas\":[{\"cells\":[{\"dataType\":2,\"value\":\"闫文超\"},{\"dataType\":1,\"value\":\"10.0\"}]},{\"cells\":[{\"dataType\":2,\"value\":\"老王\"},{\"dataType\":1,\"value\":\"20.0\"}]}],\"name\":\"Sheet1\",\"titles\":[\"Name\",\"AGE\"]}]}";
            JSONObject js = JSONObject.parseObject(str);
            //System.out.println(js);
            String sheets = js.get("sheets").toString();
            //System.out.println(sheets);
            JSONArray objects = JSONArray.parseArray(sheets);
            //System.out.println(objects);
            for (Object object : objects) {
                JSONObject js111 = JSONObject.parseObject(object.toString());
                //System.out.println(js111);
                String datas = js111.get("datas").toString();
                //System.out.println(datas);
                JSONArray objects1 = JSONArray.parseArray(datas);
                //System.out.println(objects1);
                for (Object o : objects1) {
                    JSONObject js1111 = JSONObject.parseObject(o.toString());
                    //System.out.println(js1111);
                    String cells = js1111.get("cells").toString();
                    //System.out.println(cells);
                    JSONArray objects2 = JSONArray.parseArray(cells);
                    //System.out.println("objects2 : "+ objects2);
                    for (Object o1 : objects2) {
                        JSONObject js11111 = JSONObject.parseObject(o1.toString());
                        //System.out.println(js11111);
                        String dataType = js11111.get("dataType").toString();
                        String value = js11111.get("value").toString();
                        Map<String, String> map = new HashMap<>();
                        map.put(dataType, value);
                        list.add(map);
                        //System.out.println("这他妈那个傻逼写的 dataType=" + dataType + "  value="+ value);
                    }
    
                }
            }
    
            System.out.println(list);
    
        }
    
    
    
    
    
        /**
         * 初始化list对象
         */
        public static Map<String, List<Student>> stduentInit() {
    
            Map<String, List<Student>> map = new HashMap<>();
            List<Student> list = new ArrayList<>();
            Student student = new Student(10, "马丹1", true);
            Student student1 = new Student(15, "马丹2", false);
            Student student2 = new Student(20, "马丹3", true);
            Student student3 = new Student(25, "马丹4", false);
            Student student4 = new Student(30, "马丹5", true);
            list.add(student);
            list.add(student1);
            list.add(student2);
            list.add(student3);
            list.add(student4);
            map.put("list", list);
    
            List<Student> list1 = new ArrayList<>();
            Student stu = new Student(10, "马丹1", true);
            Student stu1 = new Student(15, "马丹2", false);
            Student stu2 = new Student(20, "马丹3", true);
            Student stu3 = new Student(25, "马丹4", false);
            Student stu4 = new Student(30, "马丹5", true);
            list1.add(stu);
            list1.add(stu1);
            list1.add(stu2);
            list1.add(stu3);
            list1.add(stu4);
            map.put("list1", list1);
            return map;
        }
    
    
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145

    1.处理异常嵌套数据

    //处理异常嵌套数据
              stduents();
    
    • 1
    • 2

    此处不理解的可以把sout注释取下;每步都有打印我这边为了更好的展示就先注释了;

    [{2=闫文超}, {1=10.0}, {2=老王}, {1=20.0}]
    
    • 1

    在这里插入图片描述

    2.循环中获取数据

      // 循环中获取数据
            stduentgetName();
    
    • 1
    • 2
    key===>list1
    list===>[Student(age=10, name=马丹1, sex=true), Student(age=15, name=马丹2, sex=false), Student(age=20, name=马丹3, sex=true), Student(age=25, name=马丹4, sex=false), Student(age=30, name=马丹5, sex=true)]
    list: 集合数组==[马丹1, 马丹2, 马丹3, 马丹4, 马丹5]
    string: String数组==》马丹1,马丹2,马丹3,马丹4,马丹5
    --------------------------------------1---------------------------------------------
    collectle==>[Student(age=20, name=马丹3, sex=true), Student(age=25, name=马丹4, sex=false), Student(age=30, name=马丹5, sex=true)]
    ---------------------------------2--------------------------------------------------
    strList: [马丹1, 马丹2, 马丹3, 马丹4, 马丹5]
    key===>list
    list===>[Student(age=10, name=马丹1, sex=true), Student(age=15, name=马丹2, sex=false), Student(age=20, name=马丹3, sex=true), Student(age=25, name=马丹4, sex=false), Student(age=30, name=马丹5, sex=true)]
    list: 集合数组==[马丹1, 马丹2, 马丹3, 马丹4, 马丹5]
    string: String数组==》马丹1,马丹2,马丹3,马丹4,马丹5
    --------------------------------------1---------------------------------------------
    collectle==>[Student(age=20, name=马丹3, sex=true), Student(age=25, name=马丹4, sex=false), Student(age=30, name=马丹5, sex=true)]
    ---------------------------------2--------------------------------------------------
    strList: [马丹1, 马丹2, 马丹3, 马丹4, 马丹5]
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    欧克;演示完成后续有其他部分会补充;此处感谢吊毛同学的交流;我这边在之前写代码遇到多层(n*n)层循环嵌套逻辑的时候很头疼今天写一些demo来帮自己更好的理解相关内容

    我这边直接上代码:
    希望大家可以通过demo更好的理解他们之间的转换循环与嵌套逻辑;

    ☀️相关笔记章节:
    🌹java 1.8 stream使用总结(个人总结有一些经典文章的集合)
    🌹遍历 ArrayList和遍历 Map的几种方式
    🌹Java对象,Map,List,Set数组等相互转换大全(详细讲解,附代码,讲解案例)
    🌹List,Map多层循环嵌套Demo及其理解
    🌹List,Map 三种遍历方式:(总结理解)
    🌹List<Map<String, Object>>,Map<String,List<Map<String, Object>>>多方式循环遍历

    在这里插入图片描述
    来个小姐姐赏心悦目下 哈哈 ;

    先简答贴下POM

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.7.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example.DemoCrud</groupId>
        <artifactId>democurd</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>democurd</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <!-- mybatis整合Springboot -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.0</version>
            </dependency>
    
            <dependency>
                <!--           maven 添加json-->
                <groupId>net.sf.json-lib</groupId>
                <artifactId>json-lib</artifactId>
                <version>2.4</version>
            </dependency>
            <!-- 引入org.json所需依赖 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.70</version>
            </dependency>
            <dependency>
                <groupId>org.webjars</groupId>
                <artifactId>jquery</artifactId>
                <version>3.3.0</version>
            </dependency>
            <!--bootstrap-webjar-->
            <dependency>
                <groupId>org.webjars</groupId>
                <artifactId>bootstrap</artifactId>
                <version>4.0.0</version>
            </dependency>
            <!--配置文件注入时使用后会有提示-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.18</version>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.3.1</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.12</version>
                <scope>provided</scope>
            </dependency>
            <!-- https://mvnrepository.com/artifact/oracle/oracle-jdbc -->
            <dependency>
                <groupId>com.oracle.ojdbc</groupId>
                <artifactId>ojdbc8</artifactId>
                <version>19.3.0.0</version>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.47</version>
            </dependency>
    
    
            <!-- Druid 数据源依赖,集成了 Spring boot ,方便配置 druid 属性 -->
            <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>1.2.8</version>
            </dependency>
    
            <!-- Mysql 数据库驱动,spring boot 2.1.3 时,mysql 驱动版本为 8.0.15 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
    
            <!-- 引入 Spring Data JPAJPA 组件内部会依赖 JDBC 组件,JDBC 组件内部默认依赖的是 HikariCP 数据源-->
            <!--切换 druid 数据源时,HikariCP 依赖可以移除掉,不移除时也不影响-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
    
            <!--引用 log4j2 spring boot 启动器,内部依赖了 slf4j、log4j;排除项目中的 logback-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j2</artifactId>
            </dependency>
    
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169

    公共实体类

    @Data
    @Builder
    public class Student {
        private Integer age;
        private String name;
        private Boolean sex; //true:男  false:女
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    单层循环数据存取List,Map

    package com.example.democrud.democurd.Prototype.demo01;
    
    import lombok.Builder;
    import lombok.Data;
    import org.thymeleaf.expression.Lists;
    
    import java.util.*;
    import java.util.stream.Collectors;
    
    public class ListAndMapDemo {
    
        public static void main(String[] args) {
            List<Student> list = stduentInit();
            //取出年龄大于等于20岁的
             getAge20(list);
    
            //拼接字符串 马丹1,马丹2,马丹3,马丹4,马丹5
            // getStringName(list);
    
            //拼接字符串取年龄 10,15,20,25,30
            //  getAge(list);
    
            //根据年龄分组 10,10,20,20, ....
            //ageLimi(list);
        }
    
        /**
         * 根据年龄分组 10,10,20,20, ....
         *
         * @param list
         */
        public static void ageLimi(List<Student> list) {
            //根据年龄分组
            Map<String, List<Student>> map = new HashMap<>();
            for (Student student : list) {
                List<Student> students = map.get(student.getAge().toString());
                if (students == null) {
                    students = new ArrayList<>();
                }
                students.add(student);
                map.put(student.getAge().toString(), students);
            }
            System.out.println("优化前:" + map);
    
            Map<Integer, List<Student>> collect = list.stream().collect(Collectors.groupingBy(Student::getAge));
            System.out.println("优化前:" + collect);
        }
    
    
        /**
         * 拼接字符串取年龄 10,15,20,25,30
         *
         * @param list
         */
        public static void getAge(List<Student> list) {
            // 无法处理空
            //  String collect = list.stream().map(v -> v.getAge().toString()).collect(Collectors.joining(","));
            //  System.out.println("无法处理空"+collect);
    
    
            String collect1 = list.stream().filter(v -> v.getAge() != null).map(Student::getAge).map(String::valueOf).collect(Collectors.joining(","));
            System.out.println("可以处理优化前" + collect1);
    
            String collect2 = list.stream().filter(v -> !Objects.isNull(v.getAge())).map(Student::getAge).map(String::valueOf).collect(Collectors.joining(","));
            System.out.println("可以处理优化后" + collect2);
    
        }
    
    
        /**
         * 拼接字符串 马丹1,马丹2,马丹3,马丹4,马丹5
         *
         * @param list
         */
        public static void getStringName(List<Student> list) {
            StringBuffer buffer = new StringBuffer();
            for (Student student : list) {
                buffer.append(student.getName());
                buffer.append(",");
            }
            String substring = buffer.toString().substring(0, buffer.toString().length() - 1);
            System.out.println("优化前:" + substring);
    
            String collect = list.stream().map(v -> v.getName()).collect(Collectors.joining(","));
            System.out.println("优化后:" + collect);
        }
    
        /**
         * 取出年龄大于等于20岁的
         */
        public static void getAge20(List<Student> list) {
            //实现一
            int ages = 20;
            List<Student> list1 = new ArrayList<Student>();
    
            for (Student student5 : list) {
                if (student5.getAge() >= ages) {
                    list1.add(student5);
                }
            }
            System.out.println("优化前:" + list1);
    
            //实现二
            list = list.stream().filter(v -> v.getAge() >= 20).collect(Collectors.toList());
            System.out.println("优化后:" + list);
        }
    
        /**
         * 初始化list对象
         */
        public static List<Student> stduentInit() {
            List<Student> list = new ArrayList<Student>();
            //  Student student = new Student(null, "马丹1", true);
            Student student = new Student(10, "马丹1", true);
            Student student1 = new Student(20, "马丹2", false);
            Student student2 = new Student(10, "马丹3", true);
            Student student3 = new Student(10, "马丹4", false);
            Student student4 = new Student(20, "马丹5", true);
            list.add(student);
            list.add(student1);
            list.add(student2);
            list.add(student3);
            list.add(student4);
            return list;
    
        }
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130

    运行效果一一进行展示:

    1.取出年龄大于等于20岁的

     //取出年龄大于等于20岁的
            getAge20(list);     
    
    • 1
    • 2
    List原始数据==>[Student(age=10, name=马丹1, sex=true), Student(age=20, name=马丹2, sex=false), Student(age=10, name=马丹3, sex=true), Student(age=10, name=马丹4, sex=false), Student(age=20, name=马丹5, sex=true)]
    优化前:[Student(age=20, name=马丹2, sex=false), Student(age=20, name=马丹5, sex=true)]
    优化后:[Student(age=20, name=马丹2, sex=false), Student(age=20, name=马丹5, sex=true)]
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    2.拼接字符串 马丹1,马丹2…

     //拼接字符串 马丹1,马丹2,马丹3,马丹4,马丹5
             getStringName(list);
    
    • 1
    • 2
    List原始数据==>[Student(age=10, name=马丹1, sex=true), Student(age=20, name=马丹2, sex=false), Student(age=10, name=马丹3, sex=true), Student(age=10, name=马丹4, sex=false), Student(age=20, name=马丹5, sex=true)]
    优化前:马丹1,马丹2,马丹3,马丹4,马丹5
    优化后:马丹1,马丹2,马丹3,马丹4,马丹5
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    3. 拼接字符串取年龄 10,15,20,25,30

      //拼接字符串取年龄 10,15,20,25,30
             getAge(list);
    
    • 1
    • 2
    List原始数据==>[Student(age=10, name=马丹1, sex=true), Student(age=20, name=马丹2, sex=false), Student(age=10, name=马丹3, sex=true), Student(age=10, name=马丹4, sex=false), Student(age=20, name=马丹5, sex=true)]
    可以处理优化前10,20,10,10,20
    可以处理优化后10,20,10,10,20
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    4.0根据年龄分组 10,10,20,20, …

     //根据年龄分组 10,10,20,20, ....
            ageLimi(list);
    
    • 1
    • 2
    List原始数据==>[Student(age=10, name=马丹1, sex=true), Student(age=20, name=马丹2, sex=false), Student(age=10, name=马丹3, sex=true), Student(age=10, name=马丹4, sex=false), Student(age=20, name=马丹5, sex=true)]
    可以处理优化前10,20,10,10,20
    可以处理优化后10,20,10,10,20
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    多层循环嵌套

    package com.example.democrud.democurd.Prototype.demo01;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    
    import java.util.*;
    import java.util.stream.Collectors;
    
    public class ListAndMap1Demo {
    
        public static void main(String[] args) {
    
          // 循环中获取数据
        //  stduentgetName();
    
         //处理异常嵌套数据
              stduents();
    
    
    
        }
        public static void stduentgetName() {
    
            Map<String, List<Student>> map = stduentInit();
            //System.out.println(JSONObject.toJSON(map));
    
    
            for (Map.Entry<String, List<Student>> entry : map.entrySet()) {
                String key = entry.getKey();
                List<Student> list = entry.getValue();
                System.out.println("key===>"+key);
                System.out.println("list===>"+list);
    
                List<String> collect = list.stream().map(Student::getName).collect(Collectors.toList());
                System.out.println("list: 集合数组==》" + collect);
                String collect1 = list.stream().map(Student::getName).collect(Collectors.joining(","));
                System.out.println("string: String数组==》" + collect1);
    
                System.out.println("--------------------------------------1---------------------------------------------");
                List<Student> collectle = list.stream().filter(v -> v.getAge() >= 20).collect(Collectors.toList());
                System.out.println("collectle==>"+collectle);
    
    
    
                System.out.println("---------------------------------2--------------------------------------------------");
                List<String> strList = new ArrayList<>();
                for (Student stu : list) {
                    strList.add(stu.getName());
                }
                System.out.println("strList: " + strList);
    
            }
    
    
        }
    
        /**
         * 处理深层嵌套异常数据
         */
        public static void stduents() {
    
            List<Map<String, String>> list = new ArrayList<>();
            String str = "{\"sheets\":[{\"datas\":[{\"cells\":[{\"dataType\":2,\"value\":\"闫文超\"},{\"dataType\":1,\"value\":\"10.0\"}]},{\"cells\":[{\"dataType\":2,\"value\":\"老王\"},{\"dataType\":1,\"value\":\"20.0\"}]}],\"name\":\"Sheet1\",\"titles\":[\"Name\",\"AGE\"]}]}";
            JSONObject js = JSONObject.parseObject(str);
            //System.out.println(js);
            String sheets = js.get("sheets").toString();
            //System.out.println(sheets);
            JSONArray objects = JSONArray.parseArray(sheets);
            //System.out.println(objects);
            for (Object object : objects) {
                JSONObject js111 = JSONObject.parseObject(object.toString());
                //System.out.println(js111);
                String datas = js111.get("datas").toString();
                //System.out.println(datas);
                JSONArray objects1 = JSONArray.parseArray(datas);
                //System.out.println(objects1);
                for (Object o : objects1) {
                    JSONObject js1111 = JSONObject.parseObject(o.toString());
                    //System.out.println(js1111);
                    String cells = js1111.get("cells").toString();
                    //System.out.println(cells);
                    JSONArray objects2 = JSONArray.parseArray(cells);
                    //System.out.println("objects2 : "+ objects2);
                    for (Object o1 : objects2) {
                        JSONObject js11111 = JSONObject.parseObject(o1.toString());
                        //System.out.println(js11111);
                        String dataType = js11111.get("dataType").toString();
                        String value = js11111.get("value").toString();
                        Map<String, String> map = new HashMap<>();
                        map.put(dataType, value);
                        list.add(map);
                        //System.out.println("这他妈那个傻逼写的 dataType=" + dataType + "  value="+ value);
                    }
    
                }
            }
    
            System.out.println(list);
    
        }
    
    
    
    
    
        /**
         * 初始化list对象
         */
        public static Map<String, List<Student>> stduentInit() {
    
            Map<String, List<Student>> map = new HashMap<>();
            List<Student> list = new ArrayList<>();
            Student student = new Student(10, "马丹1", true);
            Student student1 = new Student(15, "马丹2", false);
            Student student2 = new Student(20, "马丹3", true);
            Student student3 = new Student(25, "马丹4", false);
            Student student4 = new Student(30, "马丹5", true);
            list.add(student);
            list.add(student1);
            list.add(student2);
            list.add(student3);
            list.add(student4);
            map.put("list", list);
    
            List<Student> list1 = new ArrayList<>();
            Student stu = new Student(10, "马丹1", true);
            Student stu1 = new Student(15, "马丹2", false);
            Student stu2 = new Student(20, "马丹3", true);
            Student stu3 = new Student(25, "马丹4", false);
            Student stu4 = new Student(30, "马丹5", true);
            list1.add(stu);
            list1.add(stu1);
            list1.add(stu2);
            list1.add(stu3);
            list1.add(stu4);
            map.put("list1", list1);
            return map;
        }
    
    
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145

    1.处理异常嵌套数据

    //处理异常嵌套数据
              stduents();
    
    • 1
    • 2

    此处不理解的可以把sout注释取下;每步都有打印我这边为了更好的展示就先注释了;

    [{2=闫文超}, {1=10.0}, {2=老王}, {1=20.0}]
    
    • 1

    在这里插入图片描述

    2.循环中获取数据

      // 循环中获取数据
            stduentgetName();
    
    • 1
    • 2
    key===>list1
    list===>[Student(age=10, name=马丹1, sex=true), Student(age=15, name=马丹2, sex=false), Student(age=20, name=马丹3, sex=true), Student(age=25, name=马丹4, sex=false), Student(age=30, name=马丹5, sex=true)]
    list: 集合数组==[马丹1, 马丹2, 马丹3, 马丹4, 马丹5]
    string: String数组==》马丹1,马丹2,马丹3,马丹4,马丹5
    --------------------------------------1---------------------------------------------
    collectle==>[Student(age=20, name=马丹3, sex=true), Student(age=25, name=马丹4, sex=false), Student(age=30, name=马丹5, sex=true)]
    ---------------------------------2--------------------------------------------------
    strList: [马丹1, 马丹2, 马丹3, 马丹4, 马丹5]
    key===>list
    list===>[Student(age=10, name=马丹1, sex=true), Student(age=15, name=马丹2, sex=false), Student(age=20, name=马丹3, sex=true), Student(age=25, name=马丹4, sex=false), Student(age=30, name=马丹5, sex=true)]
    list: 集合数组==[马丹1, 马丹2, 马丹3, 马丹4, 马丹5]
    string: String数组==》马丹1,马丹2,马丹3,马丹4,马丹5
    --------------------------------------1---------------------------------------------
    collectle==>[Student(age=20, name=马丹3, sex=true), Student(age=25, name=马丹4, sex=false), Student(age=30, name=马丹5, sex=true)]
    ---------------------------------2--------------------------------------------------
    strList: [马丹1, 马丹2, 马丹3, 马丹4, 马丹5]
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    欧克;演示完成后续有其他部分会补充;此处感谢吊毛同学的交流;

    如对本文内容有任何疑问、建议或意见,请联系作者,作者将尽力回复并改进📓;(联系微信:Solitudemind )

  • 相关阅读:
    C#中DataAdapter对象
    leetcode 135. 分发糖果
    使用vue脚手架快速搭建vue 2项目
    【S2021006基于Python的协同过滤算法的电影推荐系统-哔哩哔哩】 https://b23.tv/42Ygkd6
    java操作数据库的工具类简介说明
    2020年最新版Java面试题大全(文末附参考答案)
    open graph协议
    HTML页面加锁
    基于springboot+vue的幼儿园管理系统 elementui
    字符编码详解
  • 原文地址:https://blog.csdn.net/qq_42055933/article/details/127825840