JDK8——IntStream

IntStream

A sequence of primitive int-valued elements supporting sequential and parallel aggregate operations. This is the int primitive specialization of Stream.

支持对一系列原始int-valued元素对顺序和并行聚合操作。这是Stream的int原始特化。

The following example illustrates an aggregate operation using Stream and IntStream, computing the sum of the weights of the red widgets:

以下示例说明了使用Stream和IntStream的聚合操作,计算红色小部件的权重总和:


     int sum = widgets.stream()
                      .filter(w -> w.getColor() == RED)
                      .mapToInt(w -> w.getWeight())
                      .sum();
 

See the class documentation for Stream and the package documentation for java.util.stream for additional specification of streams, stream operations, stream pipelines, and parallelism.

请参阅Stream的类文档和java.util.stream的包文档,以获取流,流操作,流管道和并行性的其他规范。

 

常用方法和应用场景

range(int startInclusive, int endExclusive)

Returns a sequential ordered IntStream from startInclusive (inclusive) to endExclusive (exclusive) by an incremental step of 1.

从startInclusive(包括)到endExclusive(不包括)的递增步长返回顺序排序的IntStream。

filter(IntPredicate predicate)

Returns a stream consisting of the elements of this stream that match the given predicate.

返回由与给定断言匹配的IntStream。

findFirst()

Returns an OptionalInt describing the first element of this stream, or an empty OptionalInt if the stream is empty.

返回描述此流的第一个元素的OptionalInt,如果流为空,则返回空的OptionalInt。

 

参考资料:JDK8官网

你可能感兴趣的:(Java基础)