Java 8 Stream Usage

Source: Internet
Author: User
Tags stream api java 8 stream

First, what is stream?

Stream is not a collection element, it is not a data structure, it does not hold it, it is about algorithms and computations, it is more like an advanced version of Iterator. The original version of the Iterator, the user can only explicitly one to traverse the element and perform certain operations on it, the advanced version of the Stream, the user just give the elements they need to do what to do, such as "filter out the length of a string greater than 10", "get the first letter of each string", etc. Stream is implicitly traversed internally to make the appropriate data transformations. Stream is like an iterator (Iterator), one-way, non-reciprocating, the data can only be traversed once, traversed once and then exhausted, like flowing water from the front flow, gone forever. In contrast to iterators, streams can parallelize operations, and iterators can only be ordered and serialized. As the name implies, when you use a serial way to traverse, each item reads and then reads the next item. When using parallel traversal, the data is divided into segments, each of which is processed in a different thread, and the results are then output together.

Second,stream in Java 8

Stream, as a highlight of Java 8, is a completely different concept from the InputStream and OutputStream in the java.io package. It is also different from StAX's stream for XML parsing and not the stream that Amazon Kinesis real-time processing of big data. Stream in Java 8 is an enhancement to the functionality of a collection (Collection) object, focusing on a variety of very convenient, efficient aggregation operations (aggregate operation) for collection objects, or large-volume data operations (bulk data Operation). the Stream API greatly improves programming efficiency and program readability by using the same new LAMBDA expression. It also provides both serial and parallel modes for aggregation, and the concurrency pattern leverages the benefits of multi-core processors to split tasks and accelerate processing using fork/join parallel methods. Writing parallel code is often difficult and error-prone, but using the Stream API makes it easy to write high-performance concurrent programs without having to write a single line of multithreaded code. So, the first java.util.stream in Java 8 is the product of a functional language + multi-core ERA synthesis effect.

Iii. composition of the stream

When we use a stream, it usually consists of 3 steps: ① get a data source (source) →② Data Transformation →③ perform operation get desired result

Each conversion of the original stream object does not change, returning a new stream object (which can have multiple conversions), which allows the operation to be arranged like a chain, into a pipe,

  

There are two types of operations for a stream:

    • Intermediate: A stream can be followed by 0 or more intermediate operations. The main purpose is to open the stream, make a certain degree of data mapping/filtering, and then return a new stream to be used by the next operation. This type of operation is lazy (lazy), which means that only calls to such a method do not actually begin the traversal of the stream.
    • Terminal: A stream can have only one Terminal operation, and when the operation is executed, the stream is used "light" and cannot be manipulated again. So this must be the last operation of the stream. Terminal the execution of the operation, the traversal of the stream is actually started, and a result is generated, or a side effect.

Java 8 Stream Usage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.