Java
Mono class
Reactive Programming
Java Streams
Asynchronous Programming

Mono class in Java what is, and when to use?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Mono is a Project Reactor type that represents an asynchronous computation producing zero or one value. It is the reactive equivalent of saying, “there may be one result later, or there may be none, or the operation may fail.” Use it when your workflow is naturally single-result and non-blocking, such as fetching one database row, calling one HTTP endpoint, or saving one entity.

What Mono Represents

A Mono can complete in one of three ways:

  • emit one value and complete
  • complete without a value
  • terminate with an error

That makes it different from Flux, which can emit many values.

java
1import reactor.core.publisher.Mono;
2
3public class Demo {
4    public static void main(String[] args) {
5        Mono<String> greeting = Mono.just("hello");
6        greeting.subscribe(System.out::println);
7    }
8}

Even though the example is simple, the important idea is that the result is wrapped in a pipeline, not returned immediately as a plain value.

Use Mono for Single-Result Asynchronous Work

A good rule is simple:

  • use Mono for zero-or-one result operations
  • use Flux for many-result operations

For example, a repository method that fetches one user by id is a natural Mono<User>.

java
1import reactor.core.publisher.Mono;
2
3class UserService {
4    Mono<String> findDisplayNameById(long id) {
5        return Mono.just("user-" + id);
6    }
7}

In a real application, the value might come from a reactive database driver or a remote API call. The API still communicates the same contract: one result at most.

Transforming a Mono

Reactor becomes useful when you start composing work.

java
1import reactor.core.publisher.Mono;
2
3public class Demo {
4    public static void main(String[] args) {
5        Mono<String> result = Mono.just("mark")
6            .map(String::trim)
7            .map(String::toUpperCase)
8            .map(name -> "Hello, " + name);
9
10        result.subscribe(System.out::println);
11    }
12}

Use map when the transformation stays synchronous and returns a normal value.

Use flatMap when the next step itself returns a Mono.

java
1Mono<String> loadUser(long id) {
2    return Mono.just("user-" + id);
3}
4
5Mono<String> greeting = Mono.just(42L)
6    .flatMap(Demo::loadUser)
7    .map(name -> "Hello, " + name);

That distinction matters because flatMap prevents nested reactive types such as Mono<Mono<String>>.

Mono Is Not the Same as Optional

A Mono<T> and an Optional<T> can both describe the presence or absence of a value, but they solve different problems.

Optional is synchronous and in-memory. Mono is asynchronous and part of a reactive stream pipeline. If your method already has the value right now, Optional may be enough. If the value arrives later from a non-blocking source, Mono is the better abstraction.

When Not to Use It

Do not introduce Mono just because Reactor is available. If the code is entirely synchronous and you are not composing reactive operations, adding Mono can make simple code harder to read.

Use it when the surrounding stack is reactive or when the operation itself benefits from non-blocking composition.

Common Pitfalls

  • Using Mono for collections when the real result is many items and should be a Flux.
  • Calling .block() everywhere and losing the benefits of reactive composition.
  • Confusing map with flatMap.
  • Treating Mono as a thread rather than as a description of asynchronous work.
  • Wrapping already-simple synchronous code in Mono without a real need.

Summary

  • 'Mono represents an asynchronous result with zero or one value.'
  • It is appropriate for single-item operations such as one database lookup or one HTTP response.
  • Use map for synchronous transformations and flatMap for reactive ones.
  • Prefer Flux when many items may be emitted.
  • Do not use Mono unless the problem is actually reactive or asynchronous.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.