Stream operation is a highlight of Java 8! Although java.util.stream
is very powerful, there are still many developers who rarely use it in actual work. One of the most complained reasons is that it is difficult to debug. This was indeed the case at the beginning, because stream is like this When the streaming operation is in DEBUG, it is just one line of code. When going directly to the next step, many operations are actually passed at once, so it is difficult for us to judge which line in it is the problem.
#If the IDEA version you are using is relatively new, this plug-in already comes with it, so you don’t need to install it. . If it has not been installed yet, install it manually and 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 immersive reading and learning, you can visit the Web version: https://www .didispace.com/idea-tips/
Video demonstration: Click here to view
First look at the following This 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 it is from. problem.
With the help of the powerful IDEA, when we encounter a stream, we only need to click the button in the picture below:
A Stream operation will pop up. The tracking window:
The label in this window is each step of this stream operation. We can judge by clicking the label to view the results before and after each step is executed. Is the filter here executed correctly?
The above is the detailed content of How to debug Java Stream operations in IntelliJ IDEA. For more information, please follow other related articles on the PHP Chinese website!