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);
相关推荐
rannn_1117 小时前
【力扣hot100】链表专题|160、206、234、141、142
java·算法·leetcode·链表·面试·开发
leisoo80977 小时前
100GBA股股票数据怎么存ClickHouseRedisMySQLJSON完整对比
大数据·linux·服务器·开发语言·python
青瓦梦滋8 小时前
传输层UDP/TCP协议
linux·网络·c++·网络协议·tcp/ip·udp
IKUN家族8 小时前
Spring IoC&DI
java·spring·rpc
ltl8 小时前
机密计算:SGX、SEV-SNP、TDX 与 ARM CCA
linux
ltl8 小时前
互联与网络:NVLink、InfiniBand、RoCE 与国产替代
linux
码上上班8 小时前
tengine知识点
linux·服务器·centos
山荷枝10 小时前
03-框架--Spring
java·后端·spring
ChaHae-In10 小时前
SpringBoot统一功能处理
java·spring boot·后端
coder!mq10 小时前
说几个常见的语法糖?
java·开发语言·算法