parallel stream in Java

All streams operations can execute either in serial or in parallel. Whether a stream will execute in serial or parallel can be determined with the isParallel() method, and the orientation of a stream can be modified with the BaseStream.sequential() and BaseStream.parallel() operations. 
Except for operations identified as explicitly nondeterministic, such as findAny(), whether a stream executes sequentially or in parallel should not change the result of the computation.

The Streams API was designed to make creating parallel streams quite easy. 
To create a parallel stream,
* From an existing stream: call parallel() on an existing stream to convert it to one that supports multi-threaded processing,
* From a Java collection class: The Collection interface includes a method parallelStream() that can be called on any collection and returns a parallel stream.
```
import java.util.Arrays;

class Main {
    public static void main(String[] args) {
        Arrays.asList(1, 2, 3, 4, 5, 6)
                .stream()
                .parallel()
                .forEach(s -> System.out.print(s + " "));
        System.out.println();
        Arrays.asList(1, 2, 3, 4, 5, 6)
                .parallelStream()
                .forEach(s -> System.out.print(s + " "));
    }
}
```
Some operations on streams preserve the parallel attribute, while others do not. For example, the Stream.concat(Stream s1, Stream s2) is parallel if either s1 or s2 is parallel. On the other hand, flatMap() creates a new stream that is not parallel by default, regardless of whether the underlying elements were parallel.