flatMap() method of Mono/Flux object in Project Reactor

Compare to the flatMap() method in the Java Stream object or the flatMap() method in the Optional object in Java, we can manipulate the Stream or Optional objects of the simpler objects, the flatMap() method of the Mono object or the Flux object in the Project Reactor with the parameter as a Function, used to execute the function with the items that the Publisher (Mono/Flux) object emit, asynchronously, and then emit the result after manipulating with these items to continue performing other operations in the stream event.

For example, I have a simple Flux object as follows:

Using the flatMap() method, we can convert the data emitted from the Flux object above from String to Integer and emit this result by another Mono object. The example is as follows:

The result after converting from String to Integer will be passed to the map() method to print the console value as you can see!

Result:

flatMap() method of Mono/Flux object in Project Reactor

Mono’s flatMap() method is similar.

Add Comment