title: Java's flatMap on list of list of optional integers
body: <p>I have the following bit of simplified code that fails to compile and I don't understand why:</p>
  
  <pre><code>List&lt;Optional&lt;Integer&gt;&gt; list =
      new ArrayList&lt;&gt;();
  List&lt;Integer&gt; flattened = 
    list
      .stream()
      .flatMap(i -&gt; i)
      .collect(Collectors.toList());
  </code></pre>
  
  <p>The compiler tells me:</p>
  
  <pre><code>[ERROR] ... incompatible types: cannot infer type-variable(s) R
  [ERROR]     (argument mismatch; bad return type in lambda expression
  [ERROR]       Optional&lt;Integer&gt; cannot be converted to Stream&lt;? extends R&gt;)
  [ERROR]   where R,T are type-variables:
  [ERROR]     R extends Object declared in method &lt;R&gt;flatMap(Function&lt;? super T,? extends Stream&lt;? extends R&gt;&gt;)
  [ERROR]     T extends Object declared in interface Stream
  </code></pre>
  
  <p>I admit I'm not used to Java but I have to for a project. I mocked this in Scala where <code>list.flatten</code> and the equivalent <code>list.flatMap(i =&gt; i)</code> work just as expected:</p>
  
  <pre><code>val list = List(Some(1), Some(2), None)
  list.flatten // List(1, 2)
  </code></pre>
  
  <p>Is Java's <code>flatMap</code> different?</p>