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 1st 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 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

Since the introduction of Java 8, a lot of people started using the (new) stream functionality. Of course, there are moments when our stream operations don’t work as expected.

IntelliJ has, besides its normal debugging options, a dedicated stream debugging function. In this short tutorial, we’ll explore this great feature.

2. The Stream Trace Dialog

Let’s start by showing how to open the Stream Trace dialog. In the toolbar of the debug window, there’s a Trace Current Stream Chain option that’s only enabled when our application pauses on a breakpoint inside a stream API call:

stream tracer

Clicking the option will open the Stream Trace dialog.

The dialog has two modes. We’ll take a look at Flat Mode in the first example. And, in the second example, we’ll show the default mode, which is Split Mode.

3. Examples

Now that we’ve introduced the stream debugging functionality in IntelliJ, it’s time to work with some code examples.

3.1. Basic Example with a Sorted Stream

Let’s start with a simple code fragment to get used to the Stream Trace dialog:

int[] listOutputSorted = IntStream.of(-3, 10, -4, 1, 3)
  .sorted()
  .toArray();

Initially. we have a stream of unordered int. Next, we sort that stream and convert it to an array.

When we view the Stream Trace in Flat Mode, it shows us an overview of the steps that occur:

stream trace flat mode

On the far left, we see the initial stream. It contains the ints in the order in which we wrote them.

The first set of arrows shows us the new location of all the elements after sorting. And at the far right, we see our output. All items appear there in sorted order.

Now that we’ve seen the basics, it’s time for a more complex example.

3.2. Example Using flatMap and filter

This next example uses flatMap. Stream.flatMap helps us, for example, to convert a list of Optionals to a normal list. In this next example, we start with a list of Optional Customers. We then map it to a list of Customers and apply some filtering:

List<Optional<Customer>> customers = Arrays.asList(
    Optional.of(new Customer("John P.", 15)),
    Optional.of(new Customer("Sarah M.", 78)),
    Optional.empty(),
    Optional.of(new Customer("Mary T.", 20)),
    Optional.empty(),
    Optional.of(new Customer("Florian G.", 89)),
    Optional.empty()
);

long numberOf65PlusCustomers = customers
  .stream()
  .flatMap(c -> c
    .map(Stream::of)
    .orElseGet(Stream::empty))
  .mapToInt(Customer::getAge)
  .filter(c -> c > 65)
  .count();

Next, let’s view the Stream Trace in Split Mode, which gives us a better overview of this stream.

On the left, we see the input stream. Next, we see the flat-mapping of the stream of Optional customers to the stream of actual present customers:

stream flat Map

After that, we map our stream of customers to their ages:

stream map ToInt

The next step filters our stream of ages to a stream of ages greater than 65:

stream filter

Finally, we count the number of items in our stream of ages:

stream count

4. Caveats

In the examples above, we’ve seen some of the possibilities offered by the Stream Trace dialog. However, there are some important details to be aware of. Most of them are a direct consequence of how streams work.

Firstly, streams always need terminal operations to be executed. This is no different when using the Stream Trace dialog. Also, we must be aware of operations that do not consume the entire stream — for example, anyMatch. In this case, it will not show all elements — just the elements that are processed.

Secondly, be aware that the stream will be consumed. If we declare the Stream separately from its operations, we might run into the error “Stream has already been operated upon or closed”. We can prevent this error by joining the declaration of the stream with its usage.

5. Conclusion

In this quick tutorial, we’ve seen how to use IntelliJ’s Stream Trace dialog.

First, we looked at a simple case showing sorting and collecting. Then, we looked at a more complex scenario involving flat-mapping, mapping, filtering, and counting.

Finally, we looked into some caveats that we might bump into while using the stream debugging functionality.

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 1st 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 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW