Stream(流)在JAVA已经不是一个新词了。很早以前咱们就接触过JAVA中的输入输出流(IO Stream),它是对数据输入输出操做的抽象封装。JAVA8中提出一个集合流的抽象工具(java.util.stream,简称Stream),用于集合内元素的计算,更确切的说是过滤和统计操做。java
Stream不是一种真实的数据源(不存在数据结构),因此咱们没有办法直接来建立它,Stream只能依赖其余数据源来转换成咱们的抽象操做。Stream自己是不存在,只是咱们抽象出来的一个抽象操做,通过各类操做以后,Stream还须要转换成真实的数据源。数组
常见建立方式以下:数据结构
parallelStream()app
stream()ide
lines()工具
其实最终都是依赖StreamSupport类来完成Stream建立的。this
To perform a computation, stream operations are composed into a stream pipeline. A stream pipeline consists of a source (which might be an array, a collection, a generator function, an I/O channel, etc), zero or more intermediate operations (which transform a stream into another stream, such as filter(Predicate)), and a terminal operation (which produces a result or side-effect, such as count() or forEach(Consumer)). Streams are lazy; computation on the source data is only performed when the terminal operation is initiated, and source elements are consumed only as needed.spa
Stream操做由零个或多个中间操做(intermediate operation)和一个结束操做(terminal operation)两部分组成。只有执行结束操做时,Stream定义的中间操做才会依次执行,这就是Stream的延迟特性。orm
Returns a stream consisting of the elements of this stream that match the given predicate.对象
返回一个符合条件的Stream。
Returns a stream consisting of the results of applying the given function to the elements of this stream.
返回由新元素组成的Stream。
返回int、long、double基本类型对应的Stream。
Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is closed after its contents have been placed into this stream. (If a mapped stream is null an empty stream is used, instead.)
简单的说,就是一个或多个流合并成一个新流。
返回对应的IntStream、LongStream、DoubleStream流。
返回去重的Stream。
返回一个排序的Stream。
主要用来查看流中元素的数据状态。
返回前n个元素数据组成的Stream。属于短路操做
返回第n个元素后面数据组成的Stream。
循环操做Stream中数据。
暗元素顺序执行循环操做。
返回流中元素对应的数组对象。
聚合操做,用来作统计。
聚合操做,封装目标数据。
聚合操做,最小值,最大值,总数量。
短路操做,有一个符合条件返回true。
全部数据都符合条件返回true。
全部数据都不符合条件返回true。
短路操做,获取第一个元素。
短路操做,获取任一元素。