中间操作和终端操作有什么区别? [英] What is the difference between intermediate and terminal operations?

查看:303
本文介绍了中间操作和终端操作有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我Stream的中间操作和终端操作有何区别?

can someone tell me What is the difference between intermediate and terminal operations for Stream?

Stream操作被组合到管道中以处理流.所有操作都可以是中间操作,也可以是终端..means?.

Stream operations are combined into pipelines to process streams. All operations are either intermediate or terminal ..means?.

推荐答案

Stream支持几种操作,这些操作分为intermediateterminal操作.

A Stream supports several operations and these operations are divided into intermediate and terminal operations.

此操作之间的区别在于,中间操作是惰性的,而终端操作则不是.当您在流上调用中间操作时,该操作不会立即执行.仅在对该流调用终端操作时才执行它.在某种程度上,一旦调用了终端操作,便会存储并调用一次中间操作.您可以链接多个中间操作,在调用终端操作之前,它们都不做任何事情.届时,您先前调用的所有中间操作将与终端操作一起被调用.

The distinction between this operations is that an intermediate operation is lazy while a terminal operation is not. When you invoke an intermediate operation on a stream, the operation is not executed immediately. It is executed only when a terminal operation is invoked on that stream. In a way, an intermediate operation is memorized and is recalled as soon as a terminal operation is invoked. You can chain multiple intermediate operations and none of them will do anything until you invoke a terminal operation. At that time, all of the intermediate operations that you invoked earlier will be invoked along with the terminal operation.

所有中间操作都返回Stream(可以链接),而终端操作则不返回.中间业务是:

All intermediate operations return Stream (can be chained), while terminal operations don't. Intermediate Operations are:

filter(Predicate<T>)
map(Function<T>)
flatmap(Function<T>)
sorted(Comparator<T>)
peek(Consumer<T>)
distinct()
limit(long n)
skip(long n)

终端操作会产生非流(无法链接)结果,例如原始值,集合或根本没有值.

Terminal operations produces a non-stream (cannot be chained) result such as primitive value, a collection or no value at all.

终端操作为:

forEach
forEachOrdered
toArray
reduce
collect
min
max
count
anyMatch
allMatch
noneMatch
findFirst    
findAny

最后5个是短路端子操作.

Last 5 are short-circuiting terminal operations.

这篇关于中间操作和终端操作有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆