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

以下是两个接口定义:

/**
 * SumOperator
 *
 * @desc: 累加器
 * @version:
 * @createTime: 2020/9/27 14:26
 * @author: liuhr
 */
public interface SumOperator<T> {

    /**
     * 传入两个对象,返回这两个对象相加的结果
     * @param t1
     * @param t2
     * @return
     */
    T sum(T t1, T t2);

}
/**
 * AverageOperator
 *
 * @desc: 
 * @version:
 * @createTime: 2020/9/27 14:29
 * @author: liuhr
 */
public interface AverageOperator<T> extends SumOperator<T>{

    /**
     * 求对象t除以number的结果
     * @param t
     * @param number
     * @return
     */
    T divide(T t, double number);

}

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
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;

/**
 * CustomCollectors
 *
 * @desc: TODO 类的设计目的、功能及注意事项
 * @version:
 * @createTime: 2020/9/27 14:31
 * @author: liuhr
 */
public class CustomCollectors {

    private CustomCollectors(){}

    /**
     * 求和
     * @param sumOperator
     * @param <T>
     * @return
     */
    public static<T> AbstractCollectorImpl<T> ofSum(SumOperator<T> sumOperator) {
        return getInstance(
                list-> list.stream()
                        .reduce((t1, t2) -> sumOperator.sum(t1, t2))
                        .orElse(null)
        );
    }

    /**
     * 求平均值
     * @param averageOperator
     * @param <T>
     * @return
     */
    public static <T> AbstractCollectorImpl<T> ofAverage(AverageOperator<T> averageOperator) {
        return getInstance(
                list -> list.stream()
                        .reduce((t1, t2) -> averageOperator.sum(t1, t2))
                        .map(sum -> averageOperator.divide(sum, list.size()))
                        .orElse(null)
        );
    }

    /**
     *
     * @param finisher
     * @param <T>
     * @return
     */
    private static <T> AbstractCollectorImpl<T> getInstance(Function<List<T>, T> finisher){
        return new AbstractCollectorImpl<T>() {
            @Override
            public Function<List<T>, T> finisher() {
                return finisher;
            }
        };
    }

    /**
     * 
     * @param <T>
     */
    static abstract class AbstractCollectorImpl<T> implements Collector<T, List<T>, T> {

        @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 Set<Characteristics> characteristics() {
            return Collections.emptySet();
        }

    }

}

3、测试

import lombok.Builder;
import lombok.Data;

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

public class Test {

    @Data
    @Builder
    static class Student {

        private String name;

        private int term;

        private double mathScore;

        private double englishScore;

    }

    public static void main(String[] args) {

        List<Student> students = new ArrayList<Student>(){
            {
                add(Student.builder().name("张三").term(1).mathScore(90).englishScore(80).build());
                add(Student.builder().name("张三").term(2).mathScore(85).englishScore(77).build());
                add(Student.builder().name("张三").term(3).mathScore(95).englishScore(78).build());
                add(Student.builder().name("张三").term(4).mathScore(88).englishScore(75).build());
                add(Student.builder().name("张三").term(5).mathScore(95).englishScore(78).build());
                add(Student.builder().name("张三").term(6).mathScore(88).englishScore(75).build());

                add(Student.builder().name("李四").term(1).mathScore(90).englishScore(60).build());
                add(Student.builder().name("李四").term(2).mathScore(95).englishScore(67).build());
                add(Student.builder().name("李四").term(3).mathScore(98).englishScore(60).build());
                add(Student.builder().name("李四").term(4).mathScore(98).englishScore(70).build());
                add(Student.builder().name("李四").term(5).mathScore(100).englishScore(68).build());
                add(Student.builder().name("李四").term(6).mathScore(96).englishScore(75).build());
            }
        };

        //下面分组求出学生的平均分
        Map<String, Student> map = students.stream().collect(
                Collectors.groupingBy(
                        Student::getName,
                        CustomCollectors.ofAverage(
                                new AverageOperator<Student>() {
                                    @Override
                                    public Student divide(Student student, double number) {
                                        return Student.builder()
                                                .name(student.name)
                                                .mathScore(student.mathScore/number)
                                                .englishScore(student.englishScore/number)
                                                .build();
                                    }
                                    @Override
                                    public Student sum(Student t1, Student t2) {
                                        return Student.builder()
                                                .name(t1.name)
                                                .mathScore(t1.mathScore + t2.mathScore)
                                                .englishScore(t1.englishScore + t2.englishScore)
                                                .build();
                                    }
                                }
                         )

                )
        );

        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)

Q.E.D.

知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议