Difference between Parallel and Sequential Streams in Java


Stream was introduced in Java 8 and it was included as java.util.stream package. It is a sequence of objects which acts as an array or a collection. Different types of methods are supported by stream. Besides this, stream also supports different kinds of aggregate operations which include −

  • Filter
  • Map
  • Reduce
  • Limit
  • Match
  • Find

The source is not affected when any of these operations are performed on a stream. Instead of this, a new stream is created to apply these operations. There are two types of streams which are sequential and parallel. In this article, we will discuss the difference between parallel and sequential streams.

What is Sequential Stream?

A sequential stream uses a single thread for the processing of the pipeline. The objects of the sequential stream are available on the same processing system and are lined up in the same stream. So, a multicore system is not used for the processing.

What is Parallel Stream?

Parallel stream uses multicore processors which helps in enhancing the performance of a program. The code is divided into multiple streams through the parallel stream methods. All the streams are executed parallelly on separate cores. When the final result is to be displayed, all the cores are combined. The results can be unordered as the execution is not under the control of the developers. Parallel streams can be used in the following ways −

  • Collection interface consists of parallelStream() method which can be used to implement parallel streams.
  • The BaseStream interface consists of parallel() method which can be applied on a sequential stream.

Difference between Sequential and Parallel Stream

Sequential and Parallel Streams in Java have many differences which we can see in the table below.

Sequential Stream Parallel Stream
The execution of a sequential stream is done on the single core of a computer. The execution of a parallel stream is done on multiple cores of a computer.
The performance of the sequential stream is slow. The performance of a parallel stream is fast.
The processing of sequential stream maintains order during execution Order is not maintained during the execution of the parallel stream.
A single iteration can occur at a time. Multiple iterations can occur as the streams execute on multiple cores.
Each iteration has to wait for execution till the execution of the previous one is finished. A stream has to wait if all the cores are busy else they execute simultaneously with waiting.
The sequential stream is less prone to errors. Parallel stream is more prone to errors.
It does not depend on platform. It is dependent on platform.

Conclusion

Streams in Java are of two types which include sequential and parallel streams. Sequential stream is a stream in which the operations on objects are performed in a sequence. The result of the program is ordered. It uses the single core of the computer for execution. Its performance is slow but it is platform-independent. Parallel Stream uses multiple cores of a computer for its execution. The performance of this stream is very fast but the results are not in order.

FAQs on Sequential Stream Vs. Parallel Stream

1. Which of the streams is platform-independent?

Sequential stream is platform-independent as it uses only a single core to execute the code. Parallel stream uses multiple cores for code execution so it is platform-dependent.

2. Which stream is more prone to errors?

Parallel stream is more prone to errors as the stream runs on multiple cores of a computer. The result is not ordered. The sequential stream is less prone to errors as only a single core is used to execute the code.

3. When do the iterations have to wait in sequential and parallel streams?

In a sequential stream, only one iteration can work at a time. The next iteration has to wait till the time the execution of the current iteration is complete. In the parallel stream, the iterations work simultaneously on different cores. If all the cores are busy, iterations have to wait.

4. Which stream maintains an order during execution?

Sequential stream maintains an order during execution. It is so because it uses only one core and each iteration has to wait till the current one completes its execution.

5. Which of the streams is a fast performer?

Parallel stream is the fast performer as it uses multiple cores for execution. Sequential stream uses only one core so its performance is slow.

Updated on: 22-Jul-2024

0 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements