IntelliJ IDEA
 
Get IntelliJ IDEA
You are viewing the documentation for an earlier version of IntelliJ IDEA.

Analyze Java Stream operations

Last modified: 11 February 2024

Java 8 Streams may sometimes be difficult to debug. This happens because they require you to insert additional breakpoints and thoroughly analyze each transformation inside the stream. IntelliJ IDEA provides a solution to this by letting you visualize what is going on in Java Stream operations.

Let's take a simple program written in functional style to demonstrate how the feature works.

As its name suggests, the PrimeFinder app finds prime numbers. You can specify the starting number and the number of candidates to check using the program arguments. The checking logic is handled by a Java 8 Stream.

Now if we look at the program output, we see extra numbers there.

Concise as the functional style may be, it is not always easy to debug. To understand where these incorrect numbers are coming from, use the stream debugger feature.

The examination of the stream gave us a clue about the cause of the problem. We passed a method reference to filter and it returned an extra value. The search of a bug is now narrowed down to the Predicate of the filter operation, that is the PrimeTest.isPrime() method.

Note that stream trace does not reach beyond the terminal operation of a stream. This means that if there is further chaining, for example with Optional, it will not be visible from the Stream Trace dialog.