首页 > 其他 > 详细

collectingAndThen的使用姿势

时间:2021-08-16 14:44:07      阅读:18      评论:0      收藏:0      [点我收藏+]
public class AdvancedStreamingP2 {

    public static void main(String[] args) {

        List<Student> studentList = Student.generateData();
        collect(studentList);
        collectingAnd(studentList);
    }

    private static void collect(List<Student> studentList) {
        System.out.println("\n---------- Extracting Student Name with Max Age by Type -----------");
        Map<String, Optional<Student>> stuMax = studentList.stream().collect(groupingBy(Student::getType, maxBy(comparing(Student::getAge))));
        stuMax.forEach((k, v) -> System.out.println("Key : " + k + ", Value :" + v.get()));
    }

    /**
     * I want to extract the student name, with the max age, grouping student by Type. Is it possible by using any combination in "collect" itself?
     *
     * I want the output like :
     *
     * ---------- Extracting Student Name with Max Age by Type -----------
     *
     * Key : School, Value : Aman
     * Key : College, Value : Ajay
     */
    private static void collectingAnd(List<Student> studentList) {
        Map<String, String> stuMax =
                studentList.stream()
                        .collect(groupingBy(
                                Student::getType,
                                collectingAndThen(maxBy(comparing(Student::getAge)),v -> v.get().getName())
                        ));
        System.out.println(stuMax);
    }
}
public class Student{
    String name;
    int age;
    String type;

    public Student(){}

    public Student(String name, int age, String type) {
        this.name = name;
        this.age = age;
        this.type = type;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ", type=‘" + type + ‘\‘‘ +
                ‘}‘;
    }

    public static List<Student> generateData() {

        List<Student> st = Arrays.asList(new Student("Ashish", 27, "College"),
                new Student("Aman", 24, "School"),
                new Student("Rahul", 18, "School"),
                new Student("Ajay", 29, "College"),
                new Student("Mathur", 25, "College"),
                new Student("Modi", 28, "College"),
                new Student("Prem", 15, "School"),
                new Student("Akash", 17, "School"));
        return st;
    }
}

collect方法输出参数较多,优化后的collectingAnd方法使用了collectingAndThen

顾名思义,即收集并处理,处理函数是 Function功能函数,即 输入一个参数,并返回一个参数

collectingAndThen的使用姿势

原文:https://www.cnblogs.com/woooodlin/p/15146990.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!