我有一个 POJO,如下所示:
public class AgentSales {
private String agent;
private int month;
private int year;
private int salesCount;
public AgentSales(String agent, int month, int year, int salesCount) {
// initializers ...
}
// getters & setters
// ...
}
还有一个像下面这样的集合:
ArrayList<AgentSales> salesList = new ArrayList<AgentSales>();
salesList.add(new AgentSales("Ben", 1, 2013, 3));
salesList.add(new AgentSales("Ben", 1, 2013, 2));
salesList.add(new AgentSales("Ben", 2, 2013, 1));
salesList.add(new AgentSales("Ben", 3, 2013, 1));
salesList.add(new AgentSales("Ben", 2, 2013, 2));
salesList.add(new AgentSales("Tim", 1, 2013, 1));
salesList.add(new AgentSales("Tim", 1, 2013, 1));
salesList.add(new AgentSales("Tim", 2, 2013, 1));
salesList.add(new AgentSales("Tim", 4, 2013, 1));
salesList.add(new AgentSales("Tim", 2, 2013, 5));
salesList.add(new AgentSales("Joe", 2, 2013, 1));
salesList.add(new AgentSales("Joe", 2, 2013, 2));
salesList.add(new AgentSales("Joe", 3, 2013, 1));
salesList.add(new AgentSales("Joe", 3, 2013, 2));
salesList.add(new AgentSales("Joe", 3, 2013, 1));
如何重构/聚合此 ArrayList 的内容,以便将对应于同一月份和代理的多个项目合并为 1,并将 salesCount 值相加?
Agent | month | year | sales count
----------------------------------
Ben | 1 | 2013 | 5
Ben | 2 | 2013 | 3
Ben | 3 | 2013 | 1
Tim | 1 | 2013 | 2
Tim | 2 | 2013 | 6