写在最前面
本文的目的,是对一个数据列表,按照其中的某些属性,进行分组。
代码实现
package com.example;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class GroupService {
@SafeVarargs
public static <T> Map<List<Object>,List<T>> groupingBy(List<T> list, Function<T, Object>...function){
return list.stream().collect(Collectors.groupingBy(order -> {
List<Object> keys = new ArrayList<>();
for (Function<T, Object> tObjectFunction : function) {
keys.add(tObjectFunction.apply(order ));
}
return keys;
}, Collectors.toList()));
}
}
使用案例
现在有以下对象:
package com.example;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;
@AllArgsConstructor
@ToString
@Data
public class Student {
private Integer gradeId;
private Integer classId;
private Integer id;
private SexEnum sexEnum;
private String name;
public enum SexEnum {
MALE, FEMALE, UNKNOWN
}
}
对该学生对象,创建一个学生列表,并对其不同的属性进行分组操作。
使用示例
package com.example;
import java.util.List;
public class GroupClient {
public static void main(String[] args) {
List<Student> studentList = List.of(
new Student(5, 1, 50032, Student.SexEnum.FEMALE, "赵璐"),
new Student(4, 3, 40032, Student.SexEnum.MALE, "钱璐"),
new Student(2, 1, 20032, Student.SexEnum.FEMALE, "孙璐"),
new Student(2, 1, 20022, Student.SexEnum.MALE, "李璐"),
new Student(2, 1, 20023, Student.SexEnum.MALE, "陈璐"),
new Student(4, 1, 40232, Student.SexEnum.MALE, "冯璐")
);
System.out.println("只按照年级分组:");
GroupService.groupingBy(studentList, Student::getGradeId).forEach((k,v) -> {
System.out.println("年级 = " + k + " =====> v = " + v);
});
System.out.println("只按照班级分组:");
GroupService.groupingBy(studentList, Student::getClassId).forEach((k,v) -> {
System.out.println("班级 = " + k + " =====> v = " + v);
});
System.out.println("按照年级、班级分组:");
GroupService.groupingBy(studentList, Student::getGradeId, Student::getClassId).forEach((k,v) -> {
System.out.println("年级、班级 = " + k + " =====> v = " + v);
});
System.out.println("按照年级、班级、性别分组:");
GroupService.groupingBy(studentList, Student::getGradeId, Student::getClassId, Student::getSexEnum).forEach((k,v) -> {
System.out.println("年级、班级、性别 = " + k + " =====> v = " + v);
});
}
}
控制台输出结果
只按照年级分组:
年级 = [2] =====> v = [Student(gradeId=2, classId=1, id=20032, sexEnum=FEMALE, name=孙璐), Student(gradeId=2, classId=1, id=20022, sexEnum=MALE, name=李璐), Student(gradeId=2, classId=1, id=20023, sexEnum=MALE, name=陈璐)]
年级 = [4] =====> v = [Student(gradeId=4, classId=3, id=40032, sexEnum=MALE, name=钱璐), Student(gradeId=4, classId=1, id=40232, sexEnum=MALE, name=冯璐)]
年级 = [5] =====> v = [Student(gradeId=5, classId=1, id=50032, sexEnum=FEMALE, name=赵璐)]
只按照班级分组:
班级 = [1] =====> v = [Student(gradeId=5, classId=1, id=50032, sexEnum=FEMALE, name=赵璐), Student(gradeId=2, classId=1, id=20032, sexEnum=FEMALE, name=孙璐), Student(gradeId=2, classId=1, id=20022, sexEnum=MALE, name=李璐), Student(gradeId=2, classId=1, id=20023, sexEnum=MALE, name=陈璐), Student(gradeId=4, classId=1, id=40232, sexEnum=MALE, name=冯璐)]
班级 = [3] =====> v = [Student(gradeId=4, classId=3, id=40032, sexEnum=MALE, name=钱璐)]
按照年级、班级分组:
年级、班级 = [2, 1] =====> v = [Student(gradeId=2, classId=1, id=20032, sexEnum=FEMALE, name=孙璐), Student(gradeId=2, classId=1, id=20022, sexEnum=MALE, name=李璐), Student(gradeId=2, classId=1, id=20023, sexEnum=MALE, name=陈璐)]
年级、班级 = [4, 3] =====> v = [Student(gradeId=4, classId=3, id=40032, sexEnum=MALE, name=钱璐)]
年级、班级 = [5, 1] =====> v = [Student(gradeId=5, classId=1, id=50032, sexEnum=FEMALE, name=赵璐)]
年级、班级 = [4, 1] =====> v = [Student(gradeId=4, classId=1, id=40232, sexEnum=MALE, name=冯璐)]
按照年级、班级、性别分组:
年级、班级、性别 = [2, 1, FEMALE] =====> v = [Student(gradeId=2, classId=1, id=20032, sexEnum=FEMALE, name=孙璐)]
年级、班级、性别 = [4, 1, MALE] =====> v = [Student(gradeId=4, classId=1, id=40232, sexEnum=MALE, name=冯璐)]
年级、班级、性别 = [5, 1, FEMALE] =====> v = [Student(gradeId=5, classId=1, id=50032, sexEnum=FEMALE, name=赵璐)]
年级、班级、性别 = [2, 1, MALE] =====> v = [Student(gradeId=2, classId=1, id=20022, sexEnum=MALE, name=李璐), Student(gradeId=2, classId=1, id=20023, sexEnum=MALE, name=陈璐)]
年级、班级、性别 = [4, 3, MALE] =====> v = [Student(gradeId=4, classId=3, id=40032, sexEnum=MALE, name=钱璐)]
|