title: How to merge child objects in list based on duplicate parent object
body: <p>I have two entities with one to many relationship which are joined together by composite primary keys. Since Spring Data generates wrong count distinct query for oracle database , I have SQL output with cartesian join which leads to repeating row for parent object for every row of child object. I need to find out distinct parent objects based on composite keys and then add each child object for parent in a list and set it as property of parent object</p>
  
  <p>I am able to find out distinct parent object based on composite keys of the parent object. Following is the relevant code</p>
  
  <pre><code>private static &lt;T&gt; Predicate&lt;T&gt; distinctByKeys(Function&lt;? super T, ?&gt;... keyExtractors)
  {
  final Map&lt;List&lt;?&gt;, Boolean&gt; seen = new ConcurrentHashMap&lt;&gt;();
  return t -&gt;
  {
  final List&lt;?&gt; keys = Arrays.stream(keyExtractors)
  .map(ke -&gt; ke.apply(t))
  collect(Collectors.toList());
  
  return seen.putIfAbsent(keys, Boolean.TRUE) == null;
  };
  }
  </code></pre>
  
  <p>suppose I have the following input</p>
  
  <pre><code>list.add(new Book("Core Java", 200, new ArrayList(){{add("Page 1");}}));
  list.add(new Book("Core Java", 200, new ArrayList(){{add("Page 2");}}));
  list.add(new Book("Learning Freemarker", 150, new ArrayList(){{add("Page 15");}}));
  list.add(new Book("Spring MVC", 300, new ArrayList(){{add("Page 16");}}));
  list.add(new Book("Spring MVC", 300, new ArrayList(){{add("Page 17");}}));
  </code></pre>
  
  <p>I need to produce following output</p>
  
  <pre><code>Core Java,200, [Page 1, Page 2]
  Learning Freemarker,150, [Page 15]
  Spring MVC,300 , [Page 16, Page 17]
  </code></pre>
  
  <p>Any help in this regard will be very helpful</p>