Reputation: 875
Suppose we have
List<String> list
contains "A", "B", "C", "D"
Flux<String> flux
contains "A", "B"
Is there a way to filter out flux from list? In other words, subtract flux from list, where the results should be "C", "D"
.
Looking through the documentation for reactor, filterWhen
seems to be the closest, but it only replays the first element match the condition, all subsequent matches will be ignored.
This can be easily achieved in non-reactive world, for List or Collections, e.g. Subtracting one arrayList from another arrayList.
Upvotes: 2
Views: 3098
Reputation: 3421
It's likely that you are looking to use the collectList()
method.
Flux::collectList
will take a Flux<String>
and emit a Mono<List<String>>
.
This will conveniently give you the ability to run whatever collection comparison operation you need to run over that dataset.
With that you can provide a .map()
operation that operates and converts it to your desired results.
Mono<List<String>> monoWithRemovedElements = flux.collectList()
.map(fluxTurnedIntoList -> /*(subtract array list)*/)
If you would like to fan this back out into a Flux you can use the Mono::flatMapMany
method.
Flux<String> fluxWithRemovedElements = monoWithRemovedElements
.flatMapMany(list -> Flux.fromIterable(list))
Upvotes: 4
Reputation: 682
you need to first convert the Flux into ArrayList and then remove the elements from the list based on what elements are in Flux.
List<String> fluxedList = flux.collectList().block();
fluxedList.stream().forEach( elem -> list.remove(elem));
Upvotes: 0