用Map帮忙,Group By有多少个字段,就将这些个字段组合成一个Key,对应的value就是合计信息
你这里只有一个group by,直接用name即可
public static List<B> summary(List<A> list) {
Map<String, B> map = new LinkedHashMap<String, B>();
for (A a : list) {
String name = a.getName();
B b = map.get(name);
if (b == null) {
b = new B();
b.setName(name);
map.put(name, b);
}
b.setMoney(b.getMoney() + a.getMoney());
b.setWeight(b.getWeight() + a.getWeight());
b.setHeight(b.getHeight() + a.getHeight());
}
return new ArrayList<B>(map.values());
}
你这里只有一个group by,直接用name即可
public static List<B> summary(List<A> list) {
Map<String, B> map = new LinkedHashMap<String, B>();
for (A a : list) {
String name = a.getName();
B b = map.get(name);
if (b == null) {
b = new B();
b.setName(name);
map.put(name, b);
}
b.setMoney(b.getMoney() + a.getMoney());
b.setWeight(b.getWeight() + a.getWeight());
b.setHeight(b.getHeight() + a.getHeight());
}
return new ArrayList<B>(map.values());
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货