-2

I have java POJO with following fields

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;

}

When I retrieve data from the database. I get the data in the following format.

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

This result from db is stored in following data structure.

class ProductWithComments{
    private String productName;
    private String productCode;
    private String price;
    private String productId;
    private String country;
    private String comment;
    private String languageCode;
}

now, as you can see, the product with comments has duplicate products. because each product has comments in many languages, using Java Streams API, how can I convert the above list of data into List into List.

meaning, I group by Product and each product has many Comments. So basically grouping by needs to happen using many columns (productName , productCode, price, productId, country) then all the Comments for one Group should be listed in List<Comments>. Thanks in advance for any guide on this.

Regarding the comment that this question is similar to other questions in Stackoverflow, here is my explanation. My question is about multiple grouping fileds. The URL you have provided is grouped by one field. When group by is done using one field, it is straightforward

KItis
  • 5,476
  • 19
  • 64
  • 112
  • Are you expecting to be able to do lookups using one of these fields, or only with all five at once? – Louis Wasserman Nov 26 '21 at 04:20
  • @LouisWasserman from the database, when I get the data, I get duplicates for products. So, I am looking for a way to group by-products, so that one product has multiple comments. I am not going to do any lookups. I have updated the question with my requirement. – KItis Nov 26 '21 at 04:23
  • Does this answer your question? [Group by multiple field names in java 8](https://stackoverflow.com/questions/28342814/group-by-multiple-field-names-in-java-8) – Nowhere Man Nov 26 '21 at 09:08
  • @AlexRudenko, I had this looked at before making this question. My question is about multiple grouping fileds. The URL you have provided is grouped by one field. When group by is done using one field, it is straightforward – KItis Nov 26 '21 at 10:09
  • The URL I provided deals specifically with grouping by _multiple fields_ as the question title states. and OP had a question: _here in my case, I want to group by name and age._ – Nowhere Man Nov 26 '21 at 10:14
  • The difference is, their return value is Map< gropKey1, Map< groupKey2, Group>>, in my case, then I want the answer in Map format – KItis Nov 26 '21 at 11:55
  • I think a better solution would be to group the result in your database query. Databases are optimized for this kind of operations. How you do that depends on how you fetch the information from the database. – magicmn Nov 26 '21 at 15:32

1 Answers1

3

You need to pull out a class to use as a key:

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
}

Then you just need to do:

products.stream().collect(
  Collectors.groupingBy(
    product -> new ProductKey(product.getProductName(), ...),
    Collectors.mapping(Product::getComment, Collectors.toList())));
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413