Java。对 Java 个对象集合中的精确值进行分组和求和

Java. Group and sum exact values in collection of Java Objects

啊啊啊,终于实现了,求助:(

让我们开始吧: 我有一个传入交易列表,例如:

List<FinancialTransaction> incomingTransactions = new ArrayList();
incomingTransactions.add(new FinancialTransaction(1, 1, 2, 1000000));
incomingTransactions.add(new FinancialTransaction(2, 1, 2, 2000000));
incomingTransactions.add(new FinancialTransaction(3, 2, 1, 1000000));
incomingTransactions.add(new FinancialTransaction(4, 2, 1, 4000000));
incomingTransactions.add(new FinancialTransaction(5, 2, 3, 1000000));

金融交易 POJO:

public class FinancialTransaction {

    private Integer id;

    private Integer srcId;

    private Integer dstId;

    private Long amount;

    public Integer getId() {
        return id;
    }

    //getters, setters, constructors, toString
}

然后incomingTransactions转到下面列出的方法,它必须创建一个新的Map,键为FinancialTransaction的srcId和dstId,以此键分组,对分组对象的值求和"amount",并放入新对象Id = sdcId 和 dstId,srcId = this.srcId,dstId = this.dstId,amount = 所有分组对象的总和:

public class TransactionMerger {


    public static Map<String, FinancialTransaction> mergeTransactions(List<FinancialTransaction> financialTransactions) {

        Map<String, FinancialTransaction> mergedTransactions = new HashMap<>();

        for (FinancialTransaction ft: financialTransactions) {

            String key = ft.getSrcId() + "" + ft.getDstId();

            if (mergedTransactions.get(key) != null) {
                mergedTransactions.put(key, ft);
            } else {
//          Don't know to write here :/
            }

        }

        financialTransactions.clear();

        return mergedTransactions;
    }

}

此方法必须吸收 incomingTransactions return 类似于:

Key: 12 Value: FinancialTransaction {12, 1, 2, 3000000} //summed first and second values in incoming list
Key: 21 Value: FinancialTransaction {21, 2, 1, 5000000} //summed third and fourth values in incoming list
Key: 23 Value: FinancialTransaction {23, 2, 3, 1000000} //returned fifth values

请帮助我没有想法,我已经知道如何使用复合键从多个值进行分组,但是如何求和和分组 - 没有想法请帮助!!!!!

各位大侠!!!

首先,您必须在 FinancialTransaction 或实用程序中创建合并函数。现在,我在上面添加 class.

public static FinancialTransaction merge(FinancialTransaction first, FinancialTransaction second){
  first.setAmount(first.getAmount() + second.getAmount());
  return first;
}

现在,让我们使用 map.merge

进行分组
incomingTransactions.stream()
  .collect(Collectors.toMap(trxn -> trxn.getSrcId() + "" + trxn.getDstId(), Function.identity() , FinancialTransaction::merge));

此处,分组将使用生成的键完成,当我们遇到重复键(或类似交易)时,它将应用合并功能,该功能基本上会求和 return 金融交易的更新参考.

注意:正如评论中所指出的,请注意如何生成用于分组的密钥。

public static Map<String, FinancialTransaction> mergeTransactions(List<FinancialTransaction> financialTransactions) {

        Map<String, FinancialTransaction> mergedTransactions = new HashMap<>();

        for (FinancialTransaction ft: financialTransactions) {

            String key = ft.getSrcId() + "" + ft.getDstId();

            if (mergedTransactions.containsKey(key)) {
                //get the existing FinancialTransaction for the key and updates it's amount only
                mergedTransactions.get(key).setAmount(mergedTransactions.get(key).getAmount() + ft.getAmount());
            } else {
                //create new FinancialTransaction object for the map
                FinancialTransaction financialTransaction = new FinancialTransaction();
                //do not set Id for the new object, as its not db generated, key will behave like id
                financialTransaction.setSrcId(ft.getSrcId());
                financialTransaction.setDstId(ft.getDstId());
                financialTransaction.setAmount(ft.getAmount());
                mergedTransactions.put(key, financialTransaction);
            }
        }

        financialTransactions.clear();
        return mergedTransactions;
    }

希望这对您有所帮助。我添加了评论,如果您需要更清楚的信息,请告诉我。