将两个流合并为一个磁通 [英] Combine two Stream into one Flux

查看:75
本文介绍了将两个流合并为一个磁通的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将两个流Stream<String>合并为Flux?我了解的是,我可能需要使用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(..);
        }
    });
})

请帮助.

推荐答案

Stream合并为一个,然后调用Flux#fromStream:

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

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

另一种方法是使用Flux#fromStream然后使用Flux#merge创建Flux:

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

这篇关于将两个流合并为一个磁通的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆