在对于一个 Stream 进行屡次转换操做 (Intermediate 操做),每次都对 Stream 的每一个元素进行转换,并且是执行屡次,这样时间复杂度就是 N(转换次数)个 for 循环里把全部操做都作掉的总和吗?其实不是这样的,转换操做都是 lazy 的,多个转换操做只会在 Terminal 操做的时候融合起来,一次循环完成。咱们能够这样简单的理解,Stream 里有个操做函数的集合,每次转换操做就是把转换函数放入这个集合中,在 Terminal 操做的时候循环 Stream 对应的集合,而后对每一个元素执行全部的函数。java
还有一种操做被称为 short-circuiting。用以指:ide
当操做一个无限大的 Stream,而又但愿在有限时间内完成操做,则在管道内拥有一个 short-circuiting 操做是必要非充分条件。函数
int sum = widgets.stream() .filter(w -> w.getColor() == RED) .mapToInt(w -> w.getWeight()) .sum();
stream() 获取当前小物件的 source,filter 和 mapToInt 为 intermediate 操做,进行数据筛选和转换,最后一个 sum() 为 terminal 操做,对符合条件的所有小物件做重量求和。ui
map (mapToInt, flatMap 等)、 filter、 distinct、 sorted、 peek、 limit、 skip、 parallel、 sequential、 unorderedblog
forEach、 forEachOrdered、 toArray、 reduce、 collect、 min、 max、 count、 anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 iteratorip
anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 limitci