如何确保java8流中的处理顺序? [英] How to ensure order of processing in java8 streams?

查看:238
本文介绍了如何确保java8流中的处理顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想处理 XML java对象中的列表。我必须确保按照收到的顺序处理所有元素。



因此,我应该在每个<$上调用 sequential c $ c> stream 我用的是?
list.stream()。sequential()。filter()。forEach()



或只要我不使用并行性就足以只使用流吗?
list.stream()。filter()。forEach()

解决方案

你问错了问题。您询问顺序并行,而您想要按顺序处理,所以你必须询问订购。如果您有 ordered 流并执行保证维护顺序的操作,则无论是并行还是顺序处理流都无关紧要;实现将维护订单。



有序属性不同于并行和顺序。例如。如果在 HashSet 上调用 stream(),则在调用 stream时,流将无序( )列表上的 返回有序流。请注意,您可以致电 unordered() 释放订购合同并可能提高性能。一旦流没有排序,就无法重新建立排序。 (将无序流转换为有序流的唯一方法是调用已排序,但是,生成的订单不一定是原始订单。)



另请参阅订购部分 rel =noreferrer> java.util.stream 包文档。



为了确保在整个流操作中维护订购,您必须研究流的源文档,所有中间操作和终端操作,以确定它们是否维护订单(或者源是否具有排序)。 / p>

这可能非常微妙,例如 Stream.iterate(T,UnaryOperator) 创建一个有序流,而 Stream.generate(供应商) 创建无序流。请注意,您在问题中也犯了一个常见错误: forEach 维持订购。你必须使用 forEachOrdered 如果您想以保证的顺序处理流的元素。



因此,如果你问题中的列表确实是 java.util.List ,那么它的流()方法将返回订购的流,过滤器将不会更改排序。因此,如果您调用 list.stream()。filter()。forEachOrdered(),将按顺序依次处理所有元素,而对于列表。 parallelStream()。filter()。forEachOrdered()元素可以并行处理(例如通过过滤器),但仍然会按顺序调用终端动作(这显然会降低并行的好处)执行)。



例如,如果你使用像

这样的操作

 列表与LT; ...> 。结果= inputList.parallelStream()地图(...).filter(...).collect(Collectors.toList()); 

整个操作可能会受益于并行执行,但结果列表将始终以正确的顺序,无论是否使用并行或顺序流。


I want to process lists inside an XML java object. I have to ensure processing all elements in order I received them.

Should I therefore call sequential on each stream I use? list.stream().sequential().filter().forEach()

Or it it sufficient to just use the stream as long as I don't use parallelism? list.stream().filter().forEach()

解决方案

You are asking the wrong question. You are asking about sequential vs. parallel whereas you want to process items in order, so you have to ask about ordering. If you have an ordered stream and perform operations which guarantee to maintain the order, it doesn’t matter whether the stream is processed in parallel or sequential; the implementation will maintain the order.

The ordered property is distinct from parallel vs sequential. E.g. if you call stream() on a HashSet the stream will be unordered while calling stream() on a List returns an ordered stream. Note that you can call unordered() to release the ordering contract and potentially increase performance. Once the stream has no ordering there is no way to reestablish the ordering. (The only way to turn an unordered stream into an ordered is to call sorted, however, the resulting order is not necessarily the original order).

See also the "Ordering" section of the java.util.stream package documentation.

In order to ensure maintenance of ordering throughout an entire stream operation, you have to study the documentation of the stream’s source, all intermediate operations and the terminal operation for whether they maintain the order or not (or whether the source has an ordering in the first place).

This can be very subtle, e.g. Stream.iterate(T,UnaryOperator) creates an ordered stream while Stream.generate(Supplier) creates an unordered stream. Note that you also made a common mistake in your question as forEach does not maintain the ordering. You have to use forEachOrdered if you want to process the stream’s elements in a guaranteed order.

So if your list in your question is indeed a java.util.List, its stream() method will return an ordered stream and filter will not change the ordering. So if you call list.stream().filter() .forEachOrdered(), all elements will be processed sequentially in order, whereas for list.parallelStream().filter().forEachOrdered() the elements might be processed in parallel (e.g. by the filter) but the terminal action will still be called in order (which obviously will reduce the benefit of parallel execution).

If you, for example, use an operation like

List<…> result=inputList.parallelStream().map(…).filter(…).collect(Collectors.toList());

the entire operation might benefit from parallel execution but the resulting list will always be in the right order, regardless of whether you use a parallel or sequential stream.

这篇关于如何确保java8流中的处理顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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