I have a List<LedgerEntry> ledgerEntries and I need to calculate the sum of creditAmount and debitAmount.
class LedgerEntry{ private BigDecimal creditAmount; private BigDecimal debitAmount;
I implemented this as
BigDecimal creditTotal = ledgeredEntries.stream().map(p ->p.getCreditAmount()). reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal debitTotal = ledgeredEntries.stream().map(p ->p.getDebitAmount()). reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal sumCreditDebit = creditTotal.subtract(debitTotal);
Looks like I'm repeating List twice. Is there a way to do this at a time without skipping the list twice?
source share