【问题标题】:Sort List of objects by date and applying filter按日期排序对象列表并应用过滤器
【发布时间】:2019-06-06 20:51:26
【问题描述】:

我有付款清单:

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

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

目前它正在返回Payment 2

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

所以排序肯定有问题。

【问题讨论】:

    标签: java sorting lambda java-8 comparator


    【解决方案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
    

    【讨论】:

    • @Bohemian 我确实想要日期相等的行
    【解决方案2】:

    问题出在过滤器上:

    !it.getPaymentDueDate().isAfter(currentDueDate)
    

    这允许付款到期日与当前到期日相同,但您只希望逾期付款。

    改成这样:

    it.getPaymentDueDate().isBefore(currentDueDate)
    

    【讨论】:

      猜你喜欢
      • 2017-08-19
      • 2019-05-07
      • 1970-01-01
      • 2019-05-01
      • 1970-01-01
      • 2021-06-24
      • 2019-03-31
      • 2017-09-11
      相关资源
      最近更新 更多