gpt4 book ai didi

java - 按日期排序对象列表并应用过滤器

转载 作者:搜寻专家 更新时间:2023-10-31 19:51:47 27 4
gpt4 key购买 nike

我有付款 list :

Payment 1
CountyTaxAmount = 250.00
CityTaxAmount = 101.00
LienAmount = 0.00
HazardAmount = 0.00
PaymentDueDate = "2018-06-01"

Payment 2
CountyTaxAmount = 10.00
CityTaxAmount = 20.00
LienAmount = 0.00
HazardAmount = 0.00
PaymentDueDate = "2018-05-01"

我创建了一个接受此列表和 currentDueDate 的函数。如果 paymentDueDate 等于之前 currentDueDate 并且最接近 currentDueDate,我想在我的计算中使用该行。

出于某种原因,我的排序无法正常工作。有人可以阐明我做错了什么。这是我的代码:

private EscrowStatusEnum determineEscrowStatus(Payment pcm, LocalDate currentDueDate) {
EscrowStatusEnum escrowStatus = null;

if(currentDueDate!= null && pcm!=null
&& pcm.getPayment() != null
&& !pcm.getPayment().isEmpty()) {

Predicate<Payment> pcmRow =
it->it.getPaymentDueDate()!=null && !it.getPaymentDueDate().isAfter(currentDueDate);

final Payment sortedRow =
pcm.getPayment().stream().sorted((el1, el2) -> el1.getPaymentDueDate().compareTo(el2.getPaymentDueDate())).
filter(pcmRow).findFirst().orElse(null);

if(sortedRow != null) {

BigDecimal countyCityLienHazardSum = sortedRow.getCountyTaxAmount().add(sortedRow.getCityTaxAmount()).add(sortedRow.getLienAmount()).add(sortedRow.getHazardAmount());
BigDecimal countyCityLienSum = sortedRow.getCountyTaxAmount().add(sortedRow.getCityTaxAmount()).add(sortedRow.getLienAmount());

if(countyCityLienHazardSum.compareTo(BigDecimal.ZERO) == 0)
escrowStatus = EscrowStatusEnum.NONESCROWED;
else if(countyCityLienSum.compareTo(BigDecimal.ZERO) > 0 && sortedRow.getHazardAmount().compareTo(BigDecimal.ZERO) == 0 ||
countyCityLienSum.compareTo(BigDecimal.ZERO) >= 0 && sortedRow.getHazardAmount().compareTo(BigDecimal.ZERO) > 0)
escrowStatus = EscrowStatusEnum.ESCROWED;
}
}

return escrowStatus;
}

当我传入 "2018-06-01"currentDueDate 时,我希望我的代码返回 Payment 1

目前正在返回Payment 2

如果我从测试中删除 Payment 2,它会返回 Payment 1

所以排序一定有问题。

最佳答案

您的排序返回最早 日期。您想要的是早于截止日期的最新 日期。

要查找流中的最小值或最大值,请不要使用 sort(...).findFirst()。使用 maxmin反而。在你的情况下:

sortedRow = pcm.getPayment().stream()
.filter(pcmRow)
.max(Comparator.comparing(Payment::getPaymentDueDate))
.orElse(null); // not relevant to your question but consider not using nulls so much

关于java - 按日期排序对象列表并应用过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54154878/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com