List
为了保证程序的性能,如果要分组查数据库,一般会用到JAVA8 中的
Collection.stream().collect(Collectors.groupingBy(Entity::getXXXX));
得到Map
假设有学生信息实体类如下:
@Data
@AllArgsConstructor
@NoArgsConstructor
class StudentInfo {
/**
* 姓名
*/
private String name;
/**
* 班级
*/
private Integer classNum;
}
学生信息有学生信息List如下:
StudentInfo a = new StudentInfo("张三", 1);
StudentInfo b = new StudentInfo("李四", 1);
StudentInfo c = new StudentInfo("王五", 1);
StudentInfo d = new StudentInfo("赵大", 2);
StudentInfo e = new StudentInfo("钱二", 2);
StudentInfo f = new StudentInfo("周六", 3);
List studentInfos = new ArrayList<>(Arrays.asList(a, b, c, d, e, f));
想获各个班级下的学生姓名(想要获得效果)
1 : [张三, 李四, 王五]
2 : [赵大, 钱二]
3 : [周六]
首先能想到的写法如下 JAVA8中的 Collectors.groupingBy:
public static void main(String[] args) {
StudentInfo a = new StudentInfo("张三", 1);
StudentInfo b = new StudentInfo("李四", 1);
StudentInfo c = new StudentInfo("王五", 1);
StudentInfo d = new StudentInfo("赵大", 2);
StudentInfo e = new StudentInfo("钱二", 2);
StudentInfo f = new StudentInfo("周六", 3);
List studentInfos = new ArrayList<>(Arrays.asList(a, b, c, d, e, f));
Map> collect = studentInfos.stream().collect(Collectors.groupingBy(StudentInfo::getClassNum));
Map> class2Name = new HashMap<>();
for (int i : collect.keySet()) {
List collect1 = collect.get(i).stream().map(StudentInfo::getName).collect(Collectors.toList());
class2Name.put(i, collect1);
}
class2Name.forEach((integer, stringList) -> System.out.println(integer + " : " + stringList));
}
总感觉应该还有高级写法,在网上寻找后,发现如下写法
使用 JAVA8 中的 computeIfAbsent
public static void main(String[] args) {
StudentInfo a = new StudentInfo("张三", 1);
StudentInfo b = new StudentInfo("李四", 1);
StudentInfo c = new StudentInfo("王五", 1);
StudentInfo d = new StudentInfo("赵大", 2);
StudentInfo e = new StudentInfo("钱二", 2);
StudentInfo f = new StudentInfo("周六", 3);
List studentInfos = new ArrayList<>(Arrays.asList(a, b, c, d, e, f));
Map> stuMap1 = new HashMap<>();
studentInfos.forEach(studentInfo -> stuMap1.computeIfAbsent(studentInfo.getClassNum(), ArrayList::new).add(studentInfo.getName()));
stuMap1.forEach((integer, stringList) -> System.out.println(integer + " : " + stringList));
}
使用JAVA8中的 Collectors.toMap
public static void main(String[] args) {
StudentInfo a = new StudentInfo("张三", 1);
StudentInfo b = new StudentInfo("李四", 1);
StudentInfo c = new StudentInfo("王五", 1);
StudentInfo d = new StudentInfo("赵大", 2);
StudentInfo e = new StudentInfo("钱二", 2);
StudentInfo f = new StudentInfo("周六", 3);
List studentInfos = new ArrayList<>(Arrays.asList(a, b, c, d, e, f));
Map> stuMap = studentInfos.stream()
.collect(Collectors.toMap(StudentInfo::getClassNum, x -> Lists.newArrayList(x.getName()), (objects, objects2) -> {
objects.addAll(objects2);
return objects;
}));
stuMap.forEach((integer, stringList) -> System.out.println(integer + " : " + stringList));
System.out.println("-----------------------------------");
Map> stuMap2 = studentInfos.stream()
.collect(Collectors.toMap(StudentInfo::getClassNum, x -> new ArrayList<>(Collections.singletonList(x.getName())), (objects, objects2) -> {
objects.addAll(objects2);
return objects;
}));
stuMap2.forEach((integer, stringList) -> System.out.println(integer + " : " + stringList));
}
附上整个测试代码:
package com.example.demo;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.*;
import java.util.stream.Collectors;
public class List2MapTest {
public static void main(String[] args) {
StudentInfo a = new StudentInfo("张三", 1);
StudentInfo b = new StudentInfo("李四", 1);
StudentInfo c = new StudentInfo("王五", 1);
StudentInfo d = new StudentInfo("赵大", 2);
StudentInfo e = new StudentInfo("钱二", 2);
StudentInfo f = new StudentInfo("周六", 3);
List studentInfos = new ArrayList<>(Arrays.asList(a, b, c, d, e, f));
/*studentInfos.add(a);
studentInfos.add(b);
studentInfos.add(c);
studentInfos.add(d);
studentInfos.add(e);
studentInfos.add(f);*/
Map> stuMap1 = new HashMap<>();
studentInfos.forEach(studentInfo -> stuMap1.computeIfAbsent(studentInfo.getClassNum(), ArrayList::new).add(studentInfo.getName()));
stuMap1.forEach((integer, stringList) -> System.out.println(integer + " : " + stringList));
System.out.println("-----------------------------------");
Map> stuMap = studentInfos.stream()
.collect(Collectors.toMap(StudentInfo::getClassNum, x -> Lists.newArrayList(x.getName()), (objects, objects2) -> {
objects.addAll(objects2);
return objects;
}));
stuMap.forEach((integer, stringList) -> System.out.println(integer + " : " + stringList));
System.out.println("-----------------------------------");
Map> stuMap2 = studentInfos.stream()
.collect(Collectors.toMap(StudentInfo::getClassNum, x -> new ArrayList<>(Collections.singletonList(x.getName())), (objects, objects2) -> {
objects.addAll(objects2);
return objects;
}));
stuMap2.forEach((integer, stringList) -> System.out.println(integer + " : " + stringList));
System.out.println("-----------------------------------");
Map> collect = studentInfos.stream().collect(Collectors.groupingBy(StudentInfo::getClassNum));
System.out.println(JSON.toJSON(collect));
Map> class2Name = new HashMap<>();
for (int i : collect.keySet()) {
List collect1 = collect.get(i).stream().map(StudentInfo::getName).collect(Collectors.toList());
class2Name.put(i, collect1);
}
class2Name.forEach((integer, stringList) -> System.out.println(integer + " : " + stringList));
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class StudentInfo {
/**
* 姓名
*/
private String name;
/**
* 班级
*/
private Integer classNum;
}
最后再附上要用到的依赖
org.projectlombok
lombok
1.16.12
com.alibaba
fastjson
1.2.49
com.google.guava
guava
26.0-jre