gpt4 book ai didi

java - 使用多个属性对对象列表进行分组

转载 作者:行者123 更新时间:2023-12-02 05:36:51 25 4
gpt4 key购买 nike

如何使用 Java 中的多个属性对对象列表进行分组?如果我有包含州代码部门分支机构金额帐户的财务详细信息列表。现在我想添加具有相同州代码DeptBranchAmountAccountCode的金额.

    FinancialDetails f1 = new FinancialDetails("01","005","test1",12.23D,"12345");
FinancialDetails f2 = new FinancialDetails("01","005","test1",123.23D,"12345");
FinancialDetails f3 = new FinancialDetails("01","005","test1",99.23D,"12345");
FinancialDetails f4 = new FinancialDetails("02","006","test1",123.23D,"89898");
FinancialDetails f5 = new FinancialDetails("02","006","test1",125.23D,"89898");
FinancialDetails f6 = new FinancialDetails("03","020","test2",625.23D,"89898");
List<FinancialDetails> list = new ArrayList<FinancialDetails>();
list.add(f1);
list.add(f2);
list.add(f3);
list.add(f4);
list.add(f5);
list.add(f6);

现在我想通过添加具有相同状态代码、部门、分支机构、帐户的金额来创建一个对象

最佳答案

尝试:

private static class Key {
String stateCode;
String dept;
// ...
// + hashcode equals
}
public Collection<List<FinancialDetails>> group(List<FinancialDetails> original) {
Map<Key, List<FinancialDetails>> groupingMap = new HashMap<>();
for(FinancialDetails fd : original) {
Key key = new Key(fd.getStateCode, fd.getDept); // ...
List<FinancialDetails> group = groupingMap.get(key);
if(group == null) {
group = new ArrayList<>();
groupingMap.put(key, group);
}
group.add(fd);
}
return groupingMap.values();
}

关于java - 使用多个属性对对象列表进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24887857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com