【问题标题】:java 8 alternative for nested for loopsjava 8替代嵌套for循环
【发布时间】:2020-02-06 15:58:26
【问题描述】:

我有以下 sn-p,我想知道是否以及如何用 Java-streams/Java 8 API 替换它

List<Borrower> borrowers = creditcomplex.getBorrowers();
for (Borrower borrower : borrowers) {
    List<Facility> facilities = borrower.getFaciliies();
    for (Facility facility : facilities) {
        List<RepaymentSchedule> repaymentScheduleList = facility.getrepaymentSchedule();
        if (repaymentScheduleList != null) {
            for (RepaymentSchedule repaymentschedule : repaymentScheduleList) {
                double[] repayment = 
                    amortizationService.calculateAmortizationSchedule(repaymentschedule);
                double[] drawdown = 
                    amortizationService.calculateRepaymentSchedule(repaymentschedule);
                double[] outstandingProfie = amortizationService
                        .calculateOutstandingSchedule(repaymentschedule);
            }
        }
    }
}

【问题讨论】:

  • 我可能希望将上述一些功能折叠到(比如说)Borrower 类、Facility 类等中。然后你可以询问这些类并委托,而不是从对象中提取对象对象....有关更多信息,请参阅en.wikipedia.org/wiki/Law_of_Demeter....您可能需要传递 amortizationService,注意...

标签: java arrays collections java-8 java-stream


【解决方案1】:

你可以使用flatMap:

creditcomplex.getBorrowers().stream()
    .flatMap(b -> b.getFaciliies().stream())
    .flatMap(f -> Optional.ofNullable(f.getrepaymentSchedule()).stream())
    .forEach(repaymentschedule -> {
            double[] repayment = 
                amortizationService.calculateAmortizationSchedule(repaymentschedule);
            double[] drawdown = 
                amortizationService.calculateRepaymentSchedule(repaymentschedule);
            double[] outstandingProfie = amortizationService
                    .calculateOutstandingSchedule(repaymentschedule);
    });

P.S.1:注意Optional#stream出现在java 9中,你可能需要使用:

optional.map(Stream::of).orElseGet(Stream::empty)

取自here

PS2:你在 forEach 中所做的事情没有任何效果(你在里面声明和初始化数组,但你不能在循环之外使用它们。我留下了代码,因为它可以被替换对嵌套元素进行任何计算。

P.S.3:返回 null 而不是空列表容易出错,通常最好使用空列表。

【讨论】:

  • 为什么使用 flatMap(f -> Optional.ofNullable(f.getrepaymentSchedule()).stream()) 而不是 juste : map(Facility::getrepaymentSchedule) ???
  • @ArnaultLePrévost-Corvellec 因为null-check。在它检查的问题中:if (repaymentScheduleList != null),所以我不能简单地写f.getrepaymentSchedule().stream(),因为这会导致 NPE
  • @ArnaultLePrévost-Corvellec 返回空列表而不是 null 是一种更好的风格,我在 P.S.3 中添加了它
  • 不,您在流的下一步中过滤空对象^^
  • 完美聚合列表,但流可以返回空对象过滤器方法已在 J8 上为此目的添加 =)
【解决方案2】:

未经测试,但应该大致是

List<RepaymentSchedule> repaymentSchedules = creditcomplex.getBorrowers().stream()
    .flatMap(borrower -> borrower.getFacilities().stream())
    .map(facility -> facility.getrepaymentSchedule())
    .filter(repaymentScheduleList -> repaymentScheduleList != null)
    .flatMap(repaymentScheduleList -> repaymentScheduleList.stream())
    .collect(Collectors.toList());

将是一回事,您可以从这里创建数组。

或者,您可以省略 .collect() 而改为这样做

.forEach(repaymentSchedule -> {
    double[] repayment = 
                amortizationService.calculateAmortizationSchedule(repaymentschedule);
    double[] drawdown = 
                amortizationService.calculateRepaymentSchedule(repaymentschedule);
    double[] outstandingProfie = amortizationService
                    .calculateOutstandingSchedule(repaymentschedule);
});

【讨论】:

    猜你喜欢
    • 2022-01-14
    • 2020-12-22
    • 2012-04-15
    • 2015-08-03
    • 1970-01-01
    • 2020-08-09
    • 2017-04-02
    相关资源
    最近更新 更多