Java 流收集器异常强大,通常可以有以下等方面的操作:
Collectors.groupingBy() 分组;Collectors.counting() 计数;分组排序再计算;并行分组处理
平常使用代码案例
分组操作 :
原型:Collector<T,?,Map<K,List<T>>> groupingBy(Function<? super T,? extends K> classifier)
实例:Map<Integer, List<CanOrgBean>> groupCanOrgBean = canList.stream().collect(Collectors.groupingBy(canOrgBean -> canOrgBean.getCanType()));
计数:
实例:Map<Integer, Long> groupCanOrgBeanCount = canList.stream().collect(Collectors.groupingBy(CanOrgBean::getCanType, Collectors.counting()));
分组排序再计算:
原型:Collector<T,?,M> groupingBy(Function<? super T,? extends K> classifier, Supplier<M> mapFactory, Collector<? super T,A,D> downstream)
实例:TreeMap<Integer, Double> collect = canList.stream().collect(Collectors.groupingBy(CanOrgBean::getCanType, TreeMap::new, Collectors.averagingDouble(CanOrgBean::getCanValue)));
后续.......