Java流懒惰与融合和短路 [英] Java streams lazy vs fusion vs short-circuiting

查看:181
本文介绍了Java流懒惰与融合和短路的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Java流API中形成一个关于懒惰评估应用的cocise和一致的理解。

I'm trying to form a cocise and conherent understanding of the application of lazy evaluation within the Java streams API.

这是我目前理解的:


  • 元素仅在需要时消耗,即流是惰性的,并且中间操作是惰性的,例如过滤器,只会在需要时进行过滤。

  • 中间操作可以融合在一起(如果它们是无状态的)。

  • 短路操作不需要处理整个流。

我想要做的就是将所有这些想法结合在一起并确保我不会歪曲任何东西。我发现它很棘手,因为每当我阅读有关Java流的任何文献时,它都会说它们很懒惰或利用惰性评估,然后非常可互换地开始谈论融合和短路等优化。

What I want to do is bring all these ideas together and ensure I'm not misrepresenting anything. I'm finding it tricky because whenever I read any literature on Java streams, it goes on to say they're lazy or utilise lazy evaluation, and then very much interchangeably starts talking about optimisations such as fusion and short-circuiting.

所以我说对以下内容是对的吗?

So would I be right in saying the following?


  • fusion是如何延迟评估已在流API中实现 - 即消耗元素,并且尽可能将操作融合在一起。我想如果融合不存在那么我们肯定会回到急切的评估中,因为替代方案只是处理每个中间操作的所有元素,然后再转移到下一个中​​间操作?

  • fusion is how lazy evaluation has been implemented in the stream API - i.e. an element is consumed, and operations are fused together wherever possible. I'm thinking that if fusion didn't exist then surely we'd be back to eager evaluation as the alternative would just be to process all elements for each intermediate operation before moving onto the next?

在没有融合或懒惰评估的情况下,短路是可能的,但是通过这两个原则的实现,在流的背景下是非常有帮助的吗?

short-circuiting is possible without fusion or lazy evaluation but is very much helped in the context of streams by these the implementation of these two principles?

我对此有任何进一步的见解和清晰度表示感谢。

I'd appreciate any further insight and clarity on this.

推荐答案

至于融合。让我们假设这是一个地图操作:

As for fusion. Let's imagine here's a map operation:

.map(x -> x.squash())

它是无状态的,它只是根据指定的算法转换任何输入(在我们的例子中压扁它们)。现在过滤操作:

It's stateless and it just transforms any input according to the specified algorithm (in our case squashes them). Now the filter operation:

.filter(x -> x.getColor() != YELLOW)

它也是无状态的,只是删除了一些元素(在我们的例子中是黄色的)。现在让我们进行终端操作:

It's also stateless and it just removes some elements (in our case yellow ones). Now let's have a terminal operation:

.forEach(System.out::println)

它只显示终端的输入元素。融合意味着所有中间无状态操作都与终端消费者合并为单一操作:

It just displays the input elements to the terminal. The fusion means that all intermediate stateless operations are merged with terminal consumer into single operation:

.map(x -> x.squash())
.filter(x -> x.getColor() != YELLOW)
.forEach(System.out::println)

整个管道融合为单个消费者,它直接连接到源。处理每个元素时,源分裂器只执行组合的使用者,流管道不拦截任何内容并且不执行任何额外的簿记。这是融合。 Fusion不依赖于短路。可以在没有融合的情况下实现流(执行一个操作,获取结果,执行下一个操作,在每个操作之后将控制权返回到流引擎)。也可以没有短路融合。

The whole pipeline is fused into single Consumer which is connected directly to the source. When every single element is processed, the source spliterator just executes the combined consumer, the stream pipeline does not intercept anything and does not perform any additional bookkeeping. That's fusion. Fusion does not depend on short-circuiting. It's possible to implement streams without fusion (execute one operation, take the result, execute the next operation, taking the control after each operation back to the stream engine). It's also possible to have fusion without short-circuiting.

这篇关于Java流懒惰与融合和短路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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