In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What is the principle of Java 8 Stream, I believe that many inexperienced people are helpless about this, this article summarizes the causes and solutions of the problem, through this article I hope you can solve this problem.
Foreword:
Stream provides a high-level abstraction for Java collection operations and expressions in an intuitive way similar to querying data from a database with SQL statements.
Stream API can greatly improve the productivity of Java programmers, allowing programmers to write efficient, clean, concise code.
1. Composition and characteristics of Stream
Stream is a queue of elements from a data source and supports aggregation operations:
Elements are objects of a specific type that form a queue. Stream in Java does not store and manage elements like collections, but rather computes on demand
The source of the data stream can be Collection, Array, I/O channel, generator, etc.
Aggregate operations similar to SQL statements, such as filter, map, reduce, find, match, sorted, etc.
Unlike previous Collection operations, Stream operations have two basic characteristics:
Pipelining: Intermediate operations return the flow object itself. Such multiple operations can be concatenated into a pipeline, as in the flow style. Doing so optimizes operations such as laziness evaluation and short-circuiting
Internal iteration: Previously, traversal of a collection was done explicitly outside the collection via Iterator or For-Each, which is called external iteration. Stream provides a way to iterate internally, via Visitor patterns.
Unlike iterators, Stream can be parallelized, while iterators can only be command-and-serial. As the name implies, when traversing in serial fashion, each item is read before the next item is read. When traversing in parallel, the data is divided into multiple segments, each of which is processed in a different thread, and the results are output together.
Stream's parallel operations rely on the Fork/Join framework (JSR 166y) introduced in Java 7 to split tasks and speed up processing.
Java's parallel API evolved as follows:
"
java.lang.Thread in 1.0-1.4
5.0 java.util.concurrent in
6.0 Phasers and others
7.0 Fork/Join framework in
8.0 Lambda in
"
Stream has parallel processing capabilities, and the process of processing will be divided and ruled, that is, a large task will be divided into multiple small tasks, which means that each task is an operation:
List numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);numbers.parallelStream() .forEach(out::println);
You can see that a simple line of code helps us achieve the function of outputting elements in parallel, but since the order of parallel execution is uncontrollable, the result of each execution may not be the same.
If it has to be the same, you can use the forEachOrdered method to perform the termination operation:
List numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);numbers.parallelStream() .forEachOrdered(out::println);
The question here is, if the results need to be ordered, does that go against our original intention of parallel execution? Yes, in this scenario, there is obviously no need to use parallel stream, just execute it directly with serial stream, otherwise the performance may be worse, because in the end, all parallel results are forced to sort.
OK, let's first introduce the relevant knowledge of Stream interface.
2. BaseStream interface
The parent interface of Stream is BaseStream, which is the top-level interface of all stream implementations, defined as follows:
public interface BaseStream extends AutoCloseable { Iterator iterator(); Spliterator spliterator(); boolean isParallel(); S sequential(); S parallel(); S unordered(); S onClose(Runnable closeHandler); void close();}
where T is the type of the element in the stream, S is an implementation class of BaseStream whose elements are also T and S is itself:
S extends BaseStream
Are you feeling a little dizzy?
In fact, it is easy to understand, we look at the use of S in the interface to know: such as sequential(), parallel() these two methods, they both return S instances, that is, they support serial or parallel operations on the current stream, and return the "changed" stream object.
"
If it is parallel, it must involve splitting the current stream, that is, splitting a stream into multiple child streams, and the child streams must be of the same type as the parent stream. The sub-stream can continue to be split into sub-streams and continue to be split…
"
That is to say, S here is an implementation class of BaseStream, which is also a stream, such as Stream, IntStream, LongStream, etc.
3, Stream interface
Let's take a look at Stream's interface declaration:
public interface Stream extends BaseStream
With reference to the explanation above, it is not difficult to understand that Stream can continue to split into Stream, which we can prove by some of its methods:
Stream filter(Predicate
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.