空安全集合作为 Java 8 中的流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41590134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Null safe Collection as Stream in Java 8
提问by Gondy
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?
但是我很好奇,标准JDK中是否有这样的东西?
采纳答案by greg
You can use org.apache.commons.collections4.CollectionUtils::emptyIfNull function:
您可以使用 org.apache.commons.collections4.CollectionUtils::emptyIfNull 函数:
org.apache.commons.collections4.CollectionUtils.emptyIfNull(list).stream().filter(...);
回答by Eran
You could use Optional
:
你可以使用Optional
:
Optional.ofNullable(collection).orElse(Collections.emptySet()).stream()...
I chose Collections.emptySet()
arbitrarily as the default value in case collection
is null. This will result in the stream()
method call producing an empty Stream
if collection
is null.
我Collections.emptySet()
随意选择作为默认值,以防collection
为空。这将导致stream()
方法调用产生一个空的Stream
ifcollection
为 null。
Example :
例子 :
Collection<Integer> collection = Arrays.asList (1,2,3);
System.out.println (Optional.ofNullable(collection).orElse(Collections.emptySet()).stream().count ());
collection = null;
System.out.println (Optional.ofNullable(collection).orElse(Collections.emptySet()).stream().count ());
Output:
输出:
3
0
Alternately, as marstran suggested, you can use:
或者,正如 marstran 建议的那样,您可以使用:
Optional.ofNullable(collection).map(Collection::stream).orElse(Stream.empty ())...
回答by g-t
You can use something like that:
你可以使用这样的东西:
public static void main(String [] args) {
List<String> someList = new ArrayList<>();
asStream(someList).forEach(System.out::println);
}
public static <T> Stream<T> asStream(final Collection<T> collection) {
return Optional.ofNullable(collection)
.map(Collection::stream)
.orElseGet(Stream::empty);
}
回答by Didier L
Your collectionAsStream()
method can be simplified to a version even simpler than when using Optional
:
您的collectionAsStream()
方法可以简化为比使用更简单的版本Optional
:
public static <T> Stream<T> collectionAsStream(Collection<T> 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.
你想要的似乎只有在你需要返回流(包括平面映射)时才有用,或者可能将它与另一个连接。
回答by alltej
If downloading the library org.apache.commons.collections4
is not an option, you can just write your own wrapper/convenience method.
如果下载库org.apache.commons.collections4
不是一种选择,您可以编写自己的包装器/便利方法。
public static <T> Stream<T> asStream(final Collection<T> collection) {
return collection == null ? ( Stream<T> ) Collections.emptyList().stream()
: collection.stream();
}
Or wrapping the collection with Optional.ofNullable
或者用 Optional.ofNullable
public static <T> Stream<T> asStream(final Collection<T> collection) {
return Optional.ofNullable(collection)
.orElse( Collections.emptySet()).stream();
}
回答by fhiegel
Not sure if helps, but since Java 9, you can just write:
不确定是否有帮助,但是从 Java 9 开始,您可以编写:
Stream.ofNullable(nullableCollection)
.flatMap(Collection::stream)