Stream operations are one of the highlights of Java 8! Although java.util.stream
is very powerful, there are still many developers who seldom use it in actual work. One of the reasons for the most complaints is that it is not easy to debug. This was true at the beginning, because streaming operations such as stream In DEBUG, it is a line of code. When we go directly to the next step, in fact, a lot of operations have passed at once, so 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
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 "Playing 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: https://www.didispace.com/idea -tips/
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:
A trace window for Stream operations will pop up:
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
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。