Skip to content

Commit 88e2c2a

Browse files
committed
Published with https://stackedit.io/
1 parent e77976f commit 88e2c2a

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

29.Java8之Stream.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Stream类和集合类都用于操作集合,但是Stream跟集合类存在很
3838

3939
* Stream并不是一个数据结构存储元素;它只是通过管道的方式将数据从数组、函数生成器、I/O通道中进行传输和操作;
4040
* 对Stream的操作并不会对产生Stream的源数据进行修改;
41-
* 懒惰取值(Laziness-seeking),很多Stream操作,例如filter、map、duplicate等并不会实时产生结果集。Stream的操作区分为两种:**中间结果操作****最终结果操作****中间结果操作**永远是懒惰取值的。这样做主要是为了性能优化。
41+
* 懒惰取值(Laziness-seeking),很多Stream操作,例如filter、map、duplicate等并不会实时产生结果集。Stream的操作区分为两种:**中间操作****最终操作****中间操作**永远是懒惰取值的。这样做主要是为了性能优化。
4242
* Stream可能是无边界的。虽然集合是有大小限制,但Stream是不受大小限制的。短路操作,例如`limit(n)``findFirst()`可以从无边界的Stream中获取定长的数据。
4343
* 可消耗的。Stream的元素的在整个生命周期中只能被访问一次。类似于Iterator,如果想要重新访问Stream中的元素则需要重新生成Stream。
4444

@@ -54,5 +54,22 @@ Stream可以通过多种方式获取,例如:
5454

5555
## 流操作(Stream Operations)和管道(Pipelines)
5656

57+
流操作被划分为**中间操作****最终操作**,并且组合形成管道流。管道流由如下组合而成:数据源(例如集合,数组,函数生成器或I/O通道);紧跟在数据源后面的是零或多个中间操作(例如Stream.filter或Stream.map);最终操作紧跟在中间操作后面(例如Stream.forEach或Stream.reduce等)。
58+
Stream operations are divided into intermediate and terminal operations, and are combined to form stream pipelines. A stream pipeline consists of a source (such as a Collection, an array, a generator function, or an I/O channel); followed by zero or more intermediate operations such as Stream.filter or Stream.map; and a terminal operation such as Stream.forEach or Stream.reduce.
59+
60+
Stream operations are divided into intermediate and terminal operations, and are combined to form stream pipelines. A stream pipeline consists of a source (such as a Collection, an array, a generator function, or an I/O channel); followed by zero or more intermediate operations such as Stream.filter or Stream.map; and a terminal operation such as Stream.forEach or Stream.reduce.
61+
62+
Intermediate operations return a new stream. They are always lazy; executing an intermediate operation such as filter() does not actually perform any filtering, but instead creates a new stream that, when traversed, contains the elements of the initial stream that match the given predicate. Traversal of the pipeline source does not begin until the terminal operation of the pipeline is executed.
63+
64+
Terminal operations, such as Stream.forEach or IntStream.sum, may traverse the stream to produce a result or a side-effect. After the terminal operation is performed, the stream pipeline is considered consumed, and can no longer be used; if you need to traverse the same data source again, you must return to the data source to get a new stream. In almost all cases, terminal operations are eager, completing their traversal of the data source and processing of the pipeline before returning. Only the terminal operations iterator() and spliterator() are not; these are provided as an "escape hatch" to enable arbitrary client-controlled pipeline traversals in the event that the existing operations are not sufficient to the task.
65+
66+
Processing streams lazily allows for significant efficiencies; in a pipeline such as the filter-map-sum example above, filtering, mapping, and summing can be fused into a single pass on the data, with minimal intermediate state. Laziness also allows avoiding examining all the data when it is not necessary; for operations such as "find the first string longer than 1000 characters", it is only necessary to examine just enough strings to find one that has the desired characteristics without examining all of the strings available from the source. (This behavior becomes even more important when the input stream is infinite and not merely large.)
67+
68+
Intermediate operations are further divided into stateless and stateful operations. Stateless operations, such as filter and map, retain no state from previously seen element when processing a new element -- each element can be processed independently of operations on other elements. Stateful operations, such as distinct and sorted, may incorporate state from previously seen elements when processing new elements.
69+
70+
Stateful operations may need to process the entire input before producing a result. For example, one cannot produce any results from sorting a stream until one has seen all elements of the stream. As a result, under parallel computation, some pipelines containing stateful intermediate operations may require multiple passes on the data or may need to buffer significant data. Pipelines containing exclusively stateless intermediate operations can be processed in a single pass, whether sequential or parallel, with minimal data buffering.
71+
72+
Further, some operations are deemed short-circuiting operations. An intermediate operation is short-circuiting if, when presented with infinite input, it may produce a finite stream as a result. A terminal operation is short-circuiting if, when presented with infinite input, it may terminate in finite time. Having a short-circuiting operation in the pipeline is a necessary, but not sufficient, condition for the processing of an infinite stream to terminate normally in finite time.
73+
5774
## 参考资料
5875
* 翻译自:http://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html

0 commit comments

Comments
 (0)