Close

Reactor - Using transform Operation

[Updated: Jan 20, 2020, Created: Jan 20, 2020]

Following functions are used to transform Flux/Mono to generate a target Flux/Mono:

Class Flux

public final <V> Flux<V> transform(Function<? super Flux<T>,? extends Publisher<V>> transformer)

Class Mono

public final <V> Mono<V> transform(Function<? super Mono<T>,? extends Publisher<V>> transformer)

The advantage of using above methods over map() is that transform() methods can be used to reuse and mutualize code.

Examples

public class TransformExample {
  public static void main(String[] args) {
      System.out.println("-- Transforming Flux --");
      Function<Flux<Integer>, Flux<Integer>> transformingFluxFunction =
              flux -> flux.map(i -> i * 2);
      Flux.just(2, 4, 6, 8)
          .transform(transformingFluxFunction)
          .subscribe(System.out::println);

      System.out.println("-- Transforming Mono --");
      Function<Mono<String>, Mono<Integer>> transformingMonoFunction =
              mono -> mono.map(s -> s.length());
      Mono.just("supercalifragilisticexpialidocious")
          .transform(transformingMonoFunction)
          .subscribe(System.out::println);
  }
}
-- Transforming Flux --
4
8
12
16
-- Transforming Mono --
34

Example Project

Dependencies and Technologies Used:

  • reactor-core 3.3.2.RELEASE: Non-Blocking Reactive Foundation for the JVM.
  • JDK 8
  • Maven 3.5.4

Reactor - Using transform() operator Select All Download
  • reactor-transform-operation
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • TransformExample.java

    See Also