Java Stream API - Parallel and Sequential Streams

The calls to sequential() and parallel() simply change the mode of the entire stream. Whichever mode the stream is in when a terminal operation is invoked is the mode used. In your case, since sequential() is after parellel() your stream will be executed sequentially.

This is documented by the Stream interface:

Stream pipelines may execute either sequentially or in parallel. This execution mode is a property of the stream [emphasis added]. Streams are created with an initial choice of sequential or parallel execution. (For example, Collection.stream() creates a sequential stream, and Collection.parallelStream() creates a parallel one.) This choice of execution mode may be modified by the BaseStream.sequential() or BaseStream.parallel() methods, and may be queried with the BaseStream.isParallel() method.

And the java.util.stream package:

The stream pipeline is executed sequentially or in parallel depending on the mode of the stream on which the terminal operation is invoked. The sequential or parallel mode of a stream can be determined with the BaseStream.isParallel() method, and the stream's mode can be modified with the BaseStream.sequential() and BaseStream.parallel() operations. The most recent sequential or parallel mode setting applies to the execution of the entire stream pipeline [emphasis added].


The quickest way to get an answer to such question is to create a very simple test. I've tried something like so:

public static void main(String args[]) {
    List<Integer> list = new ArrayList<>();
    for (int i=0; i<50;i++){
        list.add(i);
    }

    list.stream()
        .sequential()
        .map(a->{
            System.out.println("sequential " + a); 
            return a;
        })
        .parallel()
        .forEach(a-> System.out.println("parallel " + a));
    
    System.out.println();
    System.out.println();
    
    list.stream()
        .parallel()
        .map(a->{
            System.out.println("parallel " + a); 
            return a;
        })
        .sequential()
        .forEach(a-> System.out.println("sequential " + a));
}

When you run the code and analyse the output of both of those streams you can see that the first one was parallel and the second one was sequential.

Based on that we can deduce that only the last parallel/sequential call matters - it is basicly overwriting the flag.

Tags:

Java