// lombok 插件,如果没有用该插件的,请自行get、set方法
@Data
public class Student {
public Student(Integer id, Integer classNo, String name, String sex, Double score) {
this.id = id;
this.classNo = classNo;
this.name = name;
this.sex = sex;
this.score = score;
}
private Integer id;
private Integer classNo;
private String name;
private String sex;
private Double score;
}
Student student1 = new Student(5, 3, "天一", "男", 100D);
Student student2 = new Student(4, 3, "王二", "男", 10D);
Student student3 = new Student(1, 1, "张三", "女", 66D);
Student student4 = new Student(2, 2, "李四", "男", 99D);
Student student5 = new Student(3, 2, "王五", "女", 59D);
List<Student> students = Arrays.asList(student1, student2, student3, student4, student5);
// 各个班级,成绩最差的学生
Map<Integer, Student> classNoStudentMap = students
.parallelStream()
.collect(
Collectors.toMap(Student::getClassNo, Function.identity(), (c1, c2) -> c1.getScore() < c2.getScore() ? c1 : c2)
);
System.out.println(classNoStudentMap);
// 分性别,成绩最差的学生
Map<String, Student> sexStudentMap = students
.parallelStream()
.collect(
Collectors.toMap(Student::getSex, Function.identity(), (c1, c2) -> c1.getScore() < c2.getScore() ? c1 : c2)
);
System.out.println(sexStudentMap);
相关阅读:https://blog.csdn.net/weixin_41835612/article/details/83687088
https://blog.csdn.net/kingmax54212008/article/details/102827306