如何检查流中是否为空?
How to check if null is in stream?
我有一个 Stream<Integer>
,想知道这个流中是否有一个 null
。我该如何检查?使用 .anyMatch(null)
会给我一个 java.lang.NullPointerException
.
anyMatch
接受一个 谓词 .
stream.anyMatch(x -> x == null)
或
stream.anyMatch(Objects::isNull)
我有一个 Stream<Integer>
,想知道这个流中是否有一个 null
。我该如何检查?使用 .anyMatch(null)
会给我一个 java.lang.NullPointerException
.
anyMatch
接受一个 谓词 .
stream.anyMatch(x -> x == null)
或
stream.anyMatch(Objects::isNull)