title: Java 8 Streams: why does Collectors.toMap behave differently for generics with wildcards?
body: <p>Assume that you have a <code>List</code> of numbers. The values in the <code>List</code> can be of type <code>Integer</code>, <code>Double</code> etc. When you declare such a <code>List</code> it is possible to declare it using a wildcard (<code>?</code>) or without a wildcard. </p>
  
  <pre><code>final List&lt;Number&gt; numberList = Arrays.asList(1, 2, 3D);
  final List&lt;? extends Number&gt; wildcardList = Arrays.asList(1, 2, 3D);
  </code></pre>
  
  <p>So, now I want to <code>stream</code> over the <code>List</code> and <code>collect</code> it all to a <code>Map</code> using the <code>Collectors.toMap</code> (obviously the code below is just an example to illustrate the problem). Lets start off by streaming the <code>numberList</code>:</p>
  
  <pre><code>final List&lt;Number&gt; numberList = Arrays.asList(1, 2, 3D, 4D);
  
  numberList.stream().collect(Collectors.toMap(
          // Here I can invoke "number.intValue()" - the object ("number") is treated as a Number
          number -&gt; Integer.valueOf(number.intValue()),
          number -&gt; number));
  </code></pre>
  
  <p>But, I can not do the same operation on the <code>wildcardList</code>:</p>
  
  <pre><code>final List&lt;? extends Number&gt; wildCardList = Arrays.asList(1, 2, 3D);
  wildCardList.stream().collect(Collectors.toMap(
          // Why is "number" treated as an Object and not a Number?
          number -&gt; Integer.valueOf(number.intValue()),
          number -&gt; number));
  </code></pre>
  
  <p>The compiler complains on the call to <code>number.intValue()</code> with the following message:</p>
  
  <blockquote>
    <p>Test.java: cannot find symbol<br/>
    symbol:   method intValue()<br/>
    location: variable number of type java.lang.Object</p>
  </blockquote>
  
  <p>From the compiler error it is obvious that the <code>number</code> in the lambda is treated as an <code>Object</code> instead of as a <code>Number</code>. </p>
  
  <p>So, now to my question(s): </p>
  
  <ul>
  <li>When collecting the wildcard version of the <code>List</code>, why is it not working like the non-wildcard version of the <code>List</code>? </li>
  <li>Why is the <code>number</code> variable in the lambda considered to be an <code>Object</code> instead of a <code>Number</code>?</li>
  </ul>