This tutorial gives you examples of how to convert Mono<List<T>>
into Flux<T>
and vice versa.
Reactor has two reactive types: Mono
, which represents an empty or single value result (0..1) and Flux
, which represents a sequence of items (0..N). Though Mono
can only have maximum of one value, the value can be a List
(an Iterable
). In some cases, you may need to convert it into a Flux
. Or maybe the opposite case, you want to convert Flux
into a Mono
that contains Iterable
. For both cases, you can find the examples below.
Convert Mono<List<T>>
Into Flux<T>
For example, you have a Mono<List<Integer>
and you want to convert it into Flux<Integer>
, you can use .flatMapMany(Flux::fromIterable)
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(i);
}
Mono<List<Integer>> m = Mono.just(list);
Flux<Integer> f = m.flatMapMany(Flux::fromIterable);
Convert Flux<T>
Into Mono<List<T>>
Now,there is a Flux<Integer>
. To convert it into Mono<List<Integer>>
, use .collectList
.
Flux<Integer> f = Flux.range(1, 10);
Mono<List<Integer>> m = f.collectList();
That's how to convert between those two reactive types of Reactor.