Java流是连续阶段的吗? [英] Are Java streams stages sequential?

查看:86
本文介绍了Java流是连续阶段的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对中间阶段顺序状态有疑问 - 从应用于所有的阶段的操作输入流(项目)是所有阶段/操作应用于每个流项吗?

I have a question on the intermediate stages sequential state - are the operations from a stage applied to all the input stream (items) or are all the stages / operations applied to each stream item?

我知道这个问题可能不容易理解,所以我举个例子。在以下流处理中:

I'm aware the question might not be easy to understand, so I'll give an example. On the following stream processing:

List<String> strings = Arrays.asList("Are Java streams intermediate stages sequential?".split(" "));
strings.stream()
           .filter(word -> word.length() > 4)
           .peek(word -> System.out.println("f: " + word))
           .map(word -> word.length())
           .peek(length -> System.out.println("m: " + length))
           .forEach(length -> System.out.println("-> " + length + "\n"));

我对此代码的期望是输出:

My expectation for this code is that it will output:

f: streams
f: intermediate
f: stages
f: sequential?

m: 7
m: 12
m: 6
m: 11

-> 7
-> 12
-> 6
-> 11

相反,输出为:

f: streams
m: 7
-> 7

f: intermediate
m: 12
-> 12

f: stages
m: 6
-> 6

f: sequential?
m: 11
-> 11

所有阶段的项目是否显示,原因是控制台输出?或者他们也是处理所有阶段,一次一个?

Are the items just displayed for all the stages, due to the console output? Or are they also processed for all the stages, one at a time?

如果问题不够明确,我可以进一步详细说明问题。

I can further detail the question, if it's not clear enough.

推荐答案

此行为启用代码的优化。如果每个中间操作都要在继续进行下一个中间操作之前处理流的所有元素,那么就不会有优化

This behaviour enables optimisation of the code. If each intermediate operation were to process all elements of a stream before proceeding to the next intermediate operation then there would be no chance of optimisation.

所以为了回答你的问题,每个元素一次一个地沿着流管道垂直移动(除了一些有状态稍后讨论的操作),因此在可能的情况下实现优化。

So to answer your question, each element moves along the stream pipeline vertically one at a time (except for some stateful operations discussed later), therefore enabling optimisation where possible.

根据您提供的示例,每个元素将沿着流管道垂直移动一个一,因为没有包含有状态操作。

Given the example you've provided, each element will move along the stream pipeline vertically one by one as there is no stateful operation included.

另一个例子,假设您正在寻找第一个字符串其长度大于 4 ,在提供结果之前处理所有元素是不必要且耗时的。

Another example, say you were looking for the first String whose length is greater than 4, processing all the elements prior to providing the result is unnecessary and time-consuming.

考虑这个简单的例子:

List<String> stringsList = Arrays.asList("1","12","123","1234","12345","123456","1234567");
int result = stringsList.stream()
                        .filter(s -> s.length() > 4)
                        .mapToInt(Integer::valueOf)
                        .findFirst().orElse(0);

上面的过滤器中间操作将找到长度大于 4 的所有元素并返回它们的新流,但是当我们找到第一个元素时,会发生什么长度大于 4 ,该元素进入 .mapToInt ,然后 findFirst 说我找到了第一个元素并且执行在那里停止。因此结果将是 12345

The filter intermediate operation above will not find all the elements whose length is greater than 4 and return a new stream of them but rather what happens is as soon as we find the first element whose length is greater than 4, that element goes through to the .mapToInt which then findFirst says "I've found the first element" and execution stops there. Therefore the result will be 12345.

请注意,当流管道中包含 sorted 之类的有状态中间操作时,特定的操作将遍历整个流。如果你考虑一下,这就完全有道理,因为你需要对元素进行排序,以确定哪些元素在排序顺序中排在第一位。

Note that when a stateful intermediate operation as such of sorted is included in a stream pipeline then that specific operation will traverse the entire stream. If you think about it, this makes complete sense as in order to sort elements you'll need to see all the elements to determine which elements come first in the sort order.

distinct 中间操作也是一个有状态操作,但是,正如@Holger所提到的那样,不像 sorted ,它没有要求遍历整个流,因为每个不同的元素都可以立即传递到管道中,并且可以满足短路条件。

The distinct intermediate operation is also a stateful operation, however, as @Holger has mentioned unlike sorted, it does not require traversing the entire stream as each distinct element can get passed down the pipeline immediately and may fulfil a short-circuiting condition.

无状态中间操作,例如 filter map 等不必遍历整个流,并且可以如上所述垂直地一次处理一个元素。

stateless intermediate operations such as filter , map etc do not have to traverse the entire stream and can freely process one element at a time vertically as mentioned above.

最后,但同样重要的是要注意,当终端操作是短路操作时,终端短路方法可以在遍历所有元素之前完成。基础流。

Lastly, but not least it's also important to note that, when the terminal operation is a short-circuiting operation the terminal-short-circuiting methods can finish before traversing all the elements of the underlying stream.

阅读: < a href =http://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/ =nofollow noreferrer> Java 8流教程

reading: Java 8 stream tutorial

这篇关于Java流是连续阶段的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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