Skip to content

Commit 2c499f5

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

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

29.Java8之Stream.md

Lines changed: 7 additions & 6 deletions
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,14 +54,13 @@ 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.
57+
流操作被划分为**中间操作****终点操作**,并且组合形成管道流。管道流由如下组合而成:数据源(例如集合,数组,函数生成器或I/O通道);紧跟在数据源后面的是零或多个中间操作(例如Stream.filter或Stream.map);最终操作紧跟在中间操作后面(例如Stream.forEach或Stream.reduce等)。
5958

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.
59+
中间操作返回的结果是流。中间操作永远是懒惰取值的;例如Stream.filter操作实际上并没有立即执行任何过滤,取而代之的是产生一个新的流,当进行传输时就会根据**过滤谓词**对原始的数据进行过滤。只有当**终点操作**被执行时,管道的遍历才会被执行(真正执行中间操作)。
6160

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.
61+
终点操作,例如Stream.forEach或IntStream.sum,会对流进行遍历并产一个结果或者副作用。当终点操作并执行完后,管道流就被消耗掉,无法再进行利用;如果需要对同样的数据进行遍历,则必须使用原始数据产生新的流。在多数情况下,终点操作被认为是饥饿的,一旦被调用,则会对数据源进行彻底的遍历直到结果被返回。最终操作中,只有`iterator()``spliterator()`不是饥饿的,他们被认为是**"逃生的窗口"**,可以让客户端(调用者)自由控制管道的遍历,在很多业务逻辑中,这种方式都是非常高效且常见的。
6362

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.
63+
流的懒惰取值模式是非常高效的。例如在`filter-map-sum`管道操作中,filter、map和sum可以融合为一次原始数据遍历。同时,这种懒惰取值的方式,可以避免对所有的数据进行遍历,例如需求:取数组中值大于100的前十个数值。如果对整个数组都进行遍历将是毫无必要的,而且非常低效。而流的懒惰取值可避免这种低效做法。例如:`list.filter(x -> x > 100).limit(100)`
6564

6665
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.)
6766

@@ -71,5 +70,7 @@ Stateful operations may need to process the entire input before producing a resu
7170

7271
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.
7372

73+
## 自定义实现
74+
7475
## 参考资料
7576
* 翻译自:http://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html

0 commit comments

Comments
 (0)