How to build a stream example in Java 8

Stream first experience

Stream is an important feature of operation set in Java 8. Let's take a look at how stream is defined in Java:

Let's interpret the above sentence:

1. Stream is a collection of elements, which makes stream look like an iterator;

2. It can support sequential and parallel aggregation of the original stream.

There are many ways to create a stream. In addition to the most common collection creation, there are several other ways.

List to stream

List inherits from the collection interface, which provides the stream () method.

Array to stream

For arrays, arrays provides a stream () method.

Map to stream

Map is not a sequence, not a collection, and cannot be directly converted to stream () But entryset() is a set and can be converted to

Create stream directly

Stream also provides an API to directly generate a stream, which can be roughly understood as a list. Because the internal array is implemented.

Read the stream of the file

Those who have used linux will admire the pipeline characters on their command line, and a pipeline character can be processed continuously. Reading files in Java can also achieve similar functions.

Generating an infinite flow through a function

Stream provides iterate to generate an infinite sequence, an infinite sequence based on initial values. You can use lambda to set the generation rules of the sequence, such as adding 2.5 times each time

Another example is the Fibonacci sequence

Stream also provides another generate method to generate sequences. Receive a user specified generation sequence function intsupplier

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

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
分享
二维码
< <上一篇
下一篇>>