What is Java Parallel Streams?
Last Updated :
21 Feb, 2025
Java Parallel Streams is a feature of Java 8 and higher, meant for utilizing multiple cores of the processor. Normally any Java code has one stream of processing, where it is executed sequentially. Whereas by using parallel streams, we can divide the code into multiple streams that are executed in parallel on separate cores and the final result is the combination of the individual outcomes. The order of execution, however, is not under our control.
Therefore, it is advisable to use parallel streams in cases where no matter what is the order of execution, the result is unaffected and the state of one element does not affect the other as well as the source of the data also remains unaffected. Parallel streams are best used when the order doesn’t matter, elements don’t depend on each other, and data remains unchanged.
Parallel streams enable large-scale data processing tasks to be handled more efficiently by utilizing the full power of multi-core processors.
The below image demonstrates the difference between Sequential Streams and Parallel Streams:
Sequential vs Parallel Streams
The main difference between Sequential and Parallel Stream is listed below:
- Sequential Streams: Process elements in a sequential manner, one element at a time
- Parallel Streams: Process elements in parallel, utilizing multiple CPU cores.
Why Parallel Streams?
Parallel Streams improve performance by utilizing multiple cores, but they aren't always the best choice. In cases where the task requires a specific order of execution, sequential streams are more reliable, even if it means sacrificing performance. The performance gain from parallel streams is significant only for large-scale or computationally intensive programs. For smaller tasks, the difference may be negligible. Use parallel streams primarily when the sequential stream behaves poorly.
How to Create and Use Parallel Streams
There are two main ways to create parallel streams in Java:
1. Using the parallel() Method
The parallel() method is used on an existing sequential stream to convert it into a parallel stream. This method return a parallel stream, which can then be processed using various stream operations.
Example: This example demonstrates how to use a parallel stream to filter and print even number from a list concurrently.
Java
// Java Program to demosntrate
// the working of parallel stream
import java.util.Arrays;
import java.util.List;
public class Geeks {
public static void main(String[] args)
{
List<Integer> numbers
= Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Convert to parallel stream and perform operations
numbers
.parallelStream()
// Filter even numbers
.filter(n -> n % 2 == 0)
// Print each number
.forEach(System.out::println);
}
}
2. Using the parallelStream() Method
The parallelStream() method in Java is used to create a parallelstream from a Collection. We can directly call parallelStream() on Collections (such as List, set) to obtain parallel stream.
Example: This example demonstrates reading lines from a file and processing them in parallel using a parallel stream.
Java
// Java Program to demonstrate the
// working of parallelStream()
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.stream.Collectors;
public class ParallelStreamFromFile {
public static void main(String[] args) throws IOException {
File file = new File("pathToYourFile.txt");
// Read all lines from the file into a List
List<String> lines = Files.readAllLines(file.toPath());
// Create a parallel stream and print each line
lines.parallelStream()
.forEach(System.out::println);
}
}
Output:
Similar Reads
Parallel vs Sequential Stream in Java Prerequisite: Streams in Java A stream in Java is a sequence of objects which operates on a data source such as an array or a collection and supports various methods. Â It was introduced in Java 8's java.util.stream package. Stream supports many aggregate operations like filter, map, limit, reduce, f
5 min read
Serial Sort v/s Parallel Sort in Java We often need to sort array while programming. For this, we use inbuilt method provided by Java in Arrays class i.e sort(). sort() method uses merge sort or Time Sort to sort the array elements. In both the cases sort() method sequentially sort the elements of an array. In Java 8, there is a new API
4 min read
Parallel Data Processing in Java | Set 1 We know that new Stream in Java (introduced in Java 8) interface let us manipulate collections of data in a declarative way. In this topic, we will discover how the Stream interface gives us the opportunity to execute operations in parallel on a collection of data without much effort. It lets us dec
2 min read
What is Apache Kafka Streams? Kafka Streams is a library for processing and analyzing data stored in Kafka. It expands on crucial stream processing ideas such as clearly separating event time from processing time, allowing for windows, and managing and querying application information simply but effectively in real time. Kafka S
4 min read
What is Reactive Programming in Java? Reactive programming is an important programming paradigm that is becoming increasingly popular in Java development. Reactive programming is based on the use of asynchronous and non-blocking data streams to handle data and events. In this article, we will explore the concept of reactive programming
4 min read
Stream In Java Stream was introduced in Java 8, the Stream API is used to process collections of objects. A stream in Java is a sequence of objects that supports various methods that can be pipelined to produce the desired result. Use of Stream in JavaThe uses of Stream in Java are mentioned below:Stream API is a
7 min read