Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of Stream in Java8?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what is the principle of Stream in Java8". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the principle of Stream in Java8"?

Composition and characteristics of Stream

Stream (stream) is a queue of elements from a data source and supports aggregation operations:

Elements are specific types of objects that form a queue. Stream in Java does not store and manage elements like collections, but is calculated on demand

The source of the data source stream can be set Collection, array Array, I _ channel, generator 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 stream object itself. Such multiple operations can be concatenated into a single pipe, like streaming style (fluent style). Doing so optimizes operations, such as delayed execution (laziness evaluation) and short circuit (short-circuiting).

Internal iteration: in the past, the collection was traversed through Iterator or For-Each, explicitly iterating outside the collection, which is called external iteration. Stream provides a way to iterate internally through the Visitor pattern (Visitor).

Unlike iterators, Stream can parallelize operations, iterators can only operate in command and serialization. As the name implies, when traversing in serial mode, each item is read and then the next item is read. When traversing in parallel, the data is divided into segments, each of which is processed in a different thread and then outputs the results together.

The parallel operation of Stream relies on the Fork/Join framework (JSR166y) introduced in Java7 to split tasks and speed up the process. The evolution of parallel API for Java is basically as follows:

Java.lang.Thread in 1.0-1.4

Java.util.concurrent in 5. 0

Phasers in 6.0, etc.

Fork/Join framework in 7. 0

Lambda in 8.0

Stream has parallel processing capabilities, and the process of processing is divided and conquered, that is, a large task is 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 elements in the parallel output set, but the result of each execution is not necessarily the same because the order of parallel execution is uncontrollable.

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 () .forEachOrthood (out::println)

Here is a question: if the results need to be orderly, is it contrary to the original intention of our parallel execution? Yes, it is obvious that there is no need to use parallel flow in this scenario, it can be executed directly with serial flow, otherwise the performance may be even worse, because in the end, all the parallel results are forcibly sorted.

OK, let's first introduce the relevant knowledge of the Stream interface.

BaseStream interface

The parent interface of Stream is BaseStream, which is the top-level interface for all flow implementations and is 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 element in the stream, S is an implementation class of BaseStream, and the element in it is also T and S is also itself:

S extends BaseStream

Are you a little dizzy?

In fact, it is easy to understand. Let's take a look at the use of S in the interface: for example, the two methods sequential () and parallel () 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 substreams, which must be consistent with the type of the parent stream. The sub-stream can continue to split the molecular flow, always split …

In other words, S here is an implementation class of BaseStream, and it is also a stream, such as Stream, IntStream, LongStream, and so on.

Stream interface

Let's take a look at the interface declaration of Stream:

Public interface Stream extends BaseStream

With reference to the above explanation, it is not difficult to understand here: that is, Stream can continue to be split into Stream, and we can prove it in some ways:

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report