The parallel execution of below code taking longer than the sequential code. I know parallel streams have more complexity and are more expensive than sequential streams and we can't expect parallel streams to work wonders all the time. I am just concerned about the below code
List<Integer> collect = IntStream.rangeClosed(1, 1000000)
.unordered()
.parallel()
.filter(e -> e%7 == 0)
.boxed()
.collect(Collectors.toList());
long endTime = System.nanoTime();
collect.forEach(System.out::println);
System.out.println(endTime - startTime);
output:
With Sequential Stream : 40 227 795
With Parallel Stream: 74 656 768
Is this stream stateful? If not then why it's taking longer with the parallel stream? What can be the reason behind this? Can there be a precise guess on this?