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

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

一、BigDecimal 求最大值和最小值

1. stream().reduce()实现

java 复制代码
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()实现

java 复制代码
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()实现

java 复制代码
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()实现

java 复制代码
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()实现

java 复制代码
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()实现

java 复制代码
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()实现

java 复制代码
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()实现

java 复制代码
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()实现

java 复制代码
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()实现

java 复制代码
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()实现

java 复制代码
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);
相关推荐
王嘉俊9252 分钟前
设计模式--享元模式:优化内存使用的轻量级设计
java·设计模式·享元模式
小蕾Java7 分钟前
PyCharm 软件使用各种问题 ,解决教程
ide·python·pycharm
Lucky_Turtle9 分钟前
【PyCharm】设置注释风格,快速注释
python
kunge1v527 分钟前
学习爬虫第四天:多任务爬虫
爬虫·python·学习·beautifulsoup
萧鼎36 分钟前
Python schedule 库全解析:从任务调度到自动化执行的完整指南
网络·python·自动化
2301_803554521 小时前
C++联合体(Union)详解:与结构体的区别、联系与深度解析
java·c++·算法
EnCi Zheng1 小时前
SpringBoot 配置文件完全指南-从入门到精通
java·spring boot·后端
烙印6011 小时前
Spring容器的心脏:深度解析refresh()方法(上)
java·后端·spring
为什么我不是源代码1 小时前
JPA读取数据库离谱问题-No property ‘selectClassByName‘ found-Not a managed type
java·sql
Lisonseekpan1 小时前
Guava Cache 高性能本地缓存库详解与使用案例
java·spring boot·后端·缓存·guava