This is the second day of my participation in the August More text Challenge. For details, see:August is more challenging
Java handles collections in ways other than for and iterators.
Until the release of java8.
1. Why use Stream
Data processing is becoming more important and more complex!
- There’s more and more data to process
- Control the response time
- Complex algorithm
Therefore, data processing requires high-level primitives:
- The ability to access data wherever it is.
- Provides functions such as mapping/filtering/reducing.
- You may need parallel implementations.
Here’s the official quote: Collections and streams, while bearing some superficial similarities, have different goals. Collections are primarily concerned with the efficient management of, and access to, their elements. By contrast, streams do not provide a means to directly access or manipulate their elements, and are instead concerned with declaratively describing their source and the computational operations which will be performed in aggregate on that source. However, if the provided stream operations do not offer the desired functionality, the BaseStream.iterator() and BaseStream.spliterator() operations can be used to perform a controlled traversal.
Despite some superficial similarities, collections and flows have different goals. Collections are primarily concerned with efficient management and access to their elements. Instead, flows do not provide a way to access or manipulate their elements directly, but instead focus on declaratively describing their source and the computed operations that will be aggregated on that source. However, if the provided stream operation does not provide the required functionality, you can use the basestream.iterator () and basestream.spliterator () operations.
2. What,什么是java8 Stream
- No storage. A Stream is not a data structure that stores elements.
- Functionality. The operation of convection produces results, but does not modify its source.
- For example, filtering
Stream
The A that you get from the set will generate oneStream
New elements without filtering elements, rather than removing elements from the source collection.
- For example, filtering
- Laziness seeks. Many stream operations, such as filtering, mapping, or deduplication, can be implemented lazily, exposing optimization opportunities.
- Perform intermediate operations, such as
filter()
Instead of actually performing any filtering, a new stream is created that, over time, contains elements of the original stream that match the given predicate. The pipeline source traversal does not begin until the terminal operation of the pipeline has been executed.
- Perform intermediate operations, such as
- It could be unbounded. Although collections have a finite size, streams do not.
- Such as
limit(n)
Or such short circuit operationfindFirst()
It allows the computation of an infinite stream to be completed in finite time. - For operations such as “Find the first string longer than 1000 characters,” just check enough strings to find the one with the desired characteristics, rather than checking all available strings in the source. (This behavior becomes even more important when the input stream is infinite rather than just large.)
- Such as
- Consumables. Elements of a flow are accessed only once during the lifecycle of the flow. With the same
Iterator
, a new stream must be generated to revisit the original source
For Java, it’s a whole new concept: functional. But in other languages, functions are not new.