Reputation: 880
I have a
list.stream()
.map(element -> func());
where func() returns with Mono. What is the easiest way to convert this list of Mono<CustomObject>
objects to Flux<CustomObject>
and return it from the stream?
Upvotes: 7
Views: 13406
Reputation: 1994
You mention a list of mono but your code seems to indicate a stream.
If you want to use Flux.concat
(see doc), you will need to collect your stream into an iterable, like list
var yourCode = list.stream().map(element -> func());
var whatYouWant = Flux.concat(yourCode.toList());
If you want to keep your stream, you can use Flux.fromStream
(see doc) with Flux.flatMap
(see doc):
var yourCode = list.stream().map(element -> func());
var whatYouWant = Flux.fromStream(yourCode).flatMap(Function.identity())
Upvotes: 1
Reputation: 1832
If you have an operation that maps a List<T>
to a List<Mono<U>>
then you can convert the original List<T>
into a Flux<T>
, for example.
Instead of:
List<T> entities = ArrayList<>();
List<Mono<U>> monos = entities.map (this::operationThatReturnsMono);
You can do something like:
List<T> entities = ArrayList<>();
Flux<U> flux = Flux.fromIterable(entities).flatMap(this::operationThatReturnsMono)
Upvotes: 0
Reputation: 197
List<Mono<CustomObject >> monoList = new ArrayList<>();
monoList(object);
Flux<CustomObject> flux = Flux.concat(monoList);
Upvotes: 13