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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With