title: Java Stream.map() on Subclasses with Wildcards Not Working
body: <p>I have two classes <code>ChildA</code> and <code>ChildB</code> which are subclasses of <code>Parent</code>.</p>
  <p>I am given a list of <code>Parent</code> and want to use Stream to map them to a <code>Map&lt;? extends Parent, List&lt;? extends Parent&gt;&gt;</code></p>
  <p>The issue is it seems the <code>.map</code> part is removing the subclass. Here is some sample code illustrating my problem:</p>
  <pre class="lang-java prettyprint-override"><code>items.stream()
          .map(item -&gt; item.isA() ? item.getA() : item.getB()) //&lt;--- issue here, returns List&lt;Parent&gt; instead of List&lt;? extends Parent&gt;
          .collect(Collectors.groupingBy(i -&gt; i.getClass()));
  </code></pre>
  <p>With the above, the output is: <code>Map&lt;? extends Class, List&lt;Parent&gt;&gt;</code> but I want <code>Map&lt;?extends Class, List&lt;? extends Parent&gt;&gt;</code> so I could then do something like:</p>
  <p><code>map.get(A.class)</code> -&gt; <code>List&lt;A&gt;</code></p>
  <p>So I can easily assign two variables:</p>
  <pre><code>List&lt;A&gt; a = map.get(a.class);
  List&lt;b&gt; b = map.get(b.class);
  </code></pre>
  <p>Where as right now I would have to do something like:
  <code>map.get(A.class).stream().map(A::cast).collect(Collectors.toList());</code></p>
  <p>Is there a way to get my desired result?</p>
  <p>EDIT:</p>
  <p>Here is a minimum reproducible example: <a href="https://replit.com/@rgurn/MinExample?v=1#Main.java" rel="nofollow noreferrer">https://replit.com/@rgurn/MinExample?v=1#Main.java</a></p>
  <p>Note it was not my design choice to make the container class <code>Item</code> and I cannot change it.</p>