How to gracefully debug Java Stream operations in IntelliJ IDEA

Stream operations are one of the highlights of Java 8! Although java.util.streamit is very powerful, there are still many developers who rarely use it in actual work. One of the most common reasons for complaining is that it is not easy to debug. This was indeed the case at the beginning, because streaming operations such as stream are in DEBUG. One line When we go directly to the next step in the code, in fact, a lot of operations have passed at once, so that it is difficult for us to judge which line is the problem. However, now, with the support of powerful IDEA plug-ins, stream debugging is actually not that difficult. Let's learn how to debug stream operations in IDEA.

Plugin: Java Stream Debugger

image.png

If the IDEA version you are using is relatively new, this plug-in is already included, so you don't need to install it. If you haven't installed it yet, install it manually, and then continue with the following operations.

This article is included in the "Fun IDEA Column" that I am serializing. This series should be written in the form of e-books. If you want to immerse yourself in reading and learning, you can visit the Web version: www.didispace.com/idea-tips/

file

Debug Stream operations

Video demo: click here to view

First look at the following code:

public class StreamTest {

    @Test
    void test() {
        List<String> list = List.of("blog.didispace.com", "spring4all.com", "openwrite.cn", "www.didispace.com");

        List<String> result = list.stream()
                .filter(e -> e.contains("didispace.com"))
                .filter(e -> e.length() > 17)
                .toList();

        System.out.println(result);
    }

}
复制代码

The logic of this code is to filter the elements in the list collection through stream. Since there are two filters, when a problem occurs, you may not know which filter has the problem.

With the powerful IDEA, when we encounter a stream, we only need to click the button in the following figure:

image.png

A trace window for Stream operations will pop up:

image.png

The label in this window is each step of the stream operation. We can judge whether the filter here is executed correctly by clicking the label to view the results before and after each step is executed.

Does it feel a lot easier at once? Well, today's sharing is here. If you haven't used this debugging function, open IDEA and try it! If you encounter difficulties in the learning process? You can join our high-quality technical exchange group , participate in exchanges and discussions, and learn and progress better!

Welcome to my public account: Programmer DD. Learn about cutting-edge industry news for the first time, share in-depth technical dry goods, and obtain high-quality learning resources

Guess you like

Origin juejin.im/post/7100835528387330061