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);
相关推荐
Coder个人博客2 小时前
Linux6.19-ARM64 mm mmu子模块深入分析
大数据·linux·车载系统·系统架构·系统安全·鸿蒙系统
侠客行03173 小时前
Mybatis连接池实现及池化模式
java·mybatis·源码阅读
蛇皮划水怪3 小时前
深入浅出LangChain4J
java·langchain·llm
老毛肚5 小时前
MyBatis体系结构与工作原理 上篇
java·mybatis
Doro再努力5 小时前
Vim 快速上手实操手册:从入门到生产环境实战
linux·编辑器·vim
wypywyp5 小时前
8. ubuntu 虚拟机 linux 服务器 TCP/IP 概念辨析
linux·服务器·ubuntu
风流倜傥唐伯虎5 小时前
Spring Boot Jar包生产级启停脚本
java·运维·spring boot
Doro再努力5 小时前
【Linux操作系统10】Makefile深度解析:从依赖推导到有效编译
android·linux·运维·服务器·编辑器·vim
senijusene5 小时前
Linux软件编程:IO编程,标准IO(1)
linux·运维·服务器
Yvonne爱编码6 小时前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python