[英]How to preserve order of the elements in streams
I want to preserve the order of array elements using below spliterator() method. 我想使用以下spliterator()方法保留数组元素的顺序。
However, when I run this program it is producing 4 2 1 3
as output. 但是,当我运行此程序时,它将产生
4 2 1 3
作为输出。
public class Test1 {
public static void main(String[] args) {
m1().anyMatch(e -> {
System.out.println(e);
return false;
});
}
private static LongStream m1() {
return StreamSupport.longStream(Arrays.spliterator(new long[] { 1, 2, 3, 4 }), true);
}
}
Kindly check and let me know if this thing is possible or not. 请检查并让我知道这件事是否可行。
You are requesting a parallel stream, hence, you can't get a defined processing order , as parallel processing and ordered processing are a general contradiction. 您正在请求并行流,因此,您无法获得已定义的处理顺序 ,因为并行处理和有序处理是一个普遍的矛盾。
If you want the elements to be processed in order, you have to do both, request a sequential stream and use a terminal operation that depends on the order: 如果要按顺序处理元素,则必须同时执行这两项操作,请求顺序流并使用取决于顺序的终端操作:
public static void main(String[] args) {
m1().filter(e -> {
System.out.println(e);
return false;
}).findFirst();
}
private static LongStream m1() {
return StreamSupport.longStream(Arrays.spliterator(new long[] { 1, 2, 3, 4 }), false);
}
But note that you should avoid writing software which relies on the processing order . 但是请注意,您应该避免编写依赖于处理顺序的软件。 You should define your task in a way that the processing steps do not depend on the processing order, even if the final result may depend on the encounter order .
即使最终结果可能取决于遇到的顺序 ,也应该以不依赖处理顺序的方式定义任务。
Eg, if you write 例如,如果您写
public static void main(String[] args) {
m1().filter(e -> {
System.out.println(e);
return e%2 == 0;
})
.findFirst()
.ifPresent(result -> System.out.println("final result: "+result));
}
private static LongStream m1() {
// note that you can have it much simpler:
return LongStream.of(1, 2, 3, 4 ).parallel();
}
the intermediate output may be in arbitrary order and there's no guaranty whether or not you will see “3” and “4” in the output, but you have the guaranty that the last output will be “final result: 2”, as that's the first matching element in encounter order . 中间输出可以是任意顺序,并且无法保证输出中是否显示“ 3”和“ 4”,但是您可以保证最后一个输出将是“最终结果:2”,因为这是遇到顺序中的第一个匹配元素。 See also here .
另请参阅此处 。
Well you are using anyMatch
which is not preserving order in the first place, so thats pretty much expected. 好吧,您使用的是
anyMatch
,它首先不保留顺序,因此,这非常值得期待。 If you want to get the first element use findFirst
for example. 如果要获取第一个元素,请使用
findFirst
例如。
As you are creating a parallel stream (second parameter = true
) StreamSupport.longStream(Arrays.spliterator(new long[] { 1, 2, 3, 4 }), true)
you don't get the ordered access. 在创建并行流(第二个参数=
true
)时, StreamSupport.longStream(Arrays.spliterator(new long[] { 1, 2, 3, 4 }), true)
您不会获得有序访问。 If you create a sequential stream (change second parameter from true
to false
) you should get what you want. 如果创建顺序流(将第二个参数从
true
更改为false
),则应该获得所需的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.