I'm using Java 8. I have a java.util.List of these "MyFlatObject" objects ...
public class MyFlatObject {
private String category;
private String productId;
private String feature;
...
}
How would I convert this List into another List of Category objects, in which those are defined as
public class Category {
private String category;
private List<Product> products;
...
}
public class Product {
private String productId;
private List<String> features;
...
}
? The goal being that if the List<MyFlatObject> list has 12 objects, but only 3 distinct categories, my resulting List<Category> list would only have three objects, each with the distinct Category object and within each Category object, all the relevant products.
What you are possibly looking for would be
Collectors.groupingBywith a downstream function usingCollectors.mapping. If I get it right you are looking to group by multiple attributes and transform them into new objects which is whereCollectors.collectingAndThencould be handy.Assuming few getters and constructors over these classes, the approach could be similar to: