1

I have a list of transactions

Code | Type | Amount
1    |A     |50.00
1    |A     |50.00
1    |B     |10.00
1    |B     |10.00
2    |B     |50.00

I want to have this result:

Code | Type | Amount
1    |A     |100.00
1    |B     |20.00
2    |B     |50.00

I just able to have 1 level of grouping by code:

List<Result> result = transactions.stream()
            .collect(Collectors.groupingBy(a -> a.getCode()))
            .entrySet()
            .stream()
            .map(e -> e.getValue().stream()
                .reduce((a1, a2) -> new Result(a1.getCode(), a1.getType(), a1.getAmount().add(a2.getAmount()))))
                .map(f -> f.get())
                .collect(Collectors.toList());

** Amount is a Big decimal.*

How to have multiple grouping?

Alvin
  • 8,219
  • 25
  • 96
  • 177

0 Answers0