首页 > 解决方案 > Java流过滤掉落在特定时期的对象

问题描述

我需要一些帮助来编写代码来过滤特定时期的利率我如何编写代码来过滤掉例如 2018-08-15 和 2019-11-25 之间的所有 intersetRate 对象

PeriodFrom   PeriodEnd     InterestRate
2018-01-01 2018-06-30 8.5
2018-07-01 2018-12-31 7.5
2019-01-01 2019-06-30 9.01
2019-07-01 2019-12-31 8.75
2020-01-06 2020-06-30 9.25
2020-07-01 2020-08-31 9.45
2020-09-01 2020-12-31 9.90 

在此期间,它应该返回 3 个对象

    import java.math.BigDecimal;
    import java.time.LocalDate;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Objects;
    import java.util.stream.Collectors;
    
    public class InterestRate {
        private final LocalDate periodFrom;
        private final LocalDate periodEnd;
        private final BigDecimal intersetRate;
    
        public InterestRate(LocalDate periodFrom, LocalDate periodEnd,
                            BigDecimal intersetRate) {
            this.periodFrom = periodFrom;
            this.periodEnd = periodEnd;
            this.intersetRate = intersetRate;
        }
    
        public LocalDate getPeriodFrom() {
            return periodFrom;
        }

    public LocalDate getPeriodEnd() {
        return periodEnd;
    }

    public BigDecimal getIntersetRate() {
        return intersetRate;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof InterestRate)) return false;
        InterestRate that = (InterestRate) o;
        return Objects.equals(getPeriodFrom(), that.getPeriodFrom()) && Objects.equals(getPeriodEnd(), that.getPeriodEnd()) && Objects.equals(getIntersetRate(), that.getIntersetRate());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getPeriodFrom(), getPeriodEnd(), getIntersetRate());
    }

    @Override
    public String toString() {
        return "InterestRate{" +
                "periodFrom=" + periodFrom +
                ", periodEnd=" + periodEnd +
                ", intersetRate=" + intersetRate +
                '}' +
        "\n" ;
    }

    public boolean isSelectable(InterestRate rate, LocalDate compareAgainstFrom,
                                LocalDate compareAgainstTo) {
        return (rate.getPeriodFrom().isAfter(compareAgainstFrom) ||
                rate.getPeriodFrom().isEqual(compareAgainstFrom)) &&
                (rate.getPeriodFrom().isBefore(compareAgainstTo) ||
                        rate.getPeriodFrom().isEqual(compareAgainstTo));
    }

    public static void main(String[] args) {
        List<InterestRate> lio = new ArrayList<>();
        LocalDate compareAgainstFrom = LocalDate.of(2018, 8, 15);
        LocalDate compareAgainstTo = LocalDate.of(2019, 11, 25);
        lio.add(new InterestRate(LocalDate.of(2018, 1, 1), LocalDate.of(2018, 6, 30), BigDecimal.valueOf(8.5)));
        lio.add(new InterestRate(LocalDate.of(2018, 7, 1), LocalDate.of(2018, 12, 31), BigDecimal.valueOf(7.5)));
        lio.add(new InterestRate(LocalDate.of(2019, 1, 1), LocalDate.of(2019, 6, 30), BigDecimal.valueOf(9.01)));
        lio.add(new InterestRate(LocalDate.of(2019, 7, 1), LocalDate.of(2019, 12, 31), BigDecimal.valueOf(9.25)));
        lio.add(new InterestRate(LocalDate.of(2020, 1, 1), LocalDate.of(2020, 6, 30), BigDecimal.valueOf(9.45)));
        lio.add(new InterestRate(LocalDate.of(2020, 7, 1), LocalDate.of(2021, 12, 31), BigDecimal.valueOf(9.90)));


        List<InterestRate> collection = lio.stream().filter(s -> s.isSelectable(s,
                compareAgainstFrom, compareAgainstTo)).collect(Collectors.toList());


        System.out.println(collection);

    }
}

================

但这仅返回两个,它不返回包含 2018 年 8 月至 12 月 31 日利率的期间。

    InterestRate{periodFrom=2019-01-01, periodEnd=2019-06-30, intersetRate=9.01},
    InterestRate{periodFrom=2019-07-01, periodEnd=2019-12-31, intersetRate=9.25}
    

我不认为这个逻辑是正确的

标签: java-8

解决方案


像这样的东西(注意性能可能不是最好的),这段代码可能是一个好的开始。我想,也许

package my.project.calc;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class InterestRate {
    private final LocalDate periodFrom;
    private final LocalDate periodEnd;
    private final BigDecimal intersetRate;

    public InterestRate(LocalDate periodFrom, LocalDate periodEnd,
                        BigDecimal intersetRate) {
        this.periodFrom = periodFrom;
        this.periodEnd = periodEnd;
        this.intersetRate = intersetRate;
    }

    public LocalDate getPeriodFrom() {
        return periodFrom;
    }

    public LocalDate getPeriodEnd() {
        return periodEnd;
    }

    public BigDecimal getIntersetRate() {
        return intersetRate;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof InterestRate)) return false;
        InterestRate that = (InterestRate) o;
        return Objects.equals(getPeriodFrom(), that.getPeriodFrom()) && Objects.equals(getPeriodEnd(), that.getPeriodEnd()) && Objects.equals(getIntersetRate(), that.getIntersetRate());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getPeriodFrom(), getPeriodEnd(), getIntersetRate());
    }

    public boolean isSelectable(InterestRate rate, LocalDate compareAgainstFrom,
                                LocalDate compareAgainstTo) {
        return (rate.getPeriodFrom().isAfter(compareAgainstFrom) ||
                rate.getPeriodFrom().isEqual(compareAgainstFrom)) &&
                (rate.getPeriodFrom().isBefore(compareAgainstTo) ||
                        rate.getPeriodFrom().isEqual(compareAgainstTo));
    }

    public static void main(String[] args) {
        List<InterestRate> lio = new ArrayList<>();
        LocalDate compareAgainstFrom = LocalDate.of(2006, 9, 10);
        LocalDate compareAgainstTo = LocalDate.now();
        lio.add(new InterestRate(LocalDate.of(2005, 9, 7), LocalDate.of(2015, 9, 7), BigDecimal.ONE));
        lio.add(new InterestRate(LocalDate.of(2001, 9, 7), LocalDate.of(2010, 9, 7), BigDecimal.ONE));
        lio.add(new InterestRate(LocalDate.of(2002, 9, 7), LocalDate.of(2020, 9, 7), BigDecimal.ONE));
        lio.add(new InterestRate(LocalDate.of(2003, 9, 7), LocalDate.of(2013, 9, 7), BigDecimal.ONE));
        lio.add(new InterestRate(LocalDate.of(2004, 9, 7), LocalDate.of(2016, 9, 7), BigDecimal.ONE));
        lio.add(new InterestRate(LocalDate.of(2006, 9, 7), LocalDate.of(2009, 9, 7), BigDecimal.ONE));
        lio.add(new InterestRate(LocalDate.of(2008, 9, 7), LocalDate.of(2017, 9, 7), BigDecimal.ONE));
        lio.add(new InterestRate(LocalDate.of(2009, 9, 7), LocalDate.of(2014, 9, 7), BigDecimal.ONE));
        lio.add(new InterestRate(LocalDate.of(2000, 9, 7), LocalDate.of(2000, 9, 7), BigDecimal.ONE));

        long counto = lio.stream().filter(s -> s.isSelectable(s,
                compareAgainstFrom, compareAgainstTo)).count();

        System.out.println(counto);

    }

尝试这个

public boolean isSelectable(InterestRate rate, LocalDate compareAgainstFrom,
                            LocalDate compareAgainstTo) {
    return ((rate.getPeriodFrom().isEqual(compareAgainstFrom) ||
                    rate.getPeriodFrom().isAfter(compareAgainstFrom)) ||
            (rate.getPeriodEnd().isEqual(compareAgainstFrom) ||
                    rate.getPeriodEnd().isAfter(compareAgainstFrom)))&&
            (rate.getPeriodFrom().isEqual(compareAgainstTo) ||
                    rate.getPeriodFrom().isBefore(compareAgainstTo)) ||
            (rate.getPeriodEnd().isEqual(compareAgainstTo) ||
                    rate.getPeriodEnd().isBefore(compareAgainstTo));
}

推荐阅读