• 技术文章 >java >java基础

    java Reduce的三种重载

    小妮浅浅小妮浅浅2021-07-16 09:44:02原创2414

    1、一个参数的reduce

    格式

    1

    Optional<T> reduce(BinaryOperator<T> accumulator)

    1

    2

    3

    4

    5

    T result = a[0]; 

    for (int i = 1; i < n; i++) {

    result = accumulator.apply(result, a[i]); 

    }

    return result;

    2、两个参数的reduce

    格式

    1

    T reduce(T identity, BinaryOperator<T> accumulator)

    1

    2

    3

    4

    5

    T result = identity;

    for (int i = 0; i < n; i++) {

    result = accumulator.apply(result, a[i]); 

    }

    return result;

    3、三个参数的Reduce,其中get和set方法使用时省略。

    格式

    1

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

    1

    2

    3

    4

    5

    6

    7

    8

    9

    static class ScoreBean {

    private String name; //学生姓名

    private int score;   //分数,需要汇总该字段  

    public ScoreBean(String name, int score) {

    this.name = name;

    this.score = score;

    }

    //get 和 set 方法省略

    }

    以上就是java Reduce的三种重载,希望对大家有所帮助。更多Java学习指路:Java基础

    本教程操作环境:windows7系统、java10版,DELL G3电脑。

    专题推荐:java reduce
    上一篇:java Match如何使用 下一篇:java8中的四种方法引用

    相关文章推荐

    • Python中reduce函数和lambda表达式的学习• 详解Python内建函数map()和reduce()• python的reduce怎么用• python中的map和reduce有什么不同• 如何使用python中的reduce函数?• 如何用python3代码展现reduce传递参数?• python中的reduce函数是如何使用的?• python中reduce函数和map函数的区别有哪些?• python中reduce函数如何实现阶乘?• js中使用reduce()方法数组去重• java中reduce在流的使用

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网