我不能编写一个收集器来计算每个帐户(即按帐号)的交易总额(长类型,而不是整数)。收集器将应用于事务流。如何使用 groupingBy 这个链 Transaction -> Account -> Number ?
class TransactionCollector {
public static Map<String, Long> getAccount2TransSum(List<Transaction> trans) {
return trans.stream().collect( // place code here
}
static class Transaction {
private final String uuid;
private final Long sum;
private final Account account;
public Transaction(String uuid, Long sum, Account account) {
this.uuid = uuid;
this.sum = sum;
this.account = account;
}
public String getUuid() {
return uuid;
}
public Long getSum() {
return sum;
}
public Account getAccount() {
return account;
}
@Override
public String toString() {
return "Transaction{" +
"uuid='" + uuid + '\'' +
", sum=" + sum +
'}';
}
}
static class Account {
private final String number;
private final Long balance;
public Account(String number, Long balance) {
this.number = number;
this.balance = balance;
}
public String getNumber() {
return number;
}
public Long getBalance() {
return balance;
}
@Override
public String toString() {
return "Account{" +
"number='" + number + '\'' +
", balance=" + balance +
'}';
}
}
}