'A difference between Mono.mapNotNull(....) and Mono.flatMap(Mono.justOrEmpty(....))

Being a novice to Project Reactor I'm trying to wrap my head around the documentation. However I still can't figure out if there any practical difference between the following (pseudo-)code:

aMonoObject
    .flatMap(value -> Mono.justOrEmpty(transform(value))
    .someOtherProcessing(...)

and

aMonoObject
    .mapNotNull(value -> transform(value))
    .someOtherProcessing(...)

given that transform is a transformation method which is declared like this:

@Nullable <T, R> R transform(T value);

The activity diagrams look quite similar in the docs, I'm not sure that I understand everything correctly.

Thank you in advance.



Solution 1:[1]

The result will be the same but there is a semantic difference between the two. In your case, the mapNotNull is a better choice because it does exactly what you need: transforms the emitted value within the existing stream. You could use Mono.justOrEmpty if you wanted to start a new stream.

A similar question would be what is the difference between Mono.just and map operator.

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 lkatiforis