title: Java 8 Stream groupingBy from Groovy
body: <p>I'm trying to use Java 8 Stream API with groovy using <code>@CompileStatic</code>:</p>
  
  <p>Writing the <code>groupingBy</code> is giving me a headache, the error says:</p>
  
  <blockquote>
    <p>groupingBy in java.util.stream.Collectors cannot be applied to groovy.lang.Closure</p>
  </blockquote>
  
  <p>Currently using Java 8 and Groovy 2.4.7</p>
  
  <p>My intention is:</p>
  
  <ol>
  <li>Use map to build DTOs</li>
  <li>Use <code>collect</code> with <code>groupingBy</code> to create a map using the UUIDs as keys</li>
  </ol>
  
  <p>This is the final working code (developed using "Will Lp" and "BalRog" support).</p>
  
  <pre><code>import groovy.transform.CompileStatic
  import groovy.transform.Immutable
  
  import java.util.function.Function
  import static java.util.stream.Collectors.groupingBy;
  
  @CompileStatic
  public class ThanksForChecking {
  
      @Immutable
      public static class Relation {
          String entityUUID
      }
  
      public static void main(String[] args) {
          List&lt;LinkedHashMap&lt;String, String&gt;&gt; rows = [
                  [entityUUID: "uuid 1"],
                  [entityUUID: "uuid 2"]
          ]
  
          Map&lt;String, List&lt;Relation&gt;&gt; relations = rows.stream()
                  .map { row -&gt; new Relation(row as HashMap) }
                  .collect(groupingBy((Function) { Relation r -&gt; return r.entityUUID }))
  
          println(relations);
  
          assert relations == [
                  "uuid 1": [new Relation(entityUUID: "uuid 1")],
                  "uuid 2": [new Relation(entityUUID: "uuid 2")]
          ]
      }
  }
  </code></pre>