You have two classes:
Account: number: String, balance: Long
Transaction: uuid: String, sum: Long, account: Account
Both classes have getters for all fields with the corresponding names (getNumber (), getSum (), getAccount (), etc.).
I need to calculate the transaction amount for each account, but definitely not by the Account, but group by Account.number
I do it like this:
Map<Account, Long> totalSumOfTransByEachAccount =
transactions.stream()
.collect(Collectors.groupingBy(Transaction::getAccount, Collectors.reducing(0, Transaction::getSum, Long::sum)));
But I need a card with a string key - Account.getNumber ()
Map<String, Long> totalSumOfTransByEachAccount =
transactions.stream()
.collect(Collectors. ??????)
Can anybody help me?
source
share