Combine two Stream into one Flux

时光毁灭记忆、已成空白 提交于 2021-01-29 14:12:45

问题


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.


回答1:


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);


来源:https://stackoverflow.com/questions/61971297/combine-two-stream-into-one-flux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!