If you change your code to use anyMatch instead of filter(...).findAny().isPresent() , it will work as expected:
boolean found = Arrays.asList(null, null, 1) .stream() .anyMatch(Objects::isNull); System.out.println(found);
Regarding why your version does not work with NPE, Stream.findAny docs says:
Throws:
NullPointerException - if the element selected is null
So this is the expected behavior.
EDIT: NPE arises because Optional.of used to construct the value returned by findAny() . And Optional.of requires a non-empty value according to the docs:
Returns an optional parameter with the specified current value other than zero.
In any case, I suppose you would like to know why Optional.of used instead of Optional.ofNullable when creating the value returned by findAny() ...
Well, I can only guess, but I think that findAny() and findFirst() are for finding values ββthat match some criteria, i.e. a Person whose name begins with A Perhaps you want to know if there is a null element in your stream. But in this case, you donβt really need to find such an element, because you already know that if you find it, it will, well ... just null . Therefore, it is enough to check only if there is null in your thread, and you can perfectly use anyMatch() to find this case.
In other words, it would be impractical to find the null element, because you cannot do anything with it (not knowing that it is null ).
EDIT 2: As user @ holi-java points out in his comment below, if findAny() returned Optional.ofNullable(null) , then there would be no way to find out if null was found or not. In this case, the result would be mixed, because Optional.ofNullable(null).equals(Optional.empty()) == true , that is, it will lead to confusion, since Optional.ofNullable(null).isPresent() == false , which means that the found matching value was not found.