Java8 Stream

summary

A new abstraction called stream has been added to the Java 8 API to process data in a declarative manner.

Stream and Java InputStream and OutputStream in io package are completely different concepts. Stream in Java 8 enhances the function of collection objects, focusing on various very convenient and efficient aggregation operations or mass data operations on collection objects.

Stream is like an advanced version iterator. As long as users give what operations they need to perform on the elements contained in it, stream will implicitly traverse internally and make corresponding data conversion.

Stream is different from iterator. Stream can operate in parallel, that is, the data is divided into multiple segments, each of which is processed in different threads, and then the results are output together.

Let's take a look at the following example:

Through this example, we can see the simplicity of the code after the operation of introducing flow.

use

The steps of using stream are as follows:

1. Create a stream

Collection class collection:

Array:

Arrays. Stream (ARR): get the serial stream of array through static method

Arrays. Stream (arr, int, int): returns the serial stream of the array, specifying the array range

Digital class:

Intstream: an integer stream interface with the following static methods:

Doublestream: decimal stream interface, with the following static methods:

Longstream: long shaping stream interface, with the following static methods:

Random: random number. There are the following methods to generate a stream (not a static method)

Wait, please try it yourself

2. Flow operation

Stream: the interface of the stream, which defines the following methods for streaming intermediate operations:

Of course, there are other operations

3. Get results

Stream: the interface of the stream, which defines the following methods of stream result operation:

Of course, there are other operations. For more operations, please try it yourself

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>