Preface
Since Java 8, all of us as programmers cannot do without the use of Stream-related functions, which is called a smooth writing (this feel~~). But there are always times when the results we want for stream operations do not match expectations, which requires us to debug step by step to locate the problem
Regular debugging
First look at the following code:
public static void main(String[] args) {
Object[] res = Stream.of(1,2,3,4,5,6,7,8).filter( i -> i%2 == 0).filter( i -> i>3).toArray();
System.out.println(Arrays.toString(res));
}
We can set breakpoints at the Stream operation and view the results step by step, like this:
We need a variety of single-step debugging, not very intuitive, we urgently need a list view, let us quickly view our Stream results
Visual debugging
Similarly, select the line breakpoint first, and enter the program Debug
Stream Trace
will pop up, and the entire Stream operation is in sight
You can also click the Flat Mode
button in the lower left corner to flatten the entire view
In actual business, we usually perform various Stream operations on collections. Let's take a more complicated example:
List<Optional<Customer>> customers = Arrays.asList(
Optional.of(new Customer("日拱一兵", 18)),
Optional.of(new Customer("卑微的小开发", 22)),
Optional.empty(),
Optional.of(new Customer("OOT", 21)),
Optional.empty(),
Optional.of(new Customer("温柔一刀", 23)),
Optional.empty()
);
long numberOf65PlusCustomers = customers
.stream()
.flatMap(c -> c
.map(Stream::of)
.orElseGet(Stream::empty))
.filter(c -> c.getAge() > 18)
.count();
System.out.println(numberOf65PlusCustomers);
Also follow the above operations to get a visual Stream Trace view, intuitively understand the entire Stream process, view object properties, etc.
to sum up
This simple function, after reading it, I believe it can be of great help to you in daily debugging. Next, I will introduce more advanced debugging skills that you have not paid attention to.
Ri Gong Yibing| Original
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。