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

  1. Group by the Holder's parent id
  2. For this Holder, take all the childId properties and put them in a Set<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]}