且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在java流中遇到订单保存

更新时间:2023-11-30 13:42:46

来源的无序性质或通过无序()明确释放订单合同可能会影响所有后续管道阶段除非他们引入的订单只能通过排序操作发生。

The unordered nature of a source or the explicit releasing of the order contract via unordered() may affect all subsequent pipeline stages unless they introduce an order which can only happen with a sorted operation.

对于无状态中间操作,如过滤器 map ,无论如何都没有区别,但像这样的操作会跳过 limit distinct 可能会表现出不同的行为,具体取决于先前的流状态是已排序还是无序。 此答案显示了 distinct 如何受到影响的示例previous unordered()

For stateless intermediate operations like filter and map, there is no difference anyway, but operations like skip, limit and distinct may exhibit a different behavior depending on whether the previous stream state was ordered or unordered. This answer shows an example of how distinct can be affected by a previous unordered().

请注意,原则上已排序,在引入订单时,可能取决于前一阶段的有序状态,因为如果前一个流是无序的,它可能会使用不稳定的排序算法。

Note that in principle, sorted, while introducing an order, may depend on the ordered state of previous stage, as it may use an unstable sort algorithm if the previous stream was unordered.

这个答案提供了一种打印流特征的方法,并评估了由于追加另一个操作而改变它们的方式。

This answer provides a way to print the characteristics of a stream and evaluate how they change due to appending another operation.

当您对终端操作进行链接时,终端操作本身的无序性质或终端操作之前的最后一级的无序状态可能足以为终端操作选择算法不试图保留订单。

When you chain a terminal operation, both, an unordered nature of the terminal operation itself or an unordered state of the last stage before the terminal operation may be sufficient to select an algorithm for the terminal operation that doesn’t try to preserve the order.

原则上,终端操作的无序性质可用于影响之前的阶段,但由于无状态中间操作无论如何都不受影响,跳过限制 distinct 必须遵守先前订购的状态(如果存在),唯一可能受影响的操作是已排序如果后续操作不合适则会过时无论如何都要关心订单。

In principle, the unordered nature of the terminal operation could be used to affect previous stages, but since stateless intermediate operations are not affected anyway and skip, limit, distinct have to obey a previous ordered state, if present, the only operation that could be affected, is sorted that becomes obsolete if the subsequent operations don’t care about the order anyway.

在当前的实现中,自Java 8更新60以来,终端操作的无序性质不会影响前一阶段的行为。与以前的实现一样,这种更改是错误地影响跳过限制。由于链接 sort 以及无序的后续操作,因此没有机会忽略过时的排序步骤并不是一个问题。如果您想了解有关相关讨论的更多信息,请参阅此答案,包括评论。

In the current implementation, since Java 8 update 60, the unordered nature of a terminal operation does not affect the behavior of previous stages. This change was made, as in previous implemen­tations, it wrongly affected skip and limit. Loosing the opportunity to elide obsolete sorting steps was not considered a problem, as chaining sort with unordered subsequent operations, is a corner case. See this answer, including the comments, if you want to know more about the related discussion.

所以对于

list.stream() // List.stream() returns an ordered stream
    .unordered() // releases order contract
    .distinct() // for equal elements, it may pick an arbitrary one
    .sorted() // re-introduces an order
    .skip(1) // will skip the minimum element due to the order
    .forEach(System.out::println); // may print the remaining elements in arbitrary order

没有单一的有序或无序行为流管道。

there is not a single ordered or unordered behavior for the stream pipeline.

相反,使用

hashSet.stream() // HashSet.stream() has no order (unless being a LinkedHashSet)
    .filter(Objects::nonNull) // not affected by order
    .distinct() // may use unorderedness, but has no effect anyway, as already distinct
    .skip(1) // may skip an arbitrary element
    .forEachOrdered(System.out::println); // would respect order if there was one

整个管道无序运行,只会因为源是无序的,所以会尊重订单。使用有序的源,它将完全被排序。

the entire pipeline runs unordered, just because the source is unordered. With an ordered source, it would be entirely ordered.

所以的答案是评估整个流管道通过特征完成的ORDER特性甚至在执行开始之前,源,中间操作和终端操作是什么?是,是的,这是在开始实际处理之前完成的,通过为管道阶段选择适当的算法,当有选择时,但这个过程不一定会导致整个管道的单一特征。

So the answer to "is the evaluation of the entire stream pipeline ORDER characteristic done by going through the characteristics of source, intermediate operations and terminal operation even before the start of the execution?" is, yes, this is done right before starting the actual processing, by selecting the appropriate algorithm for the pipeline stages, when there is a choice, but this process does not necessarily result in a single characteristic for the entire pipeline.