Stream.empty的实际用途是什么?

Stream#empty返回一个空的序列Stream。从本教程中,我了解到空流可能有助于在调用带有流参数的方法时避免空指针异常。但是,我想不出一个例子可以帮助我清楚地理解这个陈述。

回答

假设您有一个包含某些逻辑StreamflatMap操作,您希望在Stream#empty满足某些条件时进行调用

listOfLists.stream().flatMap(list -> {
    // complex business logic, maybe even extracted in another method
    return xxx ? someList.stream() : Stream.empty();
});

另一个例子是,就像Optional<T>在方法返回的情况下使返回更安全Stream<T>(假设是工厂方法)

public <T> Stream<T> makeFooStreamFrom(Object... parameters) {
    // decide on what to do based on the parameters
    return decisionMadeToReturnAnEmptyStream ? Stream.empty() : Stream.of(foo, bar);
} 

至于方法接受Stream参数的例子,可以像这样给出一个很好的例子

public <T> void consumeStream(@NotNull Stream<T> stream) {
    // In this example, if this stream is null, you'll get a NullPointerException
    // However, if you make sure to invoke this method using Stream#empty
    // No need to worry about it
    // This is the security that Stream#empty will provide in this case
    stream.map(...).filter(...).forEach(...);
}

@daniu 带来的第四个例子是当你有一个 nullable 时List。这在java-9之前最相关,正如java-9引入的那样Stream#ofNullable,顺便说一句,它Stream#empty也在内部使用

这是它的实现,也是第四个例子

public static<T> Stream<T> ofNullable(T t) {
    return t == null ? Stream.empty()
                     : StreamSupport.stream(new Streams.StreamBuilderImpl<>(t), false);
}

我们会认为上面的示例并不相关,因为它Stream无论如何都会返回 a ,但是底层实现使用了一个EMPTY_SPLITERATOR内部命名的常量,这将有助于提高性能,因为它是可重用的。

  • Note that `flatMap` explicitly allows the function to return `null` instead of `Stream.empty()`, to be treated the same. But people may prefer `Stream.empty()` as a matter of coding style. Further, you should not have “a nullable `List`”. Use an empty list instead. But, of course, `Collections.emptyList().stream()` also has to return a value, and it will be `Stream.empty()` in most implementations…

以上是Stream.empty的实际用途是什么?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>