Skip to content
Advertisement

Combine two Stream into one Flux

How can I combine two streams Stream<String> into Flux? What I understand is that I might need to use Flux create method to create this but I am not really sure about it:

flux1.create(sink -> {
    sink.onRequest(L -> {
        for(long l = 0; l < L; l++) {
            sink.next(..);
        }
    });
})

Please help.

Advertisement

Answer

Concat the Streams into one and then invoke Flux#fromStream:

Flux<String> flux = Flux.fromStream(Stream.concat(stream1, stream2));

Another way of doing this would be to create a Flux using Flux#fromStream and then Flux#merge:

Flux<String> flux = Flux.merge(flux1, flux2);
Advertisement