'Using Filter operator in Reactor Java
I am trying to filter out the name which are not present in DB results. It's not working
Data Present in Db:
A,B,C,D
Payload Contains:
C,D,E,X
Expected Result:
E,X
Below is the code snippet.
public void filterName(Flux<String> payloadList)
{
nameRepo.findAll() <-- DB call which is reactiverepository
.map(dbObj->dbObj.getName())
.collectList().flatMapMany(Mono::just)
.map(dbNameList->payloadList.filter(name->
!dbNameList.contains(name)).subscribe())
.subscribe(z-> System.out.println(z));;
}
In result I am getting LambdaSubscriber object.
Solution 1:[1]
The method collectList returns a Mono<List<String>> that can be transformed to Flux.
Therefore, using flatMapMany it becomes payloadList filtering out the dbNameList.
public void filterName(Flux<String> payloadList) {
nameRepo.findAll() < --DB call which is reactive repository
.map(dbObj -> dbObj.getName())
.collectList()
.flatMapMany(dbNameList -> payloadList.filter(name -> !dbNameList.contains(name)))
.subscribe(System.out::println);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | frascu |
