Reactor Map Vs Flatmap

Map is synchronous whereas FlatMap is asynchronous.

Map

Using map we can transform the reactor synchronously, for example:

public static Flux<User> capitaliseMany(Flux<User> userFlux) {  
  return userFlux.map(user -> new User(user.username.toUpperCase()));  
}
public static void capitaliseManyMain() {  
  Flux<User> userFlux = Flux.just(new User("rockmanvnx6"), new User("test123"));  
  capitaliseMany(userFlux).subscribe((user) -> System.out.println(user.username));  
}

This will transform the Flux into another Flux synchronously.

Flatmap

Using flatmap we can transform the reactor asynchronously, for example

public static Mono<User> asyncCapitaliseOne(User user) {  
  return Mono.just(new User(user.username.toUpperCase()))  
      .delayElement(Duration.ofMillis(new Random().nextInt(1000)));  
}
public static Flux<User> asyncCapitaliseMany(Flux<User> userFlux) {  
  return userFlux.flatMap(TransformApp::asyncCapitaliseOne);  
}
public static void asyncCapitaliseManyMain() throws InterruptedException {  
  CountDownLatch countDownLatch = new CountDownLatch(1);  
  
  Flux<User> userFlux = Flux.just(  
      new User("rockmanvnx6"),  
      new User("test123"),  
      new User("ntthnue"),  
      new User("gunny9441")  
  );  
  asyncCapitaliseMany(userFlux)  
      .doOnComplete(countDownLatch::countDown)  
      .subscribe(user -> System.out.println(user.username));  
  
  countDownLatch.await();  
}
GUNNY9441
ROCKMANVNX6
TEST123
NTTHNUE