zoukankan      html  css  js  c++  java
  • [java]Stream API——collect、reduce、orElse(x)

    一、collect

    1、R collect(Supplier supplier, BiConsumer accumulator, BiConsumer combiner)

    supplier:一个能创造目标类型实例的方法。

    accumulator:一个将当元素添加到目标中的方法。

    combiner:一个将中间状态的多个结果整合到一起的方法(并发的时候会用到)

    List result = stream.collect(() -> new ArrayList<>(), (list, item) -> list.add(item), (one, two) -> one.addAll(two));

    2、R collect(Collector collector)

    Collector其实是上面supplier、accumulator、combiner的聚合体。那么上面代码就变成:

    List list = Stream.of(1, 2, 3, 4).filter(p -> p > 2).collect(Collectors.toList());

     

    3、Collector

    Collector是Stream的可变减少操作接口,Collectors(类收集器)提供了许多常见的可变减少操作的实现。

     

    4、创建Collector

    a.转换成其他集合:toList、toSet、toCollection、toMap

    List<Integer> collectList = Stream.of(1, 2, 3, 4)
            .collect(Collectors.toList());
    System.out.println("collectList: " + collectList);
    // 打印结果
    // collectList: [1, 2, 3, 4]

     b.转成值:

    使用collect可以将Stream转换成值。maxBy和minBy允许用户按照某个特定的顺序生成一个值。

      • averagingDouble:求平均值,Stream的元素类型为double
      • averagingInt:求平均值,Stream的元素类型为int
      • averagingLong:求平均值,Stream的元素类型为long
      • counting:Stream的元素个数
      • maxBy:在指定条件下的,Stream的最大元素
      • minBy:在指定条件下的,Stream的最小元素
      • reducing: reduce操作
      • summarizingDouble:统计Stream的数据(double)状态,其中包括count,min,max,sum和平均。
      • summarizingInt:统计Stream的数据(int)状态,其中包括count,min,max,sum和平均。
      • summarizingLong:统计Stream的数据(long)状态,其中包括count,min,max,sum和平均。
      • summingDouble:求和,Stream的元素类型为double
      • summingInt:求和,Stream的元素类型为int
      • summingLong:求和,Stream的元素类型为long 

    例:

    Optional<Integer> collectMaxBy = Stream.of(1, 2, 3, 4)
                .collect(Collectors.maxBy(Comparator.comparingInt(o -> o)));
    System.out.println("collectMaxBy:" + collectMaxBy.get());
    // 打印结果
    // collectMaxBy:4

    c.分割数据块:Collectors.partitioningBy

     Map<Boolean, List<Integer>> collectParti = Stream.of(1, 2, 3, 4)
                .collect(Collectors.partitioningBy(it -> it % 2 == 0));
    System.out.println("collectParti : " + collectParti);
    // 打印结果
    // collectParti : {false=[1, 3], true=[2, 4]}

    d.数据分组:Collectors.groupingBy 

    Map<Boolean, List<Integer>> collectGroup= Stream.of(1, 2, 3, 4)
                .collect(Collectors.groupingBy(it -> it > 3));
    System.out.println("collectGroup : " + collectGroup);
    // 打印结果
    // collectGroup : {false=[1, 2, 3], true=[4]}

    e.字符串:Collectors.joining

    String strJoin = Stream.of("1", "2", "3", "4")
            .collect(Collectors.joining(",", "[", "]"));
    System.out.println("strJoin: " + strJoin);
    // 打印结果
    // strJoin: [1,2,3,4]

    二、聚合操作reduce

    T reduce(T identity, BinaryOperator accumulator)

    代码:

    int value = Stream.of(1, 2, 3, 4).reduce(100, (sum, item) -> sum + item);

    或者使用方法引用

    int value = Stream.of(1, 2, 3, 4).reduce(100, Integer::sum);

    value结果:101,103,106,110

    T reduce(T identity, BinaryOperator accumulator)

    • identity:它允许用户提供一个循环计算的初始值。(例中的100)
    • accumulator:计算的累加器,其方法签名为apply(T t,U u),在该reduce方法中第一个参数t(例中的sum)为上次函数计算的返回值,第二个参数u(例中的item)为Stream中的元素,这个函数把这两个值计算apply,得到的和会被赋值给下次执行这个方法的第一个参数。

    三、orElse(x):属于Stream终结操作,与findFirst()组合使用,返回对象,如果没有,则返回x。

    StCompanySetting stCompanySetting = stCompanySettingDao.selectAll().stream().findFirst().orElse(null);//如果没有,则返回null
  • 相关阅读:
    Mysql推荐使用规范
    程序员应该经常浏览的技术网站
    百度,腾讯,阿里等互联网公司年终奖发多少
    JNI技术详解,让程序有飞一般的感觉
    日志:分布式系统的核心
    Spring Boot七:Spring boot集成MyBatis
    通俗理解TCP的三次握手
    JDBC添加数据
    JDBC概念
    今天是阳光明媚的一天
  • 原文地址:https://www.cnblogs.com/vickylinj/p/9489296.html
Copyright © 2011-2022 走看看