Unlike the flatMap() method, the map() method of the Mono/Flux object in the Project Reactor will process the item being emitted, synchronously:
In the image above, green, yellow, and blue items emitted by Publisher will be processed by the map() method synchronously. The return result will be a Flux object with the items after being processed. You can add other tasks if you want.
My example is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.huongdanjava.projectreactor; import reactor.core.publisher.Flux; public class Example { public static void main(String[] args) { Flux<String> flux = Flux.just("Khanh", "Quan"); flux.map(value -> value.length() + 2) .log() .subscribe(); } } |
In this example, I just do a simple operation that takes the length of the string plus 2. Then use the log() method to see what the values of the returned items are.
The result is as follows: