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<Student> 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<String, AbstractMap.SimpleEntry<Double, Double>> 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<String,List> 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<String,List<Map<String,Double>>> map2 = studentList.stream()
                .collect(new AggregateCollector());
        System.out.println(map2);

        // 自定义collector
        System.out.println("---------map3---------");
        Map<String, List<Map<String, Double>>> 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"));
    }

}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黄鸭and小黑鸭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值