首页 > 编程语言 > 详细

Java - reduce函数的应用

时间:2021-08-23 22:59:44      阅读:23      评论:0      收藏:0      [点我收藏+]

前言

记录下reduce函数的简单用法,其用作从一个流中生成一个值。


具体应用

public static void main(String[] args) {

        List<Integer> arrayList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

        System.out.println(arrayList); // [1, 2, 3, 4, 5, 6, 7, 8, 9]

        /**
         * 求和
         */
        Integer sum = arrayList.stream().reduce(Integer::sum).orElse(0);
        System.out.println(sum); // 45

        /**
         * 求最大值
         */
        Integer max = arrayList.stream().reduce(Integer::max).orElse(0);
        System.out.println(max); // 9

        /**
         * 求最小值
         */
        Integer min = arrayList.stream().reduce(Integer::min).orElse(0);
        System.out.println(min); // 1

    }

- End -
梦想是咸鱼
关注一下吧
技术分享图片

Java - reduce函数的应用

原文:https://www.cnblogs.com/maggieq8324/p/15177407.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!