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.
E.g., 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. See also here.