List,Map多层循环嵌套Demo及其理解
- 先简答贴下POM
- 公共实体类
- 单层循环数据存取List,Map
- 1.取出年龄大于等于20岁的
- 2.拼接字符串 马丹1,马丹2....
- 3. 拼接字符串取年龄 10,15,20,25,30
- 4.0根据年龄分组 10,10,20,20, ....
- 多层循环嵌套
- 1.处理异常嵌套数据
- 2.循环中获取数据
- 先简答贴下POM
- 公共实体类
- 单层循环数据存取List,Map
- 1.取出年龄大于等于20岁的
- 2.拼接字符串 马丹1,马丹2....
- 3. 拼接字符串取年龄 10,15,20,25,30
- 4.0根据年龄分组 10,10,20,20, ....
- 多层循环嵌套
- 1.处理异常嵌套数据
- 2.循环中获取数据
我这边在之前写代码遇到多层(n*n)层循环嵌套逻辑的时候很头疼今天写一些demo来帮自己更好的理解相关内容
我这边直接上代码:
希望大家可以通过demo更好的理解他们之间的转换循环与嵌套逻辑;
先简答贴下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 JPA,JPA 组件内部会依赖 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>
公共实体类
@Data
@Builder
public class Student {
private Integer age;
private String name;
private Boolean sex; //true:男 false:女
}
单层循环数据存取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.取出年龄大于等于20岁的
//取出年龄大于等于20岁的
getAge20(list);
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)]
2.拼接字符串 马丹1,马丹2…
//拼接字符串 马丹1,马丹2,马丹3,马丹4,马丹5
getStringName(list);
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
3. 拼接字符串取年龄 10,15,20,25,30
//拼接字符串取年龄 10,15,20,25,30
getAge(list);
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
4.0根据年龄分组 10,10,20,20, …
//根据年龄分组 10,10,20,20, ....
ageLimi(list);
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
多层循环嵌套
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.处理异常嵌套数据
//处理异常嵌套数据
stduents();
此处不理解的可以把sout注释取下;每步都有打印我这边为了更好的展示就先注释了;
[{2=闫文超}, {1=10.0}, {2=老王}, {1=20.0}]
2.循环中获取数据
// 循环中获取数据
stduentgetName();
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]
欧克;演示完成后续有其他部分会补充;此处感谢吊毛同学的交流;我这边在之前写代码遇到多层(n*n)层循环嵌套逻辑的时候很头疼今天写一些demo来帮自己更好的理解相关内容
我这边直接上代码:
希望大家可以通过demo更好的理解他们之间的转换循环与嵌套逻辑;
List,Map多层循环嵌套Demo及其理解
- 先简答贴下POM
- 公共实体类
- 单层循环数据存取List,Map
- 1.取出年龄大于等于20岁的
- 2.拼接字符串 马丹1,马丹2....
- 3. 拼接字符串取年龄 10,15,20,25,30
- 4.0根据年龄分组 10,10,20,20, ....
- 多层循环嵌套
- 1.处理异常嵌套数据
- 2.循环中获取数据
- 先简答贴下POM
- 公共实体类
- 单层循环数据存取List,Map
- 1.取出年龄大于等于20岁的
- 2.拼接字符串 马丹1,马丹2....
- 3. 拼接字符串取年龄 10,15,20,25,30
- 4.0根据年龄分组 10,10,20,20, ....
- 多层循环嵌套
- 1.处理异常嵌套数据
- 2.循环中获取数据
先简答贴下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 JPA,JPA 组件内部会依赖 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>
公共实体类
@Data
@Builder
public class Student {
private Integer age;
private String name;
private Boolean sex; //true:男 false:女
}
单层循环数据存取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.取出年龄大于等于20岁的
//取出年龄大于等于20岁的
getAge20(list);
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)]
2.拼接字符串 马丹1,马丹2…
//拼接字符串 马丹1,马丹2,马丹3,马丹4,马丹5
getStringName(list);
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
3. 拼接字符串取年龄 10,15,20,25,30
//拼接字符串取年龄 10,15,20,25,30
getAge(list);
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
4.0根据年龄分组 10,10,20,20, …
//根据年龄分组 10,10,20,20, ....
ageLimi(list);
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
多层循环嵌套
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.处理异常嵌套数据
//处理异常嵌套数据
stduents();
此处不理解的可以把sout注释取下;每步都有打印我这边为了更好的展示就先注释了;
[{2=闫文超}, {1=10.0}, {2=老王}, {1=20.0}]
2.循环中获取数据
// 循环中获取数据
stduentgetName();
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]