为什么在方法reference(::)中使用实例方法时,NullPointerException如果我们将Object实例设置为null ,则不抛出?
public class MethodReferenceTest {
public static void main(String...strings){
PredicateSample predicateSample = new PredicateSample();
Predicate<String> predicate = predicateSample::isNotEmpty;
// NullpointerException ???
predicateSample = null;
Arrays.asList("a","b","c",null)
.stream()
.filter(predicate)
.forEach(System.out::println);
}
static class PredicateSample{
public boolean isNotEmpty(String s){
return ((s!=null) && (!s.isEmpty()));
}
}
}
Run Code Online (Sandbox Code Playgroud)
predicateSample在构造Predicate之后似乎没有调用?