Java 8 Parallel Streams Example

author-image   By Dhiraj,   24 May, 2017 31K

When it comes to stream in Java 8, there are 2 different types of stream operation available. They are serial stream and parallel stream. In this article we will be discussing about parallel stream in java 8. We will look into different examples of using parallel stream, differences between them, performance improvement with parallel stream and also discuss about some aspects of using parallel stream.

What is ParalelStream

Parallel stream enables parallel computing that involves processing elements concurrently in parallel with each element in a seperate thread. But this does not guarantee high performance and faster execution everytime. It again depends on the number of CPU cores available.

Parallelstream has a much higher overhead compared to a sequential one.Parallel stream should be used only when there is a huge amount of elements to process and there is a performance problem with processing these elements. Also the parallel stream operations should be independent.

 Other Interesting Posts
Java 8 Lambda Expression
Java 8 Stream Operations
Java 8 Datetime Conversions
Java 9 Modularity Features

Creating Parallel Streams

We can create a parallel stream from existing stream by using parallel(). Following is an example:

Stream stream = Stream.of("John", "Mike", "Ryan","Donald", "Matthew");
Stream parallelStream = stream.parallel();

Another way to create parallelstream is using parallelStream() method.

Stream parallelStream = Arrays.asList("John", "Mike", "Ryan","Donald", "Matthew").parallelStream();

Parallel Stream Example

Following is an example using parallel stream that counts number of prime numbers between 1 and 50.Stream of numbers is created by a range method and then the stream is converted to parallel stream.

public void count() {
	final long count = IntStream.range(1, 50)
            .filter(number -> isPrime(number)).count();
    System.out.println("Count - " + count);
public boolean isPrime(final int number) {
    return number > 1
            && IntStream.rangeClosed(2, (int) Math.sqrt(number)).noneMatch(
                    divisor -> number % divisor == 0);

Output : Count - 15

Serial vs Parallel Stream Processing

If we use serial stream the order is guaranted as below:

Stream.of("John", "Mike", "Ryan","Donald", "Matthew").forEach(System.out::println);



Whereas, the order is not guaranted while using parallel stream.

Stream.of("John", "Mike", "Ryan","Donald", "Matthew").parallel().forEach(System.out::println);



Perform Check with Parallel Stream Processing

Parallel stream providdes significant performance improvement for larger streams. Following is an example that processes large streams and compares the time difference between processing time of sequential stream and parallel stream.
package com.devglan;

import java.util.ArrayList;
import java.util.List;

 * @author only2dhir
public class ParallelStreamPerformanceCheck {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List numList = new ArrayList();
        for (int i = 0; i < 1000; i++) {
        // Processing sequentially
        long startTime = System.currentTimeMillis(); -> processData(i));
        long endTime = System.currentTimeMillis();
        double sequentialStreamTimetaken = (endTime - startTime) / 1000;
        System.out.println("Time required with stream() : " + sequentialStreamTimetaken);
        // Parallel processing 
        startTime = System.currentTimeMillis();
        numList.parallelStream().forEach(i -> processData(i));
        endTime = System.currentTimeMillis();
        long parallelStreamTimetaken = (endTime - startTime) / 1000;
        System.out.println("Time required with parallelStream() : " + parallelStreamTimetaken);
        System.out.println("Differential time : " + (sequentialStreamTimetaken - parallelStreamTimetaken));
    private void processData(int num) {
        try {
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block


Time required with stream() : 11.0
Time required with parallelStream() : 2
Differential time : 9.0


I hope this article served you that you were looking for. If you have anything that you want to add or share then please share it below in the comment section.

If You Appreciate This, You Can Consider:

  • Like us at: Facebook or follow us at Twitter
  • Share this article on social media or with your teammates.
  • We are thankful for your never ending support.

About The Author


I am an energetic professional who enjoys the challenges involved in working with people and resolving real-time problems. Technical expertise in building highly scalable, distributed and self-healing cloud applications. Technical Skills: Java/J2EE, Spring Framework, Hibernate, Angular, Reactive Programming, Microservices, Rest APIs, Kafka, ELK, etc.

Further Reading on Java 8

1 Java Lambda Expression

2 Java8 Streams Operations

3 Java 8 Parallel Steams