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);
相关推荐
weixin199701080161 分钟前
[特殊字符] 1688开放平台API Sign签名算法详解(Java / Python / PHP 实现)
java·python·算法
vortex55 分钟前
Linux日志轮转管理:logrotate 完全指南
linux·运维·服务器
武子康5 分钟前
Java-22 深入浅出 MyBatis - 手写ORM框架3 手写SqlSession、Executor 工作原理
java·后端
未若君雅裁13 分钟前
JVM 垃圾回收算法与分代回收机制
java·jvm·算法
ggaofeng24 分钟前
如何通过uboot加载硬盘
linux·qemu·uboot
尔染君子29 分钟前
嵌入式Linux驱动开发(按键驱动)
linux·驱动开发
摇滚侠29 分钟前
SpringMVC 入门到实战 简介和入门案例 01-13
java·后端·spring·intellij-idea
未若君雅裁29 分钟前
JVM 垃圾回收器全景与G1深度解析
java·开发语言·jvm
霸道流氓气质30 分钟前
Java 大数据量异步处理方案:线程池 vs 消息队列
java·开发语言
devilnumber30 分钟前
想真正吃透 + 灵活运用 Java 代理模式
java·开发语言·代理模式