I have the following objects:
public class Item {
String value;
List<Person> owners;
Person creator;
}
public class Person {
String name;
int id;
Person manager;
}
now i have the a list containing 3 Item objects:
i1 -> {value="1", owners=[p1, p2, p3], creator=p4}
i2 -> {value="2", owners=[p2, p3], creator=p5}
i3 -> {value="3", owners=[p5], creator=p1}
the Person objects are as follows:
p1 -> {manager=m1, ...}
p2 -> {manager=m2, ...}
p3 -> {manager=m3, ...}
p4 -> {manager=m2, ...}
p5 -> {manager=m1, ...}
I want to group the stream of Item objects based on managers of the owners and creator. The result Map<Person, List<Item>>
should look like:
{
m1: [i1, i2, i3],
m2: [i1, i2],
m3: [i1, i2]
}
I think using Stream and Collector APIs, I can make the map from Item to managers, Map<Item, List<Person>>
first, then reverse the mapping. But is there any way to make the mapping I want using Stream and Collectors only?