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