Parallel Streams in Java (introduced in Java 8) allow parallel execution of stream operations using multiple threads, leveraging the power of multi-core processors to improve performance—especially on large data sets.
What Are Parallel Streams?
A parallel stream divides the stream into multiple substreams, processes them in parallel, and then combines the results. Java internally uses the Fork/Join framework to implement parallelism.
How to Create a Parallel Stream
Approach | Syntax Example |
---|---|
From a collection | list.parallelStream() |
From a stream | stream.parallel() |
Program-1
This program illustrates the difference between sequential streams and parallel streams in Java 8, using an array of integers. It showcases how elements are processed in order vs out-of-order when processed in parallel.
//ParallelStreamsDemo.java import java.util.Arrays; public class ParallelStreamsDemo { public static void main(String[] args) { // Create a sample array of integers int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Sequential stream example System.out.print("Sequential Stream: "); Arrays.stream(numbers).forEach(num -> System.out.print(num + " ")); System.out.println(); // Parallel stream example System.out.print("Parallel Stream: "); Arrays.stream(numbers) .parallel() // Convert stream to parallel stream .forEach(num -> System.out.print(num + " ")); System.out.println(); } } /* C:\>javac ParallelStreamsDemo.java C:\>java ParallelStreamsDemo Sequential Stream: 1 2 3 4 5 6 7 8 9 10 Parallel Stream: 7 6 9 3 10 8 2 5 1 4 C:\>java ParallelStreamsDemo Sequential Stream: 1 2 3 4 5 6 7 8 9 10 Parallel Stream: 7 6 9 10 8 3 5 2 1 4 C:\>java ParallelStreamsDemo Sequential Stream: 1 2 3 4 5 6 7 8 9 10 Parallel Stream: 7 6 9 10 3 8 2 1 5 4 */
Program-2
This program demonstrates how parallel streams in Java can be used to perform complex data operations like filtering, mapping, and aggregating efficiently and concurrently.
//ParallelStreamsOperations.java import java.util.Arrays; import java.util.stream.IntStream; public class ParallelStreamsOperations { public static void main(String[] args) { // Example array int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Example: Sum of squares of even numbers in parallel int sum = Arrays.stream(numbers).parallel() .filter(n -> n % 2 == 0).map(n -> n * n).sum(); System.out.println("Sum of squares of even numbers (Parallel): " + sum); // Another example: Concurrent modification using parallel streams int[] modifiedNumbers = IntStream.range(0, 10).parallel() .map(n -> n * 2) // Double each number .toArray(); // Convert to array System.out.print("Modified numbers (Parallel): "); Arrays.stream(modifiedNumbers).forEach(num -> System.out.print(num + " ")); System.out.println(); } } /* C:\>javac ParallelStreamsOperations.java C:\>java ParallelStreamsOperations Sum of squares of even numbers (Parallel): 220 Modified numbers (Parallel): 0 2 4 6 8 10 12 14 16 18 */
Java parallel streams provide a powerful mechanism to leverage multi-core processors for concurrent execution of stream operations. They offer straightforward syntax and integrate seamlessly with existing Stream API operations, allowing developers to write concise and efficient code for parallel processing of collections. However, it’s important to understand their characteristics and use them judiciously based on the nature of the task and hardware capabilities.