title: Collecting a raw stream into a typed collection?
body: <p>I am calling a library method that returns a raw <code>Stream</code>. I know the type of the elements in the stream and want to collect into a collection with the declared element type. What is the nice, or the not so appalling way of doing it?</p>
  <h2>Minimal reproducible example</h2>
  <p>For the sake of a reproducible example suppose I want to call this method:</p>
  <pre><code>@SuppressWarnings(&quot;rawtypes&quot;)
  static Stream getStream() {
      return Stream.of(&quot;Example&quot;);
  }
  </code></pre>
  <p>I had hoped that this would work, and I have not understood why it doesn’t:</p>
  <pre><code>    List&lt;String&gt; stringList = getStream().map(s -&gt; (String) s).collect(Collectors.toList());
  </code></pre>
  <p>I get <em>Type mismatch: cannot convert from Object to List</em>. Why?</p>
  <h2>Workaround</h2>
  <p>An unchecked cast works. To narrow down the bit that needs to be marked as deliberately unchecked, we may do</p>
  <pre><code>    @SuppressWarnings(&quot;unchecked&quot;)
      Stream&lt;String&gt; stringStream = getStream();
      List&lt;String&gt; stringList = stringStream.collect(Collectors.toList());
  </code></pre>
  <p>On both Java 8 and Java 11 the returned list is a <code>java.util.ArrayList</code>, and it contains the expected <code>String</code> element.</p>
  <h2>What my search turned up or not</h2>
  <p>I tried my search engine searching for terms like <em>java collect raw stream</em>. It didn’t lead me to anything helpful. There’s a closed Java bug (link at the bottom) mentioning that once you operate on raw types, everything gets raw. But this fully raw version doesn’t work either:</p>
  <pre><code>    List stringList = getStream().collect(Collectors.toList());
  </code></pre>
  <p>I am still getting <em>Type mismatch: cannot convert from Object to List</em>. How come?</p>
  <p>We are coding for Java 8 (a little while still).</p>
  <p>I know this question has something opinion-based about it. I am still hoping that you can provide me with some facts that will help me write code that will generally be considered nicer.</p>
  <h2>Background: Hibernate 5</h2>
  <p>The method I want to call is <code>org.hibernate.query.Query.getResultStream()</code> on a raw <code>Query</code> (a <code>Query</code> lacking type parameter). I want to get the stream because I have special requirements for the type of collection I am collecting into. In my production code I am using <code>Collectors.toCollection()</code> (not <code>Collectors.toLlist()</code> as in the example in this question). BTW while writing this question I found a way to persuade Hibernate to return a typed stream. I still found the question interesting enough to post.</p>
  <h2>Link</h2>
  <ul>
  <li><a href="https://bugs.openjdk.java.net/browse/JDK-8223548" rel="nofollow noreferrer">Problem with casting inside lambdas for java.util.stream.Stream.map method</a> in the Java bug database</li>
  </ul>