How to convert an Optional into a Stream?

后端 未结 6 1614
孤独总比滥情好
孤独总比滥情好 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 04:17

    If restricted with Java-8, you can do this:

    Stream texts = optional.map(Stream::of).orElseGet(Stream::empty);
    

提交回复
热议问题