Project Reactor Mono
- Java course
- Expert
- Lesson 23/46
- 5 min
Learn single-value reactive streams with Project Reactor Mono.
Project Reactor and Mono
Project Reactor is a popular library that implements Reactive Streams. It offers two main flow types: a Mono carries at most one item (0 or 1), and a Flux carries many items. This classifies flows by item count.
Example: Mono.just(5) is a Mono that produces the single value 5. On it you can add a transform with .map(x -> x * 2), a filter with .filter(x -> x > 1), and consumption with .subscribe(...). Each operation returns a new flow, so the chain reads declaratively.
The operator chain
In Reactor, operations form a chain. Because each operation returns a new flow, they are written in sequence like Mono.just(3).map(x -> x + 1).filter(x -> x > 2).subscribe(...).
This gives a declarative style: what to do is written step by step, from flow to flow. map transforms, filter filters, flatMap flattens nested flows. Nothing runs until subscribe is called; this lazy behavior avoids needless computation.
Practice Transform and print a value with Mono
Create a Mono with Mono.just(5). Double the value with .map(x -> x * 2). Print the result with .subscribe(System.out::println).
Starter code
// Create a Mono with Mono.just
// Double with map
// Print with subscribeSample solution
import reactor.core.publisher.Mono;
public class MonoDemo {
public static void main(String[] args) {
Mono.just(5)
.map(x -> x * 2)
.subscribe(System.out::println);
}
}Expected output
10
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match the Reactor types
| Term | Meaning |
|---|---|
Mono |
Single value |
Flux |
Many values |
map |
Transforms |
subscribe |
Starts consumption |
2 Complete the Mono.just call
Mono.____(5)
Mono.just(5)
Explanation: Mono.just creates a Mono with a single value.
3 Which Reactor type produces a single value?
- Optional
- Flux
- Stream
- Mono Correct answer
Explanation: A Mono carries at most one item.