Stream.reduce always keeps order on a parallel, unordered stream

I reviewed a few previous questions, such as Preserving the Encounter order in a java stream , this answer by Brian Goetz, as well as the javadoc for Stream.reduce (), and the java.util.stream javadoc package , and yet I still can not understand following:

Take this piece of code:

  public static void main(String... args) {
    final String[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
    System.out.println("Alphabet: ".concat(Arrays.toString(alphabet)));
    System.out.println(new HashSet<>(Arrays.asList(alphabet))
          .parallelStream()
          .unordered()
          .peek(System.out::println)
          .reduce("", (a,b) -> a + b, (a,b) -> a + b));
  }

Why does reduction always * keep the order of meetings?

  • So far, after several dozen launches, the conclusion is the same
+4
source share
2 answers

unordered ; , , Stream, .

, , ( ).

( ) jdk-8 jdk-9 - reduce . , .

, unordered - , , , , .

, /, , findFirst ( ) , unordered java-9 java-8.

+4

, ABCD.

: AB CD. , AB , , CD , . , .

unordered , limit, reduce.

+3

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


All Articles