Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter a list using a function which returns Mono<Boolean> in Java?

I have a list of String.

For example:

List<String> ids = new ArrayList<>(List.of("q1", "q2", "q3"));

I want to filter this list using a function that returns Mono<Boolean>. How do I do that?

function:

public Mono<Boolean> checkIfIdExists(String id)
{
  //External call
}
like image 787
Anmol Garg Avatar asked Sep 20 '25 04:09

Anmol Garg


1 Answers

It's probably easiest to create a Flux from your List then use filterWhen() on that:

Flux.fromIterable(ids).filterWhen(id -> !checkIfIdExists(id)).collectList()

(Note this will of course give you a Mono<List<String>> rather than a List<String>, as since your filter is reactive, the final result will be reative also. If you're not working reactively but just using a reactive library, you can just call block() to get the actual result.)

like image 136
Michael Berry Avatar answered Sep 21 '25 18:09

Michael Berry