SQL语句
SELECT
income_name,
sum(amount) amount
FROM
account_entity
GROUP BY income_name
实体类:
@Data
@Accessors(chain = true)
public class AccountEntity{
@ApiModelProperty(value = "金额")
private Integer amount;
@ApiModelProperty(value = "收入账户名称")
private String incomeName;
}
分组并求和:
List<AccountEntity> list = list.stream().collect(Collectors.groupingBy(AccountEntity::getIncomeName,
Collectors.reducing((a, b) -> new AccountEntity()
.setAmount(a.getAmount() + b.getAmount())
.setIncomeName(a.getIncomeName()))
)).entrySet().stream().map(c -> c.getValue().get()).collect(Collectors.toList());