java8Stream实现多聚合

package com.kexin.aggregate;

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;

/**
 * @Author KeXin
 * @Date 2018/7/25 上午9:49
 **/
public class AggregateCollector1 implements Collector<Student,List<Map<String,Double>>,List<Map<String,Double>>> {


    @Override
    public Supplier<List<Map<String, Double>>> supplier() {
        return () -> {
            List<Map<String,Double>> list = combiner().apply(new ArrayList<>(),null);
            return list;
        };
    }

    @Override
    public BiConsumer<List<Map<String, Double>>, Student> accumulator() {
        return (list, student) -> {

            Map<String,Double> map_sum = new HashMap<>();
            Map<String,Double> map_ave = new HashMap<>();
            Map<String,Double> map_count = new HashMap<>();

            if (list.size()==0) {
                list.add(map_sum);
                list.add(map_ave);
                list.add(map_count);
            }
            map_sum = list.get(0);
            map_sum.put("sum",map_sum.getOrDefault("sum",0.0)+student.getGrade());
            list.set(0,map_sum);

            map_count = list.get(2);
            map_count.put("count",map_count.getOrDefault("count",0.0)+1);
            list.set(2,map_count);

            map_ave = list.get(1);
            map_ave.put("ave",map_sum.getOrDefault("sum",0.0)/map_count.get("count"));
            list.set(1,map_ave);
        };
    }

    @Override
    public BinaryOperator<List<Map<String, Double>>> combiner() {
        return (left, right) -> {
            if (left != null) {
                return left;
            }
            return right;
        };
    }

    @Override
    public Function<List<Map<String, Double>>, List<Map<String, Double>>> finisher() {
        return res -> res;
    }

    @Override
    public Set<Characteristics> characteristics() {
        return Collections.emptySet();
    }
}
package com.kexin.aggregate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author KeXin
 * 使用java8 stream实现groupBy的多聚合函数
 * 继承Collector接口,自定义collector
 * @Date 2018/7/24 下午7:22
 **/
public class Aggregate {

    public static void main(String[] args) {
        List studentList = new ArrayList<>();
        studentList.add(new Student("1","1",10));
        studentList.add(new Student("3","1",10));
        studentList.add(new Student("5","1",60));

        studentList.add(new Student("2","2",20));
        studentList.add(new Student("6","2",20));

        studentList.add(new Student("4","3",20));

        //单个
        System.out.println("---------单个聚合函数---------");
        System.out.println(studentList.stream().collect(Collectors.groupingBy(Student::getGlass,Collectors.counting())));

        //两个
        System.out.println("---------collectingAndThen两个聚合函数(stream次数多)---------");
        Map.SimpleEntry> map = studentList.stream()
                .collect(Collectors.groupingBy(Student::getGlass,
                        Collectors.collectingAndThen(Collectors.toList(), list -> {
                            double sum = list.stream().mapToInt(Student::getGrade).sum();
                            double average = list.stream().collect(
                                    Collectors.averagingDouble(Student::getGrade));
                            return new AbstractMap.SimpleEntry<>(sum, average);
                        })));

        System.out.println(map);

        System.out.println("---------map1---------");
        //多个
        Map map1 = studentList.stream()
                .collect(Collectors.groupingBy(Student::getGlass,
                        Collectors.collectingAndThen(Collectors.toList(), list -> {
                            double sum = list.stream().mapToInt(Student::getGrade).sum();
                            double average = list.stream().collect(
                                    Collectors.averagingDouble(Student::getGrade));
                            double count = list.stream().count();

                            return Arrays.asList(sum,average,count);
                        }
                        )));
        System.out.println(map1);


        // 自定义collector,collector内指定groupBy元素
        System.out.println("---------map2---------");
        Map>> map2 = studentList.stream()
                .collect(new AggregateCollector());
        System.out.println(map2);

        // 自定义collector
        System.out.println("---------map3---------");
        Map>> map3 = studentList.stream()
                .collect(Collectors.groupingBy(Student::getGlass,new AggregateCollector1()));
        System.out.println(map3);
        System.out.println("---------取数据样例---------");
        System.out.println("1班:"+map3.get("1"));
        System.out.println("1班总分:"+map3.get("1").get(0).get("sum"));
    }

}

你可能感兴趣的:(java高级特性)