I have a Stream<Integer>
and want to know if there is a null
in this stream. How do I check that? Using .anyMatch(null)
throws me a java.lang.NullPointerException
.
We can use lambda expression str -> str!= null inside stream filter() to filter out null values from a stream.
In any of the cases your code is going to throw a NullPointerException. The exception can be avoided by adding Null checks.
anyMatch
accepts a predicate.
stream.anyMatch(x -> x == null)
or
stream.anyMatch(Objects::isNull)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With