Here is my example:
import java.util.ArrayList;
import java.util.stream.IntStream;
public class Example {
public static void main( String[] args ) {
ArrayList<Integer> list = new ArrayList<>();
list.add(0);list.add(1);list.add(2);
SomeObject a = IntStream.range(0,list.size())
.filter(i->list.get(i)==3 || list.get(i)==4)
.mapToObj(i->new SomeObject(i,list.get(i)))
.findFirst()
.orElse(null);
System.out.println(a);
}
}
class SomeObject{
int index;
int value;
SomeObject(int index,int value){
this.index=index;
this.value=value;
}
@Override
public String toString() {
return this.index+" "+this.value;
}
}
Why when we are calling System.out.println(a);
we don't get a NullPointerException?
As the orElse(null) returns an object pointing to null. If not what is the difference between the null in the orElse method and an object pointing to null?