java 中间操作和终端操作有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47688418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:45:08  来源:igfitidea点击:

What is the difference between intermediate and terminal operations?

javajava-8java-stream

提问by Amol Raje

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

有人能告诉我中间操作和终端操作有Stream什么区别吗?

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

Stream操作被组合成管道来处理流。所有操作都是中间或终端..意味着?。

回答by Alex Mamo

A Stream supports several operations and these operations are divided into intermediateand terminaloperations.

一个 Stream 支持多种操作,这些操作分为intermediateterminal操作。

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.

这些操作之间的区别在于中间操作是惰性的,而终端操作不是。当您对流调用中间操作时,该操作不会立即执行。它仅在对该流调用终端操作时执行。在某种程度上,中间操作会被记忆并在调用终端操作时立即调用。您可以链接多个中间操作,并且在您调用终端操作之前,它们都不会执行任何操作。届时,您之前调用的所有中间操作都会与终端操作一起调用。

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

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

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.

终端操作产生非流(不能链接)结果,例如原始值、集合或根本没有值。

Terminal Operations are:

终端操作是:

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

Last 5 are short-circuiting terminal operations.

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

回答by OneCricketeer

Terminal meaning ending the process, such as collecting a stream to a list, or aggregating a stream of values.

终止意味着结束该过程,例如将流收集到列表中,或聚合值流。

Intermediate as in transitioning to a new state, on the way to a terminal operation

过渡到新状态,在进行终端操作的过程中处于中间状态

回答by nxhoaf

As per javadoc:

根据javadoc

  • Intermediate operationwill transform a stream into another stream, such as filter(Predicate)
  • Terminal operationwill produce a result or side-effect, such as count()or forEach(Consumer)
  • 中间操作会将一个流转换成另一个流,比如filter(Predicate)
  • 终端操作会产生一个结果或副作用,例如count()forEach(Consumer)

Note that all intermediate operations will NOTbe executed without a terminal operation at the end. So the pattern will be:

请注意,如果最后没有终端操作,将不会执行所有中间操作。所以模式将是:

stream()
    .intemediateOperation1()
    .intemediateOperation2()
    ...
    .intemediateOperationN()
    .terminalOperation();

回答by Hubert Grzeskowiak

You probably noticed that when you define a stream, the methods aren't just called one by one on the whole stream, but rather they do something on each element from the stream. To be able to run these streams in parallel, for every element, there is basically a whole pipeline.

您可能已经注意到,当您定义一个流时,这些方法不仅会在整个流上被一个一个地调用,而且还会对流中的每个元素执行某些操作。为了能够并行运行这些流,对于每个元素,基本上都有一个完整的管道。

In order to create multiple pipelines, Java 8+ uses the builder pattern. With every intermediate step you add a filter or converter to the stack. In order to tell Java to generate pipelines from that stack of filters, you use a terminating step. That last step combines all the different pipelines. Usually it just returns the values in a defined format, e.g. a list, but it can also run a function once for each element, or reduce the results to a boolean or a number.

为了创建多个管道,Java 8+ 使用了构建器模式。在每个中间步骤中,您都会向堆栈中添加一个过滤器或转换器。为了告诉 Java 从该过滤器堆栈生成管道,您使用了一个终止步骤。最后一步结合了所有不同的管道。通常它只是以定义的格式返回值,例如列表,但它也可以为每个元素运行一次函数,或者将结果减少为布尔值或数字。