Java8 Stream reduce操作

Reduce,顾名思义为减少的意思,就是根据指定的计算模型将Stream中的值计算得到一个最终结果。在之前的一篇文章Java8函数式编程中简单介绍,Stream的count、min 和max方法底层都是依赖reduce实现的,本篇文章将简单介绍一下Java8 Stream reduce的几种基本用法。

首先来看一下Reduce三种形式:
S.N.     方法说明
1     Optional<T> reduce(BinaryOperator<T> accumulator);
       对Stream中的数据通过累加器accumulator迭代计算,最终得到一个Optional对象
2     T reduce(T identity, BinaryOperator<T> accumulator);
       给定一个初始值identity,通过累加器accumulator迭代计算,得到一个同Stream中数据同类型的结果
3     <U> U reduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner);
       给定一个初始值identity,通过累加器accumulator迭代计算,得到一个identity类型的结果,第三个参数用于使用并行     流时合并结果


1. Optional<T> reduce(BinaryOperator<T> accumulator);

首先看一下函数式接口BinaryOperator,继承于BiFunction,Bifunction中有一个apply方法,接收两个参数,返回一个结果。如下:

    @FunctionalInterface
    public interface BinaryOperator<T> extends BiFunction<T,T,T> {
    }
     
    @FunctionalInterface
    public interface BiFunction<T, U, R> {
        R apply(T t, U u);
    }

也就是说,reduce(BinaryOperator<T> accumulator)方法需要一个函数式接口参数,该函数式接口需要两个参数,返回一个结果(reduce中返回的结果会作为下次累加器计算的第一个参数),也就是所讲的累加器。所以reduce(BinaryOperator<T> accumulator)方法可以如下调用:

   

 @Test
    public void reduceTest() {
        Optional accResult = Stream.of(1, 2, 3, 4)
                .reduce((acc, item) -> {
                    System.out.println("acc : " + acc);
                    acc += item;
                    System.out.println("item: " + item);
                    System.out.println("acc+ : " + acc);
                    System.out.println("--------");
                    return acc;
                });
        System.out.println(accResult);
    }

运行结果:

    acc : 1
    item: 2
    acc+ : 3
    --------
    acc : 3
    item: 3
    acc+ : 6
    --------
    acc : 6
    item: 4
    acc+ : 10
    --------
    Optional[10]

2. T reduce(T identity, BinaryOperator<T> accumulator);

提供一个跟Stream中数据同类型的初始值identity,通过累加器accumulator迭代计算Stream中的数据,得到一个跟Stream中数据相同类型的最终结果,可以如下调用:

  @Test
    public void reduceTest1() {
        int accResult = Stream.of(1, 2, 3, 4)
                .reduce(100, (acc, item) -> {
                    System.out.println("acc : "  + acc);
                    acc += item;
                    System.out.println("item: " + item);
                    System.out.println("acc+ : "  + acc);
                    System.out.println("--------");
                    return acc;
                });
        System.out.println(accResult);
    }

运行结果:

    acc : 100
    item: 1
    acc+ : 101
    --------
    acc : 101
    item: 2
    acc+ : 103
    --------
    acc : 103
    item: 3
    acc+ : 106
    --------
    acc : 106
    item: 4
    acc+ : 110
    --------
    110

3. <U> U reduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner);

首先看一下BiFunction的三个泛型类型分别是U、 ? super T、U,参考BiFunction函数式接口apply方法定义可以知道,累加器累加器通过类型为U和? super T的两个输入值计算得到一个U类型的结果返回。也就是说这种reduce方法,提供一个不同于Stream中数据类型的初始值,通过累加器规则迭代计算Stream中的数据,最终得到一个同初始值同类型的结果。看一个调用示例:

 

   @Test
    public void reduceTest2() {
        ArrayList<Integer> accResult_ = Stream.of(2, 3, 4)
                .reduce(Lists.newArrayList(1),
                        (acc, item) -> {
                            acc.add(item);
                            System.out.println("item: " + item);
                            System.out.println("acc+ : " + acc);
                            System.out.println("BiFunction");
                            return acc;
                        }, (acc, item) -> {
                            System.out.println("BinaryOperator");
                            acc.addAll(item);
                            System.out.println("item: " + item);
                            System.out.println("acc+ : " + acc);
                            System.out.println("--------");
                            return acc;
                        }
                );
        System.out.println("accResult_: " + accResult_);
    }

运行结果:

    item: 2
    acc+ : [1, 2]
    BiFunction
    item: 3
    acc+ : [1, 2, 3]
    BiFunction
    item: 4
    acc+ : [1, 2, 3, 4]
    BiFunction
    accResult_: [1, 2, 3, 4]

通过运行结果可以看出,第三个参数定义的规则并没有执行。这是因为reduce的第三个参数是在使用parallelStream的reduce操作时,合并各个流结果的,本例中使用的是stream,所以第三个参数是不起作用的。上述示例,提供一个只有一个元素1的arrayList,通过累加器迭代,将stream中的数据添加到arrayList中。

以上就是stream中reduce的三种用法,用来通过特定的规则计算stream中的值,得到一个最终结果,其实还是很简单的。推荐一篇刚看到的讲解Java8 Stream的文章,Java Stream 详解,我陆陆续续也写了好几篇关于Java8新特性的文章了,但是比较下来并没有上面那篇文写的那么体系,之后的文章可以借鉴一下那篇文章的组织方法。

参考链接:

    1. Java 8系列之Stream中万能的reduce
    2. Java8初体验(二)Stream语法详解
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值