JAVA获取一个LIST中的最大值

JAVA获取一个LIST中的最大值

方法一:

Voucher max = null;

if (list != null && !list.isEmpty() && list.size() > 0) {

max = list.get(0);

for (Voucher v : list) {

if (Double.parseDouble(v.getAmountCny()) >= Double.parseDouble(max.getAmountCny())) {

max = v;

}

}

}else if(list != null && !list.isEmpty() && list.size() == 0) {

max = list.get(0);

}

java 复制代码
   Voucher max = null;
        if (list != null && !list.isEmpty() && list.size() > 0) {
            
            
            max = list.get(0);
            for (Voucher v : list) {
                if (Double.parseDouble(v.getAmountCny()) >=  Double.parseDouble(max.getAmountCny())) {
                    max = v;
                }
            }
        }else if(list != null && !list.isEmpty() && list.size() == 0) {
            max = list.get(0);
            
        }

方法二:使用 Stream API

Voucher max = list.stream().max(Comparator.comparing(Voucher::getAmountCny)).orElse(null); // 如果列表为空,返回null

java 复制代码
Voucher max = list.stream().max(Comparator.comparing(Voucher::getAmountCny)).orElse(null);
相关推荐
JIngJaneIL1 小时前
基于Java失物招领系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·vue
程序员岳焱1 小时前
Java泛型高级玩法:通配符、上下界与类型擦除避坑实战(纯干货,附完整工具类)
java·后端·程序员
期待のcode1 小时前
MyBatis-Plus基本CRUD
java·spring boot·后端·mybatis
❀͜͡傀儡师1 小时前
maven 仓库的Central Portal Namespaces 怎么验证
java·maven·nexus
豐儀麟阁贵1 小时前
9.3获取字符串信息
java·开发语言·前端·算法
YJlio1 小时前
第9章小结(9.19):Sysinternals 安全工具组合拳与脚本清单
java·学习·平面
Orange_sparkle1 小时前
Windows/Linux离线部署IndexTTS2
linux·运维·服务器
甜鲸鱼1 小时前
【Spring Boot + OpenAPI 3】开箱即用的 API 文档方案(SpringDoc + Knife4j)
java·spring boot·后端
robch1 小时前
Java后端优雅的实现分页搜索排序-架构2
java·开发语言·架构