I have a object with contains a list of other objects and I want to return a flatmap of the contained objects mapped by some property of the container. Any one if is it possible using stream and lambdas only?
public class Selling{
String clientName;
double total;
List<Product> products;
}
public class Product{
String name;
String value;
}
Lets supose a list of operations:
List<Selling> operations = new ArrayList<>();
operations.stream()
.filter(s -> s.getTotal > 10)
.collect(groupingBy(Selling::getClientName, mapping(Selling::getProducts, toList());
The result would be of kind
Map<String, List<List<Product>>>
but i would like to flatten it like
Map<String, List<Product>>
In JDK9 there's new standard collector called
flatMapping
which can be implemented in the following way:You can add it to your project and use like this:
You could try something like: