Null safe Collection as Stream in Java 8

后端 未结 6 1472
天涯浪人
天涯浪人 2020-12-13 02:01

I am looking for method that can make stream of collection, but is null safe. If collection is null, empty stream is returned. Like this:

Utils.nullSafeStrea         


        
6条回答
  •  借酒劲吻你
    2020-12-13 02:25

    Not sure if helps, but since Java 9, you can just write:

    Stream.ofNullable(nullableCollection)
          .flatMap(Collection::stream)
    

提交回复
热议问题