jdk8自定义Collector实现对象的求和和求平均值

1、首先定义两个接口SumOperator、AverageOperator

所有要实现求和功能的类都必须实现SumOperator接口,要实现求平均值功能的类都必须实现AverageOperator接口,以下是两个接口定义:

public interface SumOperator<T> {

    /**
     * sum
     * @desc: 
     * @author: 
     * @createTime: 2020/4/29 12:37
     * @param t
     * @return: T
     */
    T sum(T t);

}
public interface AverageOperator<T> extends SumOperator<T>{

    /**
     * divide
     * @desc: 
     * @author: 
     * @createTime: 2020/4/29 12:37
     * @param number
     * @return: T
     */
    T divide(long number);

}

2、接下来自定义一个类来生成这两个Collector

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;

/**
 * CustomStream
 *
 * @desc: TODO 类的设计目的、功能及注意事项
 * @version:
 * @createTime: 2020/4/29 12:23
 * @author: 
 */
public class CustomCollectors{

    public static<T extends SumOperator<T>> Collector<T, List<T>, T> ofSum() {
        return new CollectorImpl<T>(
                list -> list.stream().reduce((t1, t2) -> t1.sum(t2)).get()
        );
    }

    public static<T extends SumOperator<T>> Collector<T, List<T>, T> ofSum(Function<Optional<T>, T> function) {
        return new CollectorImpl<T>(
                list -> list.stream().reduce(function.apply(list.stream().findFirst()), (t1, t2) -> t1.sum(t2))
        );
    }

    public static<T extends AverageOperator<T>> Collector<T, List<T>, T> ofAverage() {
        return new CollectorImpl<T>(
                list -> list.stream().reduce((t1, t2) -> t1.sum(t2)).get().divide(list.size())
        );
    }
    
    public static<T extends AverageOperator<T>> Collector<T, List<T>, T> ofAverage(Function<Optional<T>, T> function) {
        return new CollectorImpl<T>(
                list -> list.stream().reduce(function.apply(list.stream().findFirst()), (t1, t2) -> t1.sum(t2)).divide(list.size())
        );
    }

    static class CollectorImpl<T> implements Collector<T, List<T>, T>{

        private Function<List<T>, T> finisher;

        private CollectorImpl(Function<List<T>, T> finisher){
            this.finisher = finisher;
        }

        @Override
        public Supplier<List<T>> supplier() {
            return ArrayList::new;
        }

        @Override
        public BiConsumer<List<T>, T> accumulator() {
            return List::add;
        }

        @Override
        public BinaryOperator<List<T>> combiner() {
            return (left, right) -> {
                left.addAll(right); return left;
            };
        }

        @Override
        public Function<List<T>, T> finisher() {
            return finisher;
        }

        @Override
        public Set<Characteristics> characteristics() {
            return Collections.emptySet();
        }

    }

}

3、测试

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class Test {

    @Getter
    @Setter
    @Accessors(chain = true)
    static class Student implements AverageOperator<Student> {

        private String name;

        private int term;

        private double mathScore;

        private double englishScore;

        @Override
        public Student divide(long number) {
            this.mathScore /= number;
            this.englishScore /= number;
            return this;
        }

        @Override
        public Student sum(Student student) {
            this.mathScore += student.mathScore;
            this.englishScore += student.englishScore;
            return this;
        }

    }

    public static void main(String[] args) {
        List<Student> students = new ArrayList<Student>(){
            {
                add(new Student().setName("张三").setTerm(1).setMathScore(90).setEnglishScore(80));
                add(new Student().setName("张三").setTerm(2).setMathScore(85).setEnglishScore(77));
                add(new Student().setName("张三").setTerm(3).setMathScore(95).setEnglishScore(78));
                add(new Student().setName("张三").setTerm(4).setMathScore(88).setEnglishScore(75));
                add(new Student().setName("张三").setTerm(5).setMathScore(95).setEnglishScore(78));
                add(new Student().setName("张三").setTerm(6).setMathScore(88).setEnglishScore(75));

                add(new Student().setName("李四").setTerm(1).setMathScore(90).setEnglishScore(60));
                add(new Student().setName("李四").setTerm(2).setMathScore(95).setEnglishScore(67));
                add(new Student().setName("李四").setTerm(3).setMathScore(98).setEnglishScore(60));
                add(new Student().setName("李四").setTerm(4).setMathScore(98).setEnglishScore(70));
                add(new Student().setName("李四").setTerm(5).setMathScore(100).setEnglishScore(68));
                add(new Student().setName("李四").setTerm(6).setMathScore(96).setEnglishScore(75));
            }
        };

        //下面分组求出学生的平均分
        Collector<Student, List<Student>, Student> collector = CustomCollectors.ofAverage(optional -> {
            Student source = optional.get();
            Student target = new Student();
            target.setName(source.getName());
            return target;
        });
        Map<String, Student> map = students.stream().collect(Collectors.groupingBy(Student::getName, collector));
        map.forEach((name, student) -> {
            System.out.println(String.format("学生【%s】的平均分为;(数学:%f, 英语:%f)", name, student.getMathScore(), student.getEnglishScore()));
        });

    }

}

打印结果如下:

学生【李四】的平均分为;(数学:96.166667, 英语:66.666667)
学生【张三】的平均分为;(数学:90.166667, 英语:77.166667)
相关推荐
<p> <b><span style="font-size:14px;"></span><span style="font-size:14px;background-color:#FFE500;">【Java面试宝典】</span></b><br /> <span style="font-size:14px;">1、68讲视频课,500道大厂Java常见面试题+100个Java面试技巧与答题公式+10万字核心知识解析+授课老师1对1面试指导+无限次回放</span><br /> <span style="font-size:14px;">2、这门课程基于胡书敏老师8年Java面试经验,调研近百家互联网公司及面试官的问题打造而成,从筛选简历和面试官角度,给出能帮助候选人能面试成功的面试技巧。</span><br /> <span style="font-size:14px;">3、通过学习这门课程,你能系统掌握Java核心、数据库、Java框架、分布式组件、Java简历准备、面试实战技巧等面试必考知识点。</span><br /> <span style="font-size:14px;">4、知识点+项目经验案例,每一个都能做为面试的作品展现。</span><br /> <span style="font-size:14px;">5、本课程已经在线下的培训课程中经过实际检验,老师每次培训结束后,都能帮助同学们运用面试技巧,成功找到更好的工作。</span><br /> <br /> <span style="font-size:14px;background-color:#FFE500;"><b>【超人气讲师】</b></span><br /> <span style="font-size:14px;">胡书敏 | 10年大厂工作经验,8年Java面试官经验,5年线下Java职业培训经验,5年架构师经验</span><br /> <br /> <span style="font-size:14px;background-color:#FFE500;"><b>【报名须知】</b></span><br /> <span style="font-size:14px;">上课模式是什么?</span><br /> <span style="font-size:14px;">课程采取录播模式,课程永久有效,可无限次观看</span><br /> <span style="font-size:14px;">课件、课程案例代码完全开放给你,你可以根据所学知识,自行修改、优化</span><br /> <br /> <br /> <span style="font-size:14px;background-color:#FFE500;"><strong>如何开始学习?</strong></span><br /> <span style="font-size:14px;">PC端:报名成功后可以直接进入课程学习</span><br /> <span style="font-size:14px;">移动端:<span style="font-family:Helvetica;font-size:14px;background-color:#FFFFFF;">CSDN 学院APP(注意不是CSDN APP哦)</span></span> </p>
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页