Parallel streams split the work and merge partial results. Addition is associative so the answer is stable; subtraction is not, which is why order-dependent folds must stay sequential.
long sequential = IntStream.rangeClosed(1, 1000).asLongStream().sum();
long parallel = IntStream.rangeClosed(1, 1000).parallel().asLongStream().sum();
System.out.println("sequential sum = " + sequential);
System.out.println("parallel sum = " + parallel);
System.out.println("identical = " + (sequential == parallel));
String joined = Stream.of("a", "b", "c", "d").parallel()
.collect(Collectors.joining());
System.out.println("parallel joining keeps order: " + joined);
sequential sum = 500500
parallel sum = 500500
identical = true
parallel joining keeps order: abcd
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-07-30