Stream operations and pipelines - intermediate operation

2021-06-24 11:14:51 浏览数 (1)

Classes to support functional-style operations on streams of elements, such as map-reduce transformations on collections. For example:

代码语言:javascript复制
 int sum = widgets.stream()
                  .filter(b -> b.getColor() == RED)
                  .mapToInt(b -> b.getWeight())
                  .sum();

Here we use widgets, a Collection, as a source for a stream, and then perform a filter-map-reduce on the stream to obtain the sum of the weights of the red widgets. (Summation is an example of a reduction operation.)

比如使用在一个集合上使用map-reduce操作,如上所示,我们先把widget类型的集合作为数据源,然后执行filter、map、以及sum。最后实现了,统计红色部件的总重量。 The key abstraction introduced in this package is stream. Streams differ from collections in several ways: 如下主要讲解stream的一些特性,stream和集合主要有以下不同的几点:

  • No storage. A stream is not a data structure that stores elements; instead, it conveys elements from a source such as a da

0 人点赞