I don't think, that it's called "groupping" but I need to achive the following: I have class
class Person {
String name;
Set<String> groups;
}
I have some persons:
- Father => {"Worker", "Men"}
- Mother => {"Woman"}
- Son => {"Student","Men"}
- Daughter => {"Student","Woman"}
So, every person can be part of multiple categories. And I want to get the following mapping:
"Worker" => {Father}
"Men" => {Father, Son}
"Student" => {Son, Daughter}
"Woman" => {Mother, Daughter}
Now I can do this with manual iterating over every person and putting them to Map<String,List<Person>>
I'm trying to find a more elegant wat to do it with streams (or StreamEx) oneliner, like:
List<Persons> family= ...;
Map<String,List<Person>> groupped = family.stream().groupByMultipleAttributes(Person::getGroups)
You can generate all the associated pairs of groups and
Person
s, and then collect them withgroupingBy
into aMap
: