0
0
JavaHow-ToBeginner · 4 min read

How to Use Parallel Stream in Java for Faster Data Processing

In Java, you can use parallelStream() on a collection to process elements concurrently using multiple threads. This allows faster data processing by splitting tasks across CPU cores automatically. Simply replace stream() with parallelStream() to enable parallel execution.
📐

Syntax

The parallelStream() method is called on a collection to create a parallel stream that processes elements concurrently. It is similar to stream() but uses multiple threads behind the scenes.

  • collection.parallelStream(): Creates a parallel stream from the collection.
  • filter(), map(), forEach(): Intermediate and terminal operations to process data.
java
List<String> list = List.of("a", "b", "c");
list.parallelStream()
    .filter(s -> s.startsWith("a"))
    .forEach(System.out::println);
Output
a
💻

Example

This example shows how to use parallelStream() to sum numbers in a list concurrently. It demonstrates faster processing by using multiple CPU cores automatically.

java
import java.util.List;

public class ParallelStreamExample {
    public static void main(String[] args) {
        List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        int sum = numbers.parallelStream()
                         .mapToInt(Integer::intValue)
                         .sum();

        System.out.println("Sum: " + sum);
    }
}
Output
Sum: 55
⚠️

Common Pitfalls

Using parallelStream() can cause issues if the operations are not thread-safe or if order matters. Avoid using parallel streams with shared mutable data or when the order of processing is important.

Also, parallel streams may not always improve performance for small data sets due to overhead.

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

public class ParallelStreamPitfall {
    public static void main(String[] args) {
        List<Integer> numbers = List.of(1, 2, 3, 4, 5);
        List<Integer> results = new ArrayList<>();

        // Wrong: modifying shared list in parallel stream
        numbers.parallelStream().forEach(n -> results.add(n * 2));

        System.out.println("Results (wrong): " + results);

        // Right: collect results safely
        List<Integer> correctResults = numbers.parallelStream()
            .map(n -> n * 2)
            .toList();

        System.out.println("Results (right): " + correctResults);
    }
}
Output
Results (wrong): [4, 2, 6] Results (right): [2, 4, 6, 8, 10]
📊

Quick Reference

  • Use collection.parallelStream() to create a parallel stream.
  • Use thread-safe operations inside the stream.
  • Prefer stateless and non-interfering operations.
  • Use toList() or collectors to gather results safely.
  • Parallel streams are best for large data sets and CPU-intensive tasks.

Key Takeaways

Use parallelStream() to process collections concurrently for better performance.
Avoid modifying shared mutable data inside parallel streams to prevent errors.
Parallel streams work best with stateless, thread-safe operations.
Not all tasks benefit from parallel streams; test performance for your use case.
Collect results safely using toList() or collectors instead of side-effects.