I want to convert an Optional<T>
to a Stream<T>
:
- If the optional does hold a value, the stream contains that value.
- If the optional does not hold a value, the stream should be empty.
I came up with the following:
public static Stream<T> stream(Optional<T> optional) {
return optional.map(Stream::of).orElseGet(Stream::empty);
}
Is there a shorter way?