sql: select name , sum(amount) from table group by name
想問以上sql 怎樣在JAVA 中寫呢
public class Obj{
String Name;
int amount;
}
public List<Obj> getTotal (List<Obj> objList){
List<Obj> resultList= new LinkedList<Obj>();
for ( Obj a :objList ){
//code//
}
return resultList;
}
data:
Name Amount
apple 100
orange 100
apple 100
banana 100
Map<String, Integer> result = objList.stream().collect(Collectors.groupingBy(Obj::getName, Collectors.summingInt(Obj::getAmount)));
System.out.println("result = " + result);//result = {banana=100, orange=100, apple=200}
有可能是下面
resultList.stream().collect(
Collectors.groupingBy(Obj::getName, Collectors.summingInt(Obj::getAmount)));