gpt4 book ai didi

java - 为什么 takeWhile 是有状态的?

转载 作者:行者123 更新时间:2023-12-03 22:49:42 26 4
gpt4 key购买 nike

Javadoc指出

This is a short-circuiting stateful intermediate operation.


有状态的定义来自 Javadoc :

Stateful operations, such as distinct and sorted, may incorporate state from previously seen elements when processing new elements.Stateful operations may need to process the entire input beforeproducing a result. For example, one cannot produce any results fromsorting a stream until one has seen all elements of the stream. As aresult, under parallel computation, some pipelines containing statefulintermediate operations may require multiple passes on the data or mayneed to buffer significant data. Pipelines containing exclusivelystateless intermediate operations can be processed in a single pass,whether sequential or parallel, with minimal data buffering.


怎么样 default Stream<T> takeWhile​(Predicate<? super T> predicate) stateful?它不需要查看整个输入等...
几乎就像 filter但短路。

最佳答案

那么,takeWhile应该处理 Stream 的最长前缀满足给定的 Predicate .这意味着要想知道给定元素的 Stream应由 takeWhile 处理,您可能必须处理它之前的所有元素。
因此,您需要知道 Stream 的前面元素的处理状态。以便知道如何处理当前元素。
按顺序 Stream s 您不必保持状态,因为一旦到达与 Predicate 不匹配的第一个元素,你知道你已经完成了。
并行 Stream但是,这变得更加棘手。

关于java - 为什么 takeWhile 是有状态的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65213944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com