Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

In this tutorial, we’ll be looking at the Maybe<T> type in RxJava – which represents a stream which can emit a single value, complete in an empty state or report an error.

2. The Maybe Type

Maybe is a special kind of Observable which can only emit zero or one item, and report an error if the computation fails at some point.

In this regard, it’s like a union of Single and Completable. All these reduced types, including Maybe, offer a subset of the Flowable operators. This means we can work with Maybe like a Flowable as long as the operation makes sense for 0 or 1 items.

Since it can only emit one value, it doesn’t support backpressure handling as with a Flowable:

Maybe.just(1)
  .map(x -> x + 7)
  .filter(x -> x > 0)
  .test()
  .assertResult(8);

There are onSuccess, onError and onComplete signals that can be subscribed from a Maybe source:

Maybe.just(1)
    .subscribe(
        x -> System.out.print("Emitted item: " + x),
        ex -> System.out.println("Error: " + ex.getMessage()),
        () -> System.out.println("Completed. No items.")
     );

The above code will print Emitted item: 1 since this source will emit a success value.

For the same subscriptions:

  • Maybe.empty().subscribe(…) will print “Completed. No items.”
  • Maybe.error(new Exception(“error”)).subscribe(…) will print “Error: error”

These events are mutually exclusive for Maybe. That is, onComplete won’t be invoked after onSuccess. This is slightly different from Flowable since onComplete will be invoked when the stream is complete even after possibly some onNext invocations.

Single doesn’t have onComplete signal like Maybe because it’s meant to capture a reactive pattern which can either emit one item or fail.

On the other hand, Completable lacks onSuccess since it’s meant to deal with complete/fail situations only.

Another use case for the Maybe type is using it in combination with Flowable. The firstElement() method can be used to create Maybe from Flowable:

Flowable<String> visitors = ...
visitors
  .skip(1000)
  .firstElement()
  .subscribe(
    v -> System.out.println("1000th visitor: " + v + " won the prize"), 
    ex -> System.out.print("Error: " + ex.getMessage()), 
    () -> System.out.print("We need more marketing"));

4. Conclusion

In this short tutorial, we quickly looked on RxJava Maybe<T> usage, and how it relates to other reactive types like Flowable, Single and Completable.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW