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

## 1、首先定义两个接口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() {
}

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

@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>(){
{

}
};

//下面分组求出学生的平均分
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)


11-17 6199

07-18 3万+
04-21 2343
12-31 1859
12-10 1853
07-18 8688
02-10 5万+
08-08 9423
07-06 2万+
09-14 8219
01-28 1万+