Generally, a parallel stream is basically a stream that partitions its elements into multiple chunks, processing each chunk with a different thread. Therefore, you can automatically partition the workload of a given operation on all the cores of your multicore processor and keep all of them equally busy.
However, it's important to note that just by invoking parallelStream()
doesn't necessarily make the stream parallel, in fact, invoking this method might even return a sequential stream rather a parallel one.
as stated in the java doc:
default Stream<E> parallelStream()
Returns a possibly parallel Stream with this collection as its source.
It is allowable for this method to return a sequential stream.
Therefore we can conclude it's up to the library to determine whether it's appropriate to utilise multiple threads. in most cases, this will be the case when there is a huge amount of data to process.
as in your case, there seems to be only 100 elements within the ArrayList
hence there is no difference whether you utilise parallelStream()
or not.
Lastly, but not least I'd always use a sequential stream to process data in a sequential manner except in cases where there is a huge amount of data to process or when you're experiencing performance issues processing data with a sequential stream in which case you can switch to a parallelStream.