Java 8 Sum two object properties in one iteration

I have a List<LedgerEntry> ledgerEntries and I need to calculate the sums of creditAmount and debitAmount.

class LedgerEntry{
 private BigDecimal creditAmount;
 private BigDecimal debitAmount;

 //getters and setters

I have implemented this as,

BigDecimal creditTotal = ->p.getCreditAmount()).
reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal debitTotal = ->p.getDebitAmount()).
reduce(BigDecimal.ZERO, BigDecimal::add);

//Use creditTotal, debitTotal later

This looks like I'm iterating over the List twice. Is there a way to get this done in one go without having to steam the list twice?

Pre Java 8 version

BigDecimal creditTotal = BigDecimal.ZERO;
BigDecimal debitTotal = BigDecimal.ZERO;
for(LedgerEntry entry : ledgerEntries){
  creditTotal = creditTotal.add(entry.getCreditAmount());
  debitTotal = debitTotal.add(entry.getDebitAmount());


  • You could reduce to a totals entry:

    LedgerEntry totalsEntry = LedgerEntry(), (te, e) -> {
        return te;


    In the comments it was correctly pointed out that reduce() should not modify the initial identifier value, and that collect() should be used for mutable reductions. Below is a version using collect() (using the same BiConsumer as both accumulator and combiner). It also addresses the issue of potential NPEs if the creditAmount and/or debitAmount values have not been set.

    BiConsumer<LedgerEntry, LedgerEntry> ac = (e1, e2) -> {
        BigDecimal creditAmount = e1.getCreditAmount() != null ? e1.getCreditAmount() : BigDecimal.ZERO;
        BigDecimal debitAmount = e1.getDebitAmount() != null ? e1.getDebitAmount() : BigDecimal.ZERO;
    LedgerEntry totalsEntry =, ac, ac);

    All of the sudden the pre-Java 8 version is starting to look mighty attractive.