Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is NullSafe way to concatenate two Streams?

I have two streams of integers guestsTravelWith and guests, which I am concatenating but throwing error when any one of stream is Null. Is there a nullsafe way to concatenate two streams? Or using if conditions is my only hope?

Stream<Integer> guests = code+some_method();
Stream<Integer> guestsTravelWith = code+some_method();
Stream.concat(guestsTravelWith, guests)
like image 216
Jishnu Prathap Avatar asked Dec 04 '22 17:12

Jishnu Prathap


1 Answers

Not nice at all, but:

Stream.ofNullable(guestsTravelWith).orElse(Stream.empty()).flatMap(Function.identity())

Or you know, the "less funner" way:

guests == null ? Stream.empty() : guests;

You should reconsider the methods that return null Stream to begin with, which is a terrible idea.

like image 53
Eugene Avatar answered Dec 19 '22 02:12

Eugene