List of unique objects containing unique sub-objects
Solution 1:
Here is what you could use, without any helper methods.
What it's basically doing is the following
- Group by the
Holder
's parent id - For this
Holder
, take all thechildId
properties and put them in aSet<Long>
Map<Long, Set<Long>> returnSet = holders.stream()
.collect(Collectors.groupingBy(
Holder::getParentId,
Collectors.mapping(
Holder::getChildId,
Collectors.toSet()
)
)
);
Output
{1=[11, 12], 2=[21, 22, 23]}