Java is now 14, and it would be hard to avoid the “new” features of Java8. It’s like using text messaging in the era of wechat, so we’ve recently started streaming programming.

At the heart of streaming programming is the flow of operations, which can be generated by itself:

Stream.builder()

But much of it is generated from collections

new ArrayList<Activity>.stream()

Now that we have streams, we need to perform operations on streams. There are many operations on streams: map(),distinct(),sorted(),foreach(),collect()….. Many of these operations work as the name suggests, but some of them don’t, such as reduce () or map (). I won’t go into details about what these functions do, but others have explained it in detail, so you can look it up, but they operate on collections through convection operations.

Streaming programming operations are divided into two distinct operations (filter, limit, sort, map, distinct…) in order of execution. Reduce foreach Collect close allmatch anymatch… The operations that are subordinate to the intermediate operation are not executed sequentially, but wait until the final operation is executed, before the final operation is executed, which is why a friend of mine’s breakpoint doesn’t work on the intermediate operation function.

How do you determine whether an operation is an intermediate operation or a final operation? Intermediate operations can be linked together to form a pipeline. Intermediate operations are lazy loading, which is more favorable for lazy loading, short circuit, loop merge and other operations.

The final operation is basically to process the Stream after the intermediate operation, or aggregate it into a list or return Integer or void, and the final operation is final because it calls Runnble to close the Stream and all the data in the Stream is destroyed

At this point, Streams can be replaced by iterators, but if that were the case, there would be no need for Streams. In my opinion, the advantage of Streams over iterators is that they have Parallel Streams. Instead of having to manipulate threads ourselves, we reduce a lot of code work, and parallelStream should be more efficient than handling threads ourselves in most cases. However, there are a number of special cases where it is less efficient to use parallel streams, mostly because of data structure/ease of packing and unpacking/amount of data, etc.

zhuanlan.zhihu.com/p/21394743

More in-depth content this blog speaks well oh ↓

www.cnblogs.com/CarpenterLe…