Parallel streams in Java are part of the Java Streams API (introduced in Java 8) and allow you to perform parallel data processing in a declarative way, taking advantage of multiple CPU cores for better performance on large datasets.
🔹 What is a Stream?
A Stream is a sequence of elements that supports aggregate operations (like map
, filter
, reduce
, etc.). By default, streams run sequentially.
Example of sequential stream:
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
names.stream()
.filter(name -> name.startsWith("A"))
.forEach(System.out::println);
🔹 Parallel Streams
A parallel stream divides the data into multiple chunks, processes them in parallel on different threads, and then combines the results.
Example:
List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
// Using parallel stream
int sum = numbers.parallelStream()
.mapToInt(Integer::intValue)
.sum();
System.out.println("Sum: " + sum);
Here, the computation is split across multiple threads automatically.
🔹 Ways to Create a Parallel Stream
- From a Collection:
list.parallelStream()
- From an existing Stream:
stream.parallel()
- Directly using static methods:
Stream.of(1,2,3,4,5).parallel();
🔹 Key Differences: Sequential vs Parallel
Feature | Sequential Stream | Parallel Stream |
---|---|---|
Execution | Single thread | Multiple threads (ForkJoinPool) |
Performance | Good for small data | Good for large data |
Order | Maintains order | May not maintain order (use forEachOrdered ) |
Overhead | Low | High (thread management) |
🔹 Example: Sequential vs Parallel
List<Integer> numbers = IntStream.rangeClosed(1, 1000).boxed().toList();
// Sequential
numbers.stream()
.forEach(System.out::print);
// Parallel
numbers.parallelStream()
.forEach(System.out::print); // order not guaranteed
// Parallel with order
numbers.parallelStream()
.forEachOrdered(System.out::print); // maintains order
🔹 When to Use Parallel Streams
✅ Good for:
- Large datasets
- CPU-intensive operations
- Independent operations (no shared mutable state)
❌ Avoid if:
- Dataset is small (thread overhead > benefit)
- Operations involve I/O (like file/DB access)
- You need strict ordering