How to groupby one field and sum the other two fields? The class Salary is as following:
public class Salary {
private Integer id;
private Double simSalary;
private Double nonsimSalary;
}
the data is like:
id = 01, simSalary = 100, nonsimSalary = 0;
id = 01, simSalary = 0, nonsimSalary = 20;
id = 02, simSalary =50, nonsimSalary = 30;
I want groupby the id and sum both the simSalary and the nonsimSalary, the result should be like:
id = 01, simSalary = 100, nonsimSalary = 20;
id = 02, simSalary = 50, nonsimSalary = 30;
However using the following code, I could only sum one field, how to sum both of the two fields at one time?
List<Salary> salaryList = getSalary();
salaryList.stream().collect(Collectors.groupingBy(Salary::getId,
Collectors.summingDouble(Salary::getSimSalary))));