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.nullSafeStrea
Your collectionAsStream()
method can be simplified to a version even simpler than when using Optional
:
public static Stream collectionAsStream(Collection collection) {
return collection == null ? Stream.empty() : collection.stream();
}
Note that in most cases, it's probably better to just test for nullness before building the stream pipeline:
if (collection != null) {
collection.stream().filter(...)
} // else do nothing
What you want seems to be only useful when you need to return the stream (including for flatmapping), or maybe concatenate it with another one.