Java8中Stream流求最大值最小值的实现示例

 更新时间:2023年04月25日 11:06:14   作者:Archie_java  
本文主要介绍了Java8中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);

到此这篇关于Java8中Stream流求最大值最小值的实现示例的文章就介绍到这了,更多相关Java8 Stream流求最大值最小值内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • List对象去重和按照某个字段排序的实现方法

    List对象去重和按照某个字段排序的实现方法

    下面小编就为大家带来一篇List对象去重和按照某个字段排序的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • spring boot idea maven依赖找不到问题处理方法

    spring boot idea maven依赖找不到问题处理方法

    这篇文章主要介绍了spring boot idea 偶尔maven依赖找不到问题,这里总结了几种处理方法,方便尝试排查,对spring boot idea  maven依赖找不到问题感兴趣的朋友跟随小编一起看看吧
    2023-08-08
  • 懒人 IDEA 插件推荐: EasyCode 一键帮你生成所需代码(Easycode用法)

    懒人 IDEA 插件推荐: EasyCode 一键帮你生成所需代码(Easycode用法)

    这篇文章主要介绍了懒人 IDEA 插件推荐: EasyCode 一键帮你生成所需代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • 详解IDEA启动多个微服务的配置方法

    详解IDEA启动多个微服务的配置方法

    这篇文章主要介绍了详解IDEA启动多个微服务的配置方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • HashMap和Hashtable的详细比较

    HashMap和Hashtable的详细比较

    这篇文章主要介绍了HashMap和Hashtable的详细比较的相关资料,需要的朋友可以参考下
    2017-04-04
  • SpringBoot 版本兼容性问题解决

    SpringBoot 版本兼容性问题解决

    本文详细介绍了SpringBoot版本兼容性问题的常见场景,包括与SpringFramework、依赖库、JDK、SpringCloud及插件及工具的兼容性问题,帮助开发者避免兼容性问题,确保项目的稳定性和可维护性
    2024-10-10
  • SpringBoot开发案例 分布式集群共享Session详解

    SpringBoot开发案例 分布式集群共享Session详解

    这篇文章主要介绍了SpringBoot开发案例 分布式集群共享Session详解,在分布式系统中,为了提升系统性能,通常会对单体项目进行拆分,分解成多个基于功能的微服务,可能还会对单个微服务进行水平扩展,保证服务高可用,需要的朋友可以参考下
    2019-07-07
  • java实现门禁系统

    java实现门禁系统

    这篇文章主要为大家详细介绍了java实现门禁系统的实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • Java文件操作类 File实现代码

    Java文件操作类 File实现代码

    这篇文章主要介绍了Java文件操作类 File实现代码,需要的朋友可以参考下
    2017-08-08
  • JavaWeb中的常用的请求传参注解说明

    JavaWeb中的常用的请求传参注解说明

    这篇文章主要介绍了JavaWeb中的常用的请求传参注解说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04

最新评论