Lamda的深入认知(九)

主题:收集器

一、需求

  1. 需要更加强大的reduce操作(收集器的实质是redcue操作的包装体)
  2. Stream的reduce方法,也可以将数据reduce到加复杂的结果上Lamda的深入认知(七),但我们更加需要库一级的reduce操作!!!

二、API布局

  1. 由Collector接口的实现类,完成reduce的算法行为。
  2. 由Collectors扮演Collector的工厂,其工厂方法中会传入lamda(通常会这样),从而产生由用户定制的Collector。
  3. 将Collector传递给Stream的collect方法
  4. collect方法为终端操作,产生结果。
  5. Demo:
        //将数据收集到Set中
        Arrays.asList(2,-3,1).stream()
                .collect(Collectors.toSet());
        
        // 将数据收集到定制的set
        Arrays.asList(2,-3,1).stream()
                .collect(Collectors.toCollection(LinkedHashSet::new));

三、常见的收集器

1. 计数统计:

Collectors.counting()

2. 简单算数统计:
  • 原型:
        Collectors.summarizingInt(ToIntFunction<T>);
        Collectors.summarizingLong(ToLongFunction<T>);
        Collectors.summarizingDouble(ToDoubleFunction<T>);

        Collectors.averagingInt(ToIntFunction<T>);
        Collectors.averagingLong(ToLongFunction<T>);
        Collectors.averagingDouble(ToDoubleFunction<T>);
  • demo: 统计数组中元素的个数
        int len=Arrays.asList(new int[]{1,2,8,1},new int[]{4,8,1})
                .stream()
                .collect(Collectors.summingInt(s->s.length));
3. 综合统计:
  • 原型:
Collectors.summarizingInt(ToIntFunction<T>)
Collectors.summarizingLong(ToLongFunction<T>)
Collectors.summarizingDouble(ToDoubleFunction<T>)
  • demo:
IntSummaryStatistics mm = Arrays.asList(new int[]{1, 2, 8, 1}, new int[]{4, 8, 1}).stream()
                .collect(Collectors.summarizingInt(ary->ary.length));

//结果: IntSummaryStatistics{count=2, sum=7, min=3, average=3.500000, max=4}
4. 收集到集合对象中
  • 原型:
public static <T> Collector<T, ?, List<T>> toList()

public static <T> Collector<T, ?, Set<T>> toSet()

public static <T, C extends Collection<T>> Collector<T, ?, C> toCollection(Supplier<C> collectionFactory)
  • demo:
5. 找出最大及最小值
  • 原型:
public static <T> Collector<T, ?, Optional<T>> maxBy(Comparator<? super T> comparator)
  • demo:
        //两种不同的方式
        String r1=Arrays.asList("abc","x","cdef","dd").stream()
                .collect(Collectors.maxBy((s1,s2)->s1.length()-s2.length()))
                .get();
        
        String r2=Arrays.asList("abc","x","cdef","dd").stream()
                .collect(Collectors.maxBy(Comparator.comparing(String::length)))
                .get();
        //结果:cdef
6. 连接操作
  • 原型:
public static Collector<CharSequence, ?, String> joining(CharSequence delimiter)
  • demo:
        String r3=Arrays.asList("abc","x","cdef","dd").stream()
                .collect(Collectors.joining("-"));
        //结果:abc-x-cdef-dd

7. 广义reduce: 功能最强大的Collector(除自定制Collector外)

  • 原型:
public static <T, U> Collector<T, ?, U> reducing(U identity, 
  Function<? super T, ? extends U> mapper, BinaryOperator<U> op)
  • demo:统计字符的总个数
        int totalLen1=Arrays.asList("abc","x","cdef","dd").stream()
                .collect(Collectors.reducing(0,s->s.length(),(a,b)->a+b));
        int totalLen2=Arrays.asList("abc","x","cdef","dd").stream()
                .collect(Collectors.reducing(0,String::length,Integer::sum));
        //result:10
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Int Double Long 设置特定的stream类型, 提高性能,增加特定的函数 无存储。stream不是一...
    patrick002阅读 5,014评论 0 0
  • 原文地址: 深蓝至尊 一. 流式处理简介 在我接触到java8流式处理的时候,我的第一感觉是流式处理让集合操作变得...
    咻咻咻i阅读 4,862评论 0 0
  • 本文采用实例驱动的方式,对JAVA8的stream API进行一个深入的介绍。虽然JAVA8中的stream AP...
    浮梁翁阅读 25,943评论 3 50
  • 原文地址 http://blog.csdn.net/myherux/article/details/7185511...
    Vissioon阅读 3,994评论 0 0
  • 1.流的基本概念 1.1 什么是流? 流是Java8引入的全新概念,它用来处理集合中的数据,暂且可以把它理解为一种...
    升空的焰火阅读 5,298评论 0 1