Java Stream programming has brought great inconvenience to debugging. Idea has introduced the StreamTrace function, which can see the relationship and results of each step of operation in detail, which is very convenient for debugging.

First met StreamTrace

Simply convert the string to its number of characters and set a breakpoint to enable debug mode.

As shown in the figure above, you can see the number of elements in each step, the result of the operation, and the corresponding level before and after the element conversion. It is very clear. You can also view the contents of specific objects.

Using StreamTrace

StreamTrace can only be used in Debug mode. After setting a breakpoint on the Stream code, start Debug and click the Stream button, as shown in the figure.

When clicked, the default Split mode is displayed.

You can switch to FlatMode by clicking the lower-left button, or you can switch back again.

Practical demonstration

Here is a demonstration of a stream with length less than 5 characters and a filter

@Test
    public void TestTrace(a) {
        Stream.of("beijing"."tianjin"."shanghai"."wuhan")
                .map(String::length)
                .filter(e->e>5)
                .collect(Collectors.toList());
    }
Copy the code