Saving order in java thread

I examined related questions such as How to ensure processing order in java8 threads? ordering the output element is not entirely clear to me. So please clarify my next doubt.

 Integer[] intArray = {1, 2, 3, 4, 5, 6, 7, 8 };
            List<Integer> listOfIntegers =
                new ArrayList<>(Arrays.asList(intArray));
       listOfIntegers
            .parallelStream()
             .unordered()
            .forEachOrdered(e -> System.out.print(e + " "));

I think, at least theoretically (or according to the java specification), it can print in random order than 1, 2, 3, 4, 5, 6, 7, 8. Am I correct?

Another related question - the decision to maintain the order of the meeting is taken at what point in the execution? To be more precise, is it an assessment of the entire characteristics of the ORDER flow pipeline, performed by passing through the characteristics of the initial, intermediate operations and the operation of the terminal before it even starts?

+2
2

unordered() , , sorted.

, filter map, , skip, limit distinct , . , distinct unordered().

, sorted , , .

, - .

, , , .

, , , skip, limit, distinct , , , , - sorted, , .

, 8 Java 8, . , , skip limit. , sort . . , , .

,

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

.

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

, , . .

, "- ORDER , , ?" , , , , , .

+4

unordered, . , , , , , .

forEachOrdered "", , .unordered(), , . unordered, , forEach.

, forEachOrdered . , , unordered, .

+2

Source: https://habr.com/ru/post/1683049/


All Articles