简体   繁体   中英

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

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 :

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:

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.

PS3: Returning null instead of empty list is error prone and it's usually better to go with an empty list.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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