The following piece of code works :
// emitting employees...
.flatMap(employee -> {
boolean isAlive = employee.isAlive();
return Mono.just(isAlive)
.flatMap(myBoolean -> {
if (myBoolean) {
return Mono.empty();
}
else {
return Mono.just(employee);
}
});
})
But I was wondering why I can't use a .map upon processing myBoolean (NullPointerException when it returns the null)
.map(myBoolean -> {
if (myBoolean) {
return null;
}
else {
return employee;
}
});
I believe I lack some understanding about the map versus flatMap
In Java 8 streams I understand the difference between map and flatMap (for each item received, map emits 1, flatMap emits N)
But in Reactor I'm a bit confused. I thought that both map and flatMap emit 1 item for each element received, the difference would be that the map emits it as a Mono while flatMap doesn't. To emit N items, I would use flatMapMany.
Thanks in advance for your explanations !