How to convert an Optional into a Stream?

后端 未结 6 1609
孤独总比滥情好
孤独总比滥情好 2020-12-05 03:56

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

6条回答
  •  悲&欢浪女
    2020-12-05 04:28

    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

    stream(Optional.of("Hello"))
    

提交回复
热议问题