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 小时前
Linux和window共享文件夹
linux
程序员清风12 小时前
程序员兼职必看:靠谱软件外包平台挑选指南与避坑清单!
java·后端·面试
皮皮林55113 小时前
利用闲置 Mac 从零部署 OpenClaw 教程 !
java
木心月转码ing17 小时前
WSL+Cpp开发环境配置
linux
华仔啊19 小时前
挖到了 1 个 Java 小特性:var,用完就回不去了
java·后端
SimonKing19 小时前
SpringBoot整合秘笈:让Mybatis用上Calcite,实现统一SQL查询
java·后端·程序员
日月云棠1 天前
各版本JDK对比:JDK 25 特性详解
java
用户8307196840821 天前
Spring Boot 项目中日期处理的最佳实践
java·spring boot
JavaGuide2 天前
Claude Opus 4.6 真的用不起了!我换成了国产 M2.5,实测真香!!
java·spring·ai·claude code
IT探险家2 天前
Java 基本数据类型:8 种原始类型 + 数组 + 6 个新手必踩的坑
java