I am looking for method that can make stream of collection, but is null safe. If collection is null, empty stream is returned. Like this:
Utils.nullSafeStream(collection).filter(...);
I have created my own method:
public static <T> Stream<T> nullSafeStream(Collection<T> collection) {
if (collection == null) {
return Stream.empty();
}
return collection.stream();
}
But I am curious, if there is something like this in standard JDK?
null
collection in the first place. The best practice is to prefer using an empty collection instead.null
. A bit like in greg's solution.