Transforming Data
Operators to modify, filter, and transform data flowing through streams.
Basic Transformations
// Map - synchronous transformationFlux<Integer> numbers = Flux.range(1, 5) .map(n -> n * 2); // [2, 4, 6, 8, 10]
// FlatMap - asynchronous transformationFlux<String> results = Flux.range(1, 3) .flatMap(n -> Mono.fromCallable(() -> fetchFromDatabase(n)) .subscribeOn(Schedulers.boundedElastic()) );
// FilterFlux<Integer> evenNumbers = Flux.range(1, 10) .filter(n -> n % 2 == 0);Complex Transformations
Section titled “Complex Transformations”// concatMap (preserves order)Flux<String> orderedResults = Flux.range(1, 3) .concatMap(n -> simulateAsync(n)); // Guaranteed order: 1, 2, 3
// switchMap (cancels previous)Flux<String> searchResults = searchTermFlux .switchMap(term -> searchApi(term)); // Cancels previous search
// groupByFlux<GroupedFlux<Integer, String>> groups = Flux.just("apple", "banana", "cherry", "date") .groupBy(word -> word.length());Transformation Pipeline
Section titled “Transformation Pipeline”graph LR
A[Source] --> B[Filter]
B --> C[Map]
C --> D[FlatMap]
D --> E[Buffer]
E --> F[Subscriber]
Common Operators:
Section titled “Common Operators:”- map: Transform each element
- filter: Keep only matching elements
- flatMap: Transform to new Publisher
- concatMap: Preserve order of results
- switchMap: Cancel previous operations