Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if null is in stream?

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.

like image 369
principal-ideal-domain Avatar asked Aug 20 '15 09:08

principal-ideal-domain


People also ask

How do I check if a stream is null?

We can use lambda expression str -> str!= null inside stream filter() to filter out null values from a stream.

How do I stop null pointer exception in streams?

In any of the cases your code is going to throw a NullPointerException. The exception can be avoided by adding Null checks.


1 Answers

anyMatch accepts a predicate.

stream.anyMatch(x -> x == null)

or

stream.anyMatch(Objects::isNull)
like image 65
Marko Topolnik Avatar answered Oct 10 '22 01:10

Marko Topolnik