Map自定义key,然后把value的集合List进行指定字段排序

发布时间 2023-04-06 13:52:29作者: 蛋挞小子
package com.zdft.purchase;

import com.google.common.collect.Lists;

import java.util.*;
import java.util.stream.Collectors;

public class StudentMethod {
    // 需求:Map自定义key,然后把value的集合List进行指定字段排序;例如:多次考试,取最高分的集合展示
    public static void main(String[] args) {
        Student student1 = new Student(1, "张三", "1991-01", "英语", "27");
        Student student2 = new Student(2, "李四", "1992-01", "英语", "15");
        Student student3 = new Student(3, "王五", "1993-01", "英语", "24");
        Student student4 = new Student(4, "赵六", "1994-01", "英语", "15");
        Student student5 = new Student(5, "李七", "1995-01", "英语", "29");
        Student student6 = new Student(6, "钱八", "1996-01", "英语", "18");
        Student student7 = new Student(7, "孙九", "1997-01", "英语", "25");
        Student student8 = new Student(8, "周八", "1998-01", "英语", "80");
        Student student9 = new Student(8, "周八", "1998-01", "英语", "90");
        Student student10 = new Student(8, "周八", "1998-01", "英语", "65");
        Student student11 = new Student(8, "周八", "1998-01", "英语", "60");

        List<Student> list = new ArrayList<>();
        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.add(student4);
        list.add(student5);
        list.add(student6);
        list.add(student7);
        list.add(student8);
        list.add(student9);
        list.add(student10);
        list.add(student11);

        // 第一种方式
        Map<String, List<Student>> map2 = list.stream().collect(Collectors.toMap(m -> m.getId() + m.getName() + m.getBirth(), item -> Lists.newArrayList(item),
                (List<Student> newValueList, List<Student> oldValueList) -> {
                    oldValueList.addAll(newValueList);
                    return oldValueList;
                })
        );

        List<Student> collect = new ArrayList<>();

        for (String key : map2.keySet()) {
            List<Student> studentList = map2.get(key);
            Student student = studentList.stream().sorted(Comparator.comparing(Student::getScore).reversed()).findFirst().orElse(null);
            collect.add(student);
        }

        // 第二种方式
        /*Map<String, Student> map = list.stream().collect(
                Collectors.groupingBy(m -> m.getId() + m.getName() + m.getBirth(), Collectors.collectingAndThen(
                        Collectors.reducing((t1, t2) -> t1.getScore().compareTo(t2.getScore()) > 0 ? t1 : t2),
                        Optional::get
                ))
        );

        List<Student> collect = map.values().stream().collect(Collectors.toList());*/


        // 按ID排序
        Collections.sort(collect, Comparator.comparing(Student::getId));
        collect.forEach(m1 -> {
            System.out.println(m1.toString());
        });
    }
}

class Student {
    private int id;
    private String name;
    private String birth;
    private String curriculum;
    private String score;

    public Student() {

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBirth() {
        return birth;
    }

    public void setBirth(String birth) {
        this.birth = birth;
    }

    public String getCurriculum() {
        return curriculum;
    }

    public void setCurriculum(String curriculum) {
        this.curriculum = curriculum;
    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }

    public Student(int id, String name, String birth, String curriculum, String score) {
        this.id = id;
        this.name = name;
        this.birth = birth;
        this.curriculum = curriculum;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", birth='" + birth + '\'' +
                ", curriculum='" + curriculum + '\'' +
                ", score=" + score +
                '}';
    }
}