When computing Cartesian products using streams, I can produce them in parallel and consume them in order, as the following code demonstrates:
int min = 0; int max = 9; Supplier<IntStream> supplier = () -> IntStream.rangeClosed(min, max).parallel(); supplier.get() .flatMap(a -> supplier.get().map(b -> a * b)) .forEachOrdered(System.out::println);
This will completely print everything in order, now consider the following code, where I want to add it to the list, while maintaining order.
int min = 0; int max = 9; Supplier<IntStream> supplier = () -> IntStream.rangeClosed(min, max).parallel(); List<Integer> list = supplier.get() .flatMap(a -> supplier.get().map(b -> a * b)) .boxed() .collect(Collectors.toList()); list.forEach(System.out::println);
Now it does not print in order!
This is understandable, given that I do not demand anywhere to maintain order.
Now the question is: is there a way to collect() or is there a Collector that preserves order?
skiwi source share