如何检查null是否在流中?

时间:2022-04-16 12:02:54

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.

我有一个流 ,想知道这个流中是否有null。怎么检查呢?使用. anymatch (null)将向我抛出java.lang.NullPointerException。

1 个解决方案

#1


17  

anyMatch accepts a predicate.

anyMatch接受一个谓词。

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

or

stream.anyMatch(Objects::isNull)

#1


17  

anyMatch accepts a predicate.

anyMatch接受一个谓词。

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

or

stream.anyMatch(Objects::isNull)