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);
