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

时间:2014-07-22 12:47:44

标签: java collections

如何使用Java中的多个属性对对象列表进行分组? 如果我有State codeDeptBranchAmountAccount的财务详细信息列表。 现在,我想使用相同的state codeDeptBranchAmountAccountCode添加金额。

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

现在我想通过添加具有相同状态代码的金额创建一个对象,Dept,Branch,Account

2 个答案:

答案 0 :(得分:2)

尝试:

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

答案 1 :(得分:0)

班级学生{

private String age;
private String name;
public String getAge() {
    return age;
}
public void setAge(String age) {
    this.age = age;
}
@Override
public String toString() {
    return "Student [age=" + age + ", name=" + name + "]";
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Student(String age, String name) {
    super();
    this.age = age;
    this.name = name;
}

}

public class Hello {

public static void main(String args[]){

    ArrayList<Student> al=new ArrayList<Student>();
    al.add(new Student("12","Prince"));
    al.add(new Student("10", "Amar"));
    al.add(new Student("12","Akansha"));
    HashMap<String, List<Student>> map=new HashMap<String, List<Student>>();
    Iterator<Student> itr=al.iterator();
    while(itr.hasNext()){

        Student s1=itr.next();
        if(map.containsKey(s1.getAge())){

            List<Student> innerList=map.get(s1.getAge());
            innerList.add(s1);
            map.put(s1.getAge(), innerList);

        }else{

            List<Student> innerList=new ArrayList<Student>();
            innerList.add(s1);
            map.put(s1.getAge(),innerList);

        }
    }

    for(Map.Entry<String, List<Student>> entry:map.entrySet() ){

        String key=entry.getKey();
        List<Student> list=entry.getValue();
        System.out.print(key+"=");
        for(Student obj:list){

            System.out.print(obj+" ");
        }
        System.out.println();
    }


}

}