简体   繁体   English

嵌套 for 循环的 java 8 替代方案

[英]java 8 alternative for nested for loops

I have the following snippet and I wonder if and how it is possible to replace it with Java-streams/Java 8 API我有以下代码段,我想知道是否以及如何用 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);
            }
        }
    }
}

You can use flatMap :您可以使用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);
    });

PS1: Note that Optional#stream appeared in java 9, you might need to use: PS1:请注意Optional#stream出现在 java 9 中,您可能需要使用:

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

It's taken from here .这是从这里拿的。

PS2: What you're doing in a forEach does not have any effect (you're declaring and initializing arrays inside, but you cannot use them outside of the loop. I left the code because it can be replaced with any computation on the nested elements. PS2:你在forEach所做的没有任何影响(你在内部声明和初始化数组,但你不能在循环外使用它们。我留下了代码,因为它可以被嵌套的任何计算替换元素。

PS3: Returning null instead of empty list is error prone and it's usually better to go with an empty list. PS3:返回null而不是空列表容易出错,通常最好使用空列表。

Untested, but it should roughly be未经测试,但大致应该是

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

would be one thing, and from here youcan create the arrays.将是一回事,从这里你可以创建数组。

Alternatively, you can omit the .collect() and instead do或者,您可以省略.collect()而是执行

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM