java中各种类型用Stream流求最大值最小值方式

 更新时间:2025年06月23日 08:47:47   作者:yololee_  
这篇文章主要介绍了java中各种类型用Stream流求最大值最小值方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

java中各种类型用Stream流求最大值最小值

一、BigDecimal 求最大值和最小值

1. stream().reduce()实现

        List<BigDecimal> list = new ArrayList<>(Arrays.asList(new BigDecimal("1"), new BigDecimal("2")));
        BigDecimal max = list.stream().reduce(list.get(0), BigDecimal::max);
        BigDecimal min = list.stream().reduce(list.get(0), BigDecimal::min);

2. stream().max()或stream().min()实现

        List<BigDecimal> list = new ArrayList<>(Arrays.asList(new BigDecimal("1"), new BigDecimal("2")));
        BigDecimal max = list.stream().max(Comparator.comparing(x -> x)).orElse(null);
        BigDecimal min = list.stream().min(Comparator.comparing(x -> x)).orElse(null);

二、Integer 求最大值和最小值

1. stream().reduce()实现

        List<Integer> list = new ArrayList<>(Arrays.asList(1, 2));
        Integer max = list.stream().reduce(list.get(0), Integer::max);
        Integer min = list.stream().reduce(list.get(0), Integer::min);

2. Collectors.summarizingInt()实现

        List<Integer> list = new ArrayList<>(Arrays.asList(1, 2));
        IntSummaryStatistics intSummaryStatistics = list.stream().collect(Collectors.summarizingInt(x -> x));
        Integer max = intSummaryStatistics.getMax();
        Integer min = intSummaryStatistics.getMin();

3. stream().max()或stream().min()实现

        List<Integer> list = new ArrayList<>(Arrays.asList(1, 2));
        Integer max = list.stream().max(Comparator.comparing(x -> x)).orElse(null);
        Integer min = list.stream().min(Comparator.comparing(x -> x)).orElse(null);

三、Long 求最大值和最小值

1. stream().reduce()实现

        List<Long> list = new ArrayList<>(Arrays.asList(1L, 2L));
        Long max = list.stream().reduce(list.get(0), Long::max);
        Long min = list.stream().reduce(list.get(0), Long::min);

2. Collectors.summarizingLong()实现

        List<Long> list = new ArrayList<>(Arrays.asList(1L, 2L));
        LongSummaryStatistics summaryStatistics = list.stream().collect(Collectors.summarizingLong(x -> x));
        Long max = summaryStatistics.getMax();
        Long min = summaryStatistics.getMin();

3. stream().max()或stream().min()实现

        List<Long> list = new ArrayList<>(Arrays.asList(1L, 2L));
        Long max = list.stream().max(Comparator.comparing(x -> x)).orElse(null);
        Long min = list.stream().min(Comparator.comparing(x -> x)).orElse(null);

四、Double 求最大值和最小值

1. stream().reduce()实现

        List<Double> list = new ArrayList<>(Arrays.asList(1d, 2d));
        Double max = list.stream().reduce(list.get(0), Double::max);
        Double min = list.stream().reduce(list.get(0), Double::min);

2. Collectors.summarizingLong()实现

        List<Double> list = new ArrayList<>(Arrays.asList(1d, 2d));
        DoubleSummaryStatistics summaryStatistics = list.stream().collect(Collectors.summarizingDouble(x -> x));
        Double max = summaryStatistics.getMax();
        Double min = summaryStatistics.getMin();

3. stream().max()或stream().min()实现

        List<Double> list = new ArrayList<>(Arrays.asList(1d, 2d));
        Double max = list.stream().max(Comparator.comparing(x -> x)).orElse(null);
        Double min = list.stream().min(Comparator.comparing(x -> x)).orElse(null);

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • springboot项目中jacoco服务端部署使用

    springboot项目中jacoco服务端部署使用

    这篇文章主要为大家介绍了springboot项目中jacoco服务端部署使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • java爬虫之使用HttpClient模拟浏览器发送请求方法详解

    java爬虫之使用HttpClient模拟浏览器发送请求方法详解

    这篇文章主要介绍了java爬虫之使用HttpClient模拟浏览器发送请求方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • Spring之什么是ObjectFactory?什么是ObjectProvider?

    Spring之什么是ObjectFactory?什么是ObjectProvider?

    这篇文章主要介绍了Spring之什么是ObjectFactory?什么是ObjectProvider?具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • Spring boot 运用策略模式实现避免多次使用if

    Spring boot 运用策略模式实现避免多次使用if

    这篇文章主要介绍了Spring boot 运用策略模式实现避免多次使用if,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-09-09
  • java实现简单TCP聊天程序

    java实现简单TCP聊天程序

    这篇文章主要为大家详细介绍了java实现简单TCP聊天程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • Java使用JavaMail API实现原生邮箱发送功能的方法

    Java使用JavaMail API实现原生邮箱发送功能的方法

    文章介绍了如何使用JavaMailAPI在Java后端实现原生邮箱发送功能,包括配置邮件服务器、获取校验码以及解决SSL握手异常的问题,需要的朋友可以参考下
    2026-02-02
  • 带你了解Java数据结构和算法之高级排序

    带你了解Java数据结构和算法之高级排序

    这篇文章主要为大家介绍了Java数据结构和算法之高级排序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-01-01
  • Java 本地方法Native Method详细介绍

    Java 本地方法Native Method详细介绍

    这篇文章主要介绍了 Java 本地方法Native Method详细介绍的相关资料,需要的朋友可以参考下
    2017-02-02
  • java中Set接口常用方法详解

    java中Set接口常用方法详解

    在Java编程中Set接口是集合框架的一部分,它继承自Collection接口,主要用于存储不包含重复元素的集合,这篇文章主要介绍了java中Set接口常用方法的相关资料,需要的朋友可以参考下
    2026-03-03
  • SpringCloud 分布式锁的多种实现

    SpringCloud 分布式锁的多种实现

    本文主要介绍了SpringCloud 分布式锁的多种实现,主要有三种方式,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04

最新评论