title: How to call Java's Stream.collect() from groovy?
body: <p>Having some background with FP from Scala I really don't like Groovy's names for collections methods. Given that and some architectural choices made above, I found using a Java 8 streams API (plus java.util.Optional) in Groovy code an appealing solution.</p>
  
  <p>Until I hit this:</p>
  
  <pre><code>def finalCollection = [ 'some', 'items', 'really', 'not', 'important' ].stream()
          .map { aMethodReturningOptional(it) } //map String to Optional&lt;Item&gt;
          .flatMap { it.map(Stream.&amp;of).orElseGet(Stream.&amp;empty) } //convert to Stream&lt;Item&gt;
          .collect() //Groovy's collect, not stream's!
  </code></pre>
  
  <p>Note it works only in Groovy 2+ - <a href="http://mrhaki.blogspot.com/2015/04/groovy-goodness-use-closures-as-java.html" rel="noreferrer">treating closure as lambda</a>.
  What bothers me is the last line of the example code. Groovy translates the call to the <code>DefaultGroovyMethods.collect()</code> instead of <code>Stream.collect()</code> that I originally wanted to use. Of course then the last line would be:</p>
  
  <pre><code>        .collect(Collectors.toList()) //Should call Java collect, but it doesn't
  </code></pre>
  
  <p>It seems counter-intuitive to me, that some extension method is called instead of native, 'built-in' method of the class.</p>
  
  <p>How can I rewrite the example so the <code>Stream.collect()</code> method would be called?</p>
  
  <p><strong>UPDATE</strong>: After some more fiddling, I've found out what problem I had originally. I wrote <code>.collect{Collectors.toList()}</code> (note curly braces), which of course called Groovy method, not Java. Note to self: remember to quadruple-check before posting...</p>