Java 流 API - 按多个字段分组
Java Streams API - Grouping by multiple fields
我有 java 个具有以下字段的 POJO
class Product{
private String productName;
private String productCode;
private String price;
private String productId;
private String country;
private List<Comments> comments;
}
class Comments {
private String productCode;
private String languageCode;
private String comment;
}
当我从数据库中检索数据时。我得到以下格式的数据。
productName, productCode, price, productId, country, languageCode , comment
iPhone , 1XBA22 , 1000 , 134 , USA , EN , comment in English
iPhone , 1XBA22 , 1000 , 134 , USA , CN , comment in Chinese
laptop , 1234 , 2000 , 145 , UK , EN , comment in English
laptop , 1234 , 2000 , 145 , UK , CN , comment in Chinese
laptop , 1234 , 2000 , 145 , UK , FR , comment in French
db 的结果存储在以下数据结构中。
class ProductWithComments{
private String productName;
private String productCode;
private String price;
private String productId;
private String country;
private String comment;
private String languageCode;
}
现在,如您所见,有评论的产品有重复的产品。因为每个产品都有多种语言的评论,使用Java Streams API,我如何将上面的数据列表转换成List into List.
也就是说,我按产品分组,每个产品都有很多评论。所以基本上分组需要使用许多列 (productName , productCode, price, productId, country)
然后一个组的所有评论都应该列在 List<Comments>
中。在此先感谢您提供这方面的任何指导。
关于这个问题与Whosebug中其他问题类似的评论,这里是我的解释。我的问题是关于多个分组文件。您提供的 URL 按一个字段分组。使用一个字段完成group by时,很简单
你需要拿出一个class作为钥匙:
class ProductKey {
private String productName;
private String productCode;
private String price;
private String productId;
private String country;
private String languageCode;
// constructor, equals, hashCode, etc.
// leave out the fields we're not grouping by
}
那么你只需要做:
products.stream().collect(
Collectors.groupingBy(
product -> new ProductKey(product.getProductName(), ...),
Collectors.mapping(Product::getComment, Collectors.toList())));
我有 java 个具有以下字段的 POJO
class Product{
private String productName;
private String productCode;
private String price;
private String productId;
private String country;
private List<Comments> comments;
}
class Comments {
private String productCode;
private String languageCode;
private String comment;
}
当我从数据库中检索数据时。我得到以下格式的数据。
productName, productCode, price, productId, country, languageCode , comment
iPhone , 1XBA22 , 1000 , 134 , USA , EN , comment in English
iPhone , 1XBA22 , 1000 , 134 , USA , CN , comment in Chinese
laptop , 1234 , 2000 , 145 , UK , EN , comment in English
laptop , 1234 , 2000 , 145 , UK , CN , comment in Chinese
laptop , 1234 , 2000 , 145 , UK , FR , comment in French
db 的结果存储在以下数据结构中。
class ProductWithComments{
private String productName;
private String productCode;
private String price;
private String productId;
private String country;
private String comment;
private String languageCode;
}
现在,如您所见,有评论的产品有重复的产品。因为每个产品都有多种语言的评论,使用Java Streams API,我如何将上面的数据列表转换成List into List.
也就是说,我按产品分组,每个产品都有很多评论。所以基本上分组需要使用许多列 (productName , productCode, price, productId, country)
然后一个组的所有评论都应该列在 List<Comments>
中。在此先感谢您提供这方面的任何指导。
关于这个问题与Whosebug中其他问题类似的评论,这里是我的解释。我的问题是关于多个分组文件。您提供的 URL 按一个字段分组。使用一个字段完成group by时,很简单
你需要拿出一个class作为钥匙:
class ProductKey {
private String productName;
private String productCode;
private String price;
private String productId;
private String country;
private String languageCode;
// constructor, equals, hashCode, etc.
// leave out the fields we're not grouping by
}
那么你只需要做:
products.stream().collect(
Collectors.groupingBy(
product -> new ProductKey(product.getProductName(), ...),
Collectors.mapping(Product::getComment, Collectors.toList())));