I have a Product class:
class Product {
String name;
List group;
//more fields, getters, setters
public Product(String name, Group..
You can create a stream of Entry
by using flatMap
and then collect them into Map
using Collectors.mapping
productList.stream()
.flatMap(p->p.getGroup()
.stream()
.map(g->new AbstractMap.SimpleEntry<>(g,p.getName()))) // or from jdk 9 you can use Map.entry(g, p.getName());
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));