project reactor - How to combine a Mono and a Flux?

While Kevin Hussey's solution is a correct one, I think it is better to have it another way around:

Mono<String> mono1 = Mono.just("x");
Flux<String> flux1 = Flux.just("{1}", "{2}", "{3}", "{4}");
mono1.flatMapMany(m -> flux1.map(x -> Tuples.of(x, m))).subscribe(System.out::println);

This way you have 1 subscription to mono1, instead of creating one for each value of flux1. See the marble diagram in the documentation for Flux.flatMap() method.

As suggested by Alan Sereb, I am using tuples.


Zip looks for pairs to add them together, your logic looks to change the values the flux.

Mono<String> mono1 = Mono.just("x");
Flux<String> flux1 = Flux.just("{1}", "{2}", "{3}", "{4}");   
flux1.flatMap(x -> mono1.map(m -> x+m)).subscribe(System.out::println);

In some cases, it is more complicated than concatenating strings so you have to rely on Tuples.

Flux<Object> myFlux = Flux.just(1,2,3,4);
Mono<Object> myMono = Mono.just("hello");

// combined flux with mono
myFlux.flatMap(fluxItem -> 
     myMono.map(monoItem -> Tuples.of(fluxItem, monoItem)))

// this will have pairs of ( (1, "hello"), (2, "hello"), (3, "hello"), (4, "hello") )