title: java 8 findFirst vs map on optional
body: <p>Given this code:</p>
  
  <pre><code>class Foo {
    Integer attr;
    public Integer getAttr() {return attr;}
  }
  
  List&lt;Foo&gt; list = new ArrayList&lt;&gt;();
  list.add(new Foo());
  
  list.stream().map(Foo::getAttr).findAny().orElse(null);  //A
  list.stream().findAny().map(Foo::getAttr).orElse(null);  //B
  </code></pre>
  
  <p>Line A throws </p>
  
  <blockquote>
    <p>java.lang.NullPointerException: null</p>
  </blockquote>
  
  <p>while line B returns null.</p>
  
  <p>What is the cause of this behaviour? Both <code>findAny()</code> and <code>map()</code> return <code>Optional&lt;T&gt;</code>.</p>