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

1 篇文章 0 订阅

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值