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);
相关推荐
num_killer8 小时前
小白的Langchain学习
java·python·学习·langchain
SunkingYang8 小时前
QT编译报错:使用Lambda表达式作为槽函数,报错‘xxx‘ in capture list does not name a variable
qt·list·报错·lambda表达式·槽函数·in capture list·does not name
wdfk_prog8 小时前
[Linux]学习笔记系列 -- hashtable
linux·笔记·学习
期待のcode9 小时前
Java虚拟机的运行模式
java·开发语言·jvm
程序员老徐9 小时前
Tomcat源码分析三(Tomcat请求源码分析)
java·tomcat
a程序小傲9 小时前
京东Java面试被问:动态规划的状态压缩和优化技巧
java·开发语言·mysql·算法·adb·postgresql·深度优先
仙俊红9 小时前
spring的IoC(控制反转)面试题
java·后端·spring
阿湯哥9 小时前
AgentScope Java 集成 Spring AI Alibaba Workflow 完整指南
java·人工智能·spring
CheungChunChiu9 小时前
Linux 内核动态打印机制详解
android·linux·服务器·前端·ubuntu
小楼v9 小时前
说说常见的限流算法及如何使用Redisson实现多机限流
java·后端·redisson·限流算法