IntelliJ IDEA
 
Get IntelliJ IDEA

Analyze Java Stream operations

Last modified: 23 October 2024

Java Streams may sometimes be difficult to debug. This happens because they encapsulate the iteration logic, and it might complicate tracing how particular values are processed. To help you debug Java Streams, IntelliJ IDEA lets you visualize what is going on in a Java Stream.

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

As the classes' names suggest, the 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.

To understand where these incorrect numbers are coming from, let's use the stream debugger feature.

The examination of the stream shows that the extra value is coming from the filter operation. The search of a bug is now narrowed down to the Predicate of filter, or more specifically, PrimeTest.isPrime() method.