Java - reduce函数的应用

时间:2021-08-23
本文章向大家介绍Java - reduce函数的应用,主要包括Java - reduce函数的应用使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

前言

记录下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 -
梦想是咸鱼
关注一下吧
以上为本篇文章的主要内容,希望大家多提意见,如果喜欢记得点个推荐哦
本文版权归作者和博客园共有,欢迎转载,转载时保留原作者和文章地址即可。

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