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);
相关推荐
云原生指北10 小时前
GitHub Copilot SDK 入门:五分钟构建你的第一个 AI Agent
java
Leinwin14 小时前
OpenClaw 多 Agent 协作框架的并发限制与企业化规避方案痛点直击
java·运维·数据库
薛定谔的悦14 小时前
MQTT通信协议业务层实现的完整开发流程
java·后端·mqtt·struts
如意.75914 小时前
【Linux开发工具实战】Git、GDB与CGDB从入门到精通
linux·运维·git
enjoy嚣士14 小时前
springboot之Exel工具类
java·spring boot·后端·easyexcel·excel工具类
Thera77715 小时前
C++ 高性能时间轮定时器:从单例设计到 Linux timerfd 深度优化
linux·开发语言·c++
罗超驿15 小时前
独立实现双向链表_LinkedList
java·数据结构·链表·linkedlist
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ16 小时前
Linux 查询某进程文件所在路径 命令
linux·运维·服务器
盐水冰16 小时前
【烘焙坊项目】后端搭建(12) - 订单状态定时处理,来单提醒和顾客催单
java·后端·学习
凸头16 小时前
CompletableFuture 与 Future 对比与实战示例
java·开发语言