title: JAVA8 - Grouping with lambda
body: <p>I have a collection with structure like this:</p>
  
  <pre><code>@Entity
  public class RRR{
  
      private Map&lt;XClas, YClas&gt; xySets;
  
  }
  </code></pre>
  
  <p>and <code>XClas</code> has a field called <code>ZZZ</code></p>
  
  <p>my question is:
  I would like to aggregate it with lambda to get a <code>Map&lt;ZZZ, List&lt;RRR&gt;&gt;</code>.</p>
  
  <p>Is it possible? Now I'm stuck with:</p>
  
  <pre><code>Map xxx = rrrList.stream().collect(
                  Collectors.groupingBy(x-&gt;x.xySets().entrySet().stream().collect(
                          Collectors.groupingBy(y-&gt;y.getKey().getZZZ()))));
  </code></pre>
  
  <p>but it's <code>Map&lt;Map&lt;ZZZ, List&lt;XClas&gt;&gt;, List&lt;RRR&gt;&gt;</code> so it's not what I was looking for :)</p>
  
  <p>Right now just to make it work, I did aggregation with two nested loops, but it would be so great, if you could help me make it done with lambdas.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I post what I got by now, as asked.
  I already left nested loops, and I manage to work my way up to this point:</p>
  
  <pre><code>Map&lt;ZZZ, List&lt;RRR&gt;&gt; temp;
  rrrList.stream().forEach(x -&gt; x.getxySetsAsList().stream().forEach(z -&gt; {
              if (temp.containsKey(z.getKey().getZZZ())){
                  List&lt;RRR&gt; uuu = new LinkedList&lt;&gt;(temp.get(z.getKey().getZZZ()));
                  uuu.add(x);
                  temp.put(z.getKey().getZZZ(), uuu);
              } else {
                  temp.put(z.getKey().getZZZ(), Collections.singletonList(x));
              }
          }));
  </code></pre>
  
  <p>Thanks in advance</p>