You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Transform the item emitted by this Mono by applying a synchronous function to it.
flatMap
Transform the item emitted by this Mono asynchronously, returning the value emitted by another Mono (possibly changing the value type).
Flux
method
description
map
Transform the items emitted by this Flux by applying a synchronous function to each item.
flatMap
Transform the elements emitted by this Flux asynchronously into Publishers, then flatten these inner publishers into a single Flux through merging, which allow them to interleave.
Differences
1. One-to-One vs One-to-Many
<V> Flux<V> map(Function<? superT, ? extendsV> mapper)
// – the mapper converts a single value of type T to a single value of type VFlux<R> flatMap(Function<? superT, ? extendsPublisher<? extendsR>> mapper)
– themapperconvertsasinglevalueoftypeTtoaPublisherofelementsoftypeR
map 의 경우는, 스트림의 아이템들에 대해 one-to-one 으로 값 변형을 실행하고, flatMap 은 one-to-many로 변형한다.
2. Synchronous vs Asynchronous
map 과 flatMap 에서 가장 두드러지는 차이는 바로, 전자는 동기적으로 subscriber에 item들이 변형이 되어 전달된다는 점이고, 후자는 비동기적으로 실행된다는 점이다.
map 은 동기적 연산자인데, 단순히 하나의 값을 다른 하나로 변환해주는 메서드일 뿐이다. 이 메서드는 호출한 곳과 같은 스레드에서 실행된다.