IntelliJ IDEA 2019.3 Help

Analyze Java Stream operations

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.

public class PrimeFinder { public static void main(String[] args) { IntStream.iterate(1, n -> n + 1) .skip(Integer.parseInt(args[0])) .limit(Integer.parseInt(args[1])) .filter(PrimeTest::isPrime) .forEach(System.out::println); } }

As its name suggests, the PrimeFinder app finds prime numbers. It takes the number to start with as the first argument, and the number of candidates to check as the second argument. The checking logic is handled by a Java Stream.

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

79 83 89 91 97

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.

  1. Suspend the program at a line where the stream is used. You can use any stream operation for this including terminal ones.

    The program is suspended at the breakpoint that is             set on the creation of the Stream
  2. Click the Trace Current Stream Chain button Trace Current Stream Chain button.

  3. Use the Stream Trace dialog to analyze the operations inside the stream. The tabs in the top part let you switch between particular operations and see how the values are transformed with each operation.

    The Stream Trace dialog

    If you want to get a bird's eye view of the entire stream, click Flat Mode.

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.

Last modified: 20 March 2020