【发布时间】:2020-11-25 21:31:45
【问题描述】:
class ProductTitle {
private Long productId;
private String productName
private String sellerName
private Long categoryId;
private Long titleId;
private int status;
private String title;
private Long titlePrice;
}
class Aggregate {
productId;
productName;
sellerName;
categoryId;
List<TitleInfo> titles;
}
class TitleInfo {
titleId;
status;
title;
}
SampleData:
productId:1, productname: "product1", sellerName: "seller1",categoryId:2, titleId:25, status:1, title:title1
productId:1, productname: "product1", sellerName: "seller1",categoryId:2, titleId:23, status:1, title:title2
productId:1, productname: "product1", sellerName: "seller1",categoryId:2, titleId:45, status:1, title:title3
productId:2, productname: "product2", sellerName: "seller2",categoryId:5, titleId:67, status:0, title:title4
productId:2, productname: "product2", sellerName: "seller2",categoryId:5, titleId:11, status:1, title:title5
productId:3, productname: "product3", sellerName: "seller3",categoryId:9, titleId:10, status:1, title:title6
productId:6, productname: "product4", sellerName: "seller4",categoryId:7, titleId:36, status:1, title:title7
我有像 sampleData 一样的 ProductTitle 列表。列表中的某些项目重复了 SellerName、productName、categoryId 和 productId。我需要像List<Aggregate> list 这样输出。如何通过流获得此结果。
对不起我的英语
【问题讨论】:
-
到目前为止你尝试了什么?
-
@Naman 我尝试对重复字段进行分组,但它看起来像这样
Map<Long, Map<Long,Map<String, Map<String,List< ProductTitle >>>>>我不确定这是正确的方法 -
您可以查看this answer 中的
groupingBy方法并定义产品标题到标题信息的值映射。如果您尝试这样做并且中途卡住,请使用您的解决方案编辑问题并询问什么不起作用。
标签: java java-8 java-stream grouping