【发布时间】:2018-07-24 08:10:56
【问题描述】:
你有两个类:
Account: number: String, balance: Long
Transaction: uuid: String, sum: Long, account: Account
两个类都有对应名称的所有字段的 getter(getNumber()、getSum()、getAccount() 等)。
我需要计算每个帐户的交易总和,但不是按帐户,而是按 Account.number 分组
我是这样设计的:
Map<Account, Long> totalSumOfTransByEachAccount =
transactions.stream()
.collect(Collectors.groupingBy(Transaction::getAccount, Collectors.reducing(0, Transaction::getSum, Long::sum)));
但我需要带有字符串键的映射 - Account.getNumber()
Map<String, Long> totalSumOfTransByEachAccount =
transactions.stream()
.collect(Collectors. ??????)
谁能帮帮我?
【问题讨论】:
标签: java stream java-stream