java How to convert an Optional<T> into a Stream<T>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33942614/
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
How to convert an Optional<T> into a Stream<T>?
提问by slartidan
I want to prepend a stream with an Optional. Since Stream.concat
can only concatinate Streams I have this question:
I want to prepend a stream with an Optional. Since Stream.concat
can only concatinate Streams I have this question:
How do I convert an Optional<T> into a Stream<T>?
How do I convert an Optional<T> into a Stream<T>?
Example:
Example:
Optional<String> optional = Optional.of("Hello");
Stream<String> texts = optional.stream(); // not working
回答by slartidan
In Java 8 you can do this:
In Java 8 you can do this:
Stream<String> texts = optional.map(Stream::of).orElseGet(Stream::empty);
回答by Tagir Valeev
In Java-9 the missing stream()
method is added, so this code works:
In Java-9 the missing stream()
method is added, so this code works:
Stream<String> texts = optional.stream();
See JDK-8050820. Download Java-9 here.
See JDK-8050820. Download Java-9 here.
回答by Konstantin Yovkov
You can do:
You can do:
Stream<String> texts = optional.isPresent() ? Stream.of(optional.get()) : Stream.empty();
回答by Utku ?zdemir
I can recommend Guava's Streams.stream(optional)
method if you are not on Java 9. A simple example:
I can recommend Guava's Streams.stream(optional)
method if you are not on Java 9. A simple example:
Streams.stream(Optional.of("Hello"))
Also possible to static import Streams.stream
, so you can just write
Also possible to static import Streams.stream
, so you can just write
stream(Optional.of("Hello"))
回答by JohnnyLambada
If you're on an older version of Java (lookin' at you, Android) and are using the aNNiMON Lightweight Stream API, you can do something along the lines of the following:
If you're on an older version of Java (lookin' at you, Android) and are using the aNNiMON Lightweight Stream API, you can do something along the lines of the following:
final List<String> flintstones = new ArrayList<String>(){{
add("Fred");
add("Wilma");
add("Pebbles");
}};
final List<String> another = Optional.ofNullable(flintstones)
.map(Stream::of)
.orElseGet(Stream::empty)
.toList();
This example just makes a copy of the list.
This example just makes a copy of the list.
回答by Gábor Lipták
A nice library from one of my ex collegues is Streamify. A lot of collectors, creating streams from practicly everything.
A nice library from one of my ex collegues is Streamify. A lot of collectors, creating streams from practicly everything.
https://github.com/sourcy/streamify
https://github.com/sourcy/streamify
Creating a stream form an optional in streamify:
Creating a stream form an optional in streamify:
Streamify.stream(optional)