1. Basic introduction
Map can transform data elements into a mapping table to get a new element. The map operator is shown in Figure 6-3.
The upper arrow in the figure is the timeline of the original sequence, and the lower arrow is the timeline of the data sequence after map processing. Map accepts a functional interface, which is used to define the strategy of transformation operation:
public final <V> Flux<V> map(Function<? super T,? extends V> mapper) public final <R> Mono<R> map(Function<? super T, ? extends R> mapper)
2, code
2.1 normal conditions
2.1.1 normal code
package com.test.reactor; import reactor.core.publisher.Flux; import reactor.test.StepVerifier; /** * @map Operator test */ public class MapOperatorDemo { public static void main(String[] args) { // Generate 6 integer data from 1 in steps of 1 StepVerifier.create(Flux.range(1, 6) // Cube elements .map(i -> i * i * i)) // expected value .expectNext(1, 8, 27, 64, 125, 216) // Simulation of abnormal conditions //.expectNext(10, 8, 27, 64, 125, 216) // Completion signal .expectComplete() .verify(); } }
2.1.2 operation results
No abnormal output2.2 abnormal conditions
2.2.1 exception code
package com.test.reactor; import reactor.core.publisher.Flux; import reactor.test.StepVerifier; /** * @map Operator test */ public class MapOperatorDemo { public static void main(String[] args) { // Generate 6 integer data from 1 in steps of 1 StepVerifier.create(Flux.range(1, 6) // Cube elements .map(i -> i * i * i)) // expected value //.expectNext(1, 8, 27, 64, 125, 216) // Simulation of abnormal conditions .expectNext(10, 8, 27, 64, 125, 216) // Completion signal .expectComplete() .verify(); } }
2.2.2 abnormal operation results
Exception in thread "main" java.lang.AssertionError: expectation "expectNext(10)" failed (expected value: 10; actual value: 1)