The total sum of transaction by each account

Report a typo

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() and so on).

Write a collector that calculates the total sum of transactions (long type, not integer) by each account (i.e. by account number). The collector will be applied to a stream of transactions.

Write a program in Java 17
import java.util.*;
import static java.util.stream.Collectors.*;

class TransactionCollector {

public static Map<String, Long> getAccount2TransSum(List<Transaction> trans) {
return trans.stream()
.collect(
// Write a collector 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;
}

___

Create a free account to access the full topic