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

问题描述 投票:0回答:2

[Aaaah,这终于发生了,我寻求帮助:(

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

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));

FinancialTransaction 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
}

然后,传入事务将移动到下面列出的方法,该方法必须创建一个新映射,其键为FinancialTransaction的srcId和dstId,按此键分组,对分组对象的“金额”值求和,并将新对象的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返回类似:

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

[请帮助我没有主意,我已经知道如何使用多个值对组合键进行分组,但是总和分组-没有主意请帮助!!!

大家伙!!

[Aaaah,终于发生了,我寻求帮助:(让我们开始:我有一个传入事务的列表,例如:List entryTransactions = new ArrayList(); ...

java collections stream java-stream pojo
2个回答
0
投票
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;
    }

0
投票

首先,您必须在FinancialTransaction或实用程序中创建合并功能。现在,我要添加上面的类。

© www.soinside.com 2019 - 2024. All rights reserved.