Saturday, September 6, 2025
HomeLanguagesJavaIntStream parallel() in Java

IntStream parallel() in Java

IntStream parallel() is a method in java.util.stream.IntStream. This method returns a parallel IntStream, i.e, it may return itself, either because the stream was already present, or because the underlying stream state was modified to be parallel.

IntStream parallel() is an intermediate operation. These operations are always lazy. Intermediate operations are invoked on a Stream instance and after they finish their processing, they give a Stream instance as output.

Syntax :

IntStream parallel()

Where, IntStream is a sequence of 
primitive int-valued elements and the function 
returns a parallel IntStream.

Below given are some examples to understand the function in a better way.
Example 1 :




// Java program to demonstrate working of
// IntStream parallel() on a given range
import java.util.*;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Creating a stream of integers
        IntStream stream = IntStream.range(5, 12);
  
        System.out.println("The corresponding " + 
                         "parallel IntStream is :");
        stream.parallel().forEach(System.out::println);
    }
}


Output :

The corresponding parallel IntStream is :
9
8
11
10
6
5
7

Example 2 :




// Printing sequential stream for the 
// same input as above example 1.
import java.util.*;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        IntStream stream = IntStream.range(5, 12);
  
        System.out.println("The corresponding " + 
                      "sequential IntStream is :");
        stream.sequential().forEach(System.out::println);
    }
}


Output :

The corresponding sequential IntStream is :
5
6
7
8
9
10
11

Example 3 :




// Java program to show sorted output
// of parallel stream.
import java.util.*;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Creating a stream of integers
        IntStream stream = IntStream.of(3, 4, 1, 5, 2, 3, 9);
  
        System.out.println("The sorted parallel" + 
                              " IntStream is :");
        stream.parallel().sorted().forEach(System.out::println);
    }
}


Output :

The sorted parallel IntStream is :
4
2
3
1
3
5
9

Note that it still shows as unsorted. That’s because forEach() is being used. To get the items processed in sorted order, use forEachOrdered(). But note that this negates the advantage of using parallel.

RELATED ARTICLES

Most Popular

Dominic
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS