liveBook · Manning

Map

As explained in Spring in Action, Sixth Edition

map is a Project Reactor operator that transforms each value emitted by a reactive publisher into another value while preserving the publisher type.

`map()` transforms each value emitted by a Reactor `Flux` or `Mono`. Give it a function that accepts one value and returns another; the result remains a `Flux` or `Mono`, but its value type can change. For example, a `Flux<String>` of names can become a `Flux<Player>`. section 11.3.3 section 11.2

@Test
public void map() {
  Flux<Player> playerFlux = Flux
    .just("Michael Jordan", "Scottie Pippen", "Steve Kerr")
    .map(n -> {
      String[] split = n.split("\\s");
      return new Player(split[0], split[1]);
    });
  
  StepVerifier.create(playerFlux)
      .expectNext(new Player("Michael", "Jordan"))
      .expectNext(new Player("Scottie", "Pippen"))
      .expectNext(new Player("Steve", "Kerr"))
      .verifyComplete();
}
 
@Data
private static class Player {
  private final String firstName;
  private final String lastName;
}

Follow the lambda from each player-name string to its `Player` object. The StepVerifier assertions check the values emitted by the resulting Flux. section 11.3.3

Mapping happens synchronously as each source item is published. If the transformation itself returns a `Mono` or `Flux`, use `flatMap()` to avoid nesting publishers; paired with `subscribeOn()`, it can support asynchronous transformations. section 11.3.3 section 13.1.4

Supply reactive security with user details

A reactive user repository returns `Mono<User>`, while `ReactiveUserDetailsService.findByUsername()` must return `Mono<UserDetails>`. Return `userRepo.findByUsername(username).map(user -> user.toUserDetails())`. If no user matches, the Mono is empty and authentication fails. section 12.5.2

Put saved tacos back into an order

When a repository emits saved `Taco` objects, `.map(taco -> { order.addTaco(taco); return order; })` adds each taco, including its assigned ID, to the order and emits `TacoOrder` rather than `Taco`. The surrounding save operation uses `flatMap()` because its transformation returns a publisher. section 13.1.4

How Spring in Action, Sixth Edition covers it

How deep it goes
Defines synchronous per-item mapping, demonstrates typed transformations, and contrasts flatMap() when a transformation returns a publisher.
Examples
Tests basketball-player name conversion with StepVerifier and reassembles TacoOrder aggregates from repository-emitted tacos.
What it assumes
Introduces Flux and Mono as publishers, then uses Java lambdas and reactive repository methods in working examples.
What you can do after
Apply it via Reactor's Flux and Mono to transform values into types required by downstream services.
How it connects
Connects Flux, Mono, flatMap(), subscribeOn(), ReactiveUserDetailsService, and reactive repositories through transformation pipelines.

Also explained in

Compare how 8 books explain map →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.