JDK8——IntStream
Posted 小豆芽2333
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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官网
以上是关于JDK8——IntStream的主要内容,如果未能解决你的问题,请参考以下文章