我有这个对象列表
List<DetailDto> details;
@Value
public class DetailDto implements Serializable {
String category1;
Integer category2;
Integer price;
transient Integer totalPrice;
}
有了这份清单
[
{
"category1": "ABC",
"category2": 30,
"price": 195,
"totalPrice": null
},
{
"category1": "ABC",
"category2": 30,
"price": 195,
"totalPrice": null
},
{
"category1": "ABC",
"category2": 30,
"price": 195,
"totalPrice": null
},
{
"category1": "ABC",
"category2": 30,
"price": 390,
"totalPrice": null
},
{
"category1": "ABC",
"category2": 30,
"price": 390,
"totalPrice": null
},
{
"category1": "DEF",
"category2": 30,
"price": 455,
"totalPrice": null
},
{
"category1": "DEF",
"category2": 30,
"price": 455,
"totalPrice": null
},
{
"category1": "DEF",
"category2": 30,
"price": 455,
"totalPrice": null
},
{
"category1": "DEF",
"category2": 30,
"price": 455,
"totalPrice": null
},
{
"category1": "GHI",
"category2": 1,
"price": 18000,
"totalPrice": null
}
]
我想通过将字段List<DetailDto>总结为条件为的字段来制作另一个对象:pricetotalPrice
字符串category1很常见
整数category2很常见
整数price很常见
在这一点上我有这个
List<List<DetailDto>> summarizedList = detail().stream()
.collect(Collectors.groupingBy(DetailDto::category1,
Collectors.groupingBy(DetailDto::category2,
Collectors.groupingBy(DetailDto::price))))
.values()
.stream()
.flatMap(c1 -> c1.values().stream())
.flatMap(c2 -> c2.values().stream())
.collect(Collectors.toList());
返回我List<List<DetailDto>>
而且我不知道如何使它正确,在我尝试这个之后
summarizedList.stream().map(dto -> dto.stream().reduce((x,y) -> new DetailDto(x.productCode(), x.productQt(), x.orderPrice(), Integer.sum(x.orderPrice(), y.orderPrice()).orElse(null).collect(Collectors.toList());
你们能帮帮我吗?
白猪掌柜的
相关分类