Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a parallel stream from an array?

I can create a Stream from an array using Arrays.stream(array) or Stream.of(values). Similarly, is it possible to create a ParallelStream directly from an array, without creating an intermediate collection as in Arrays.asList(array).parallelStream()?

like image 949
Soban Soundararajan Avatar asked Dec 02 '16 13:12

Soban Soundararajan


People also ask

How do you create a parallel stream?

Parallel Streams. Any stream in Java can easily be transformed from sequential to parallel. We can achieve this by adding the parallel method to a sequential stream or by creating a stream using the parallelStream method of a collection: List<Integer> listOfNumbers = Arrays.

How do I turn an array into a stream?

stream() A good way to turn an array into a stream is to use the Arrays class' stream() method. This works the same for both primitive types and objects.

Can we create stream from array?

The stream(T[] array) method of Arrays class in Java, is used to get a Sequential Stream from the array passed as the parameter with its elements. It returns a sequential Stream with the elements of the array, passed as parameter, as its source.

What is a parallel stream?

When a stream executes in parallel, the Java runtime partitions the stream into multiple substreams. Aggregate operations iterate over and process these substreams in parallel and then combine the results. When you create a stream, it is always a serial stream unless otherwise specified.


2 Answers

  Stream.of(array).parallel()     Arrays.stream(array).parallel() 
like image 188
Eugene Avatar answered Sep 24 '22 21:09

Eugene


TLDR;

Any sequential Stream can be converted into a parallel one by calling .parallel() on it. So all you need is:

  1. Create a stream
  2. Invoke method parallel() on it.

Long answer

The question is pretty old, but I believe some additional explanation will make the things much clearer.

All implementations of Java streams implement interface BaseStream. Which as per JavaDoc is:

Base interface for streams, which are sequences of elements supporting sequential and parallel aggregate operations.

From API's point of view there is no difference between sequential and parallel streams. They share the same aggregate operations.

In order do distinguish between sequential and parallel streams the aggregate methods call BaseStream::isParallel method.

Let's explore the implementation of isParallel method in AbstractPipeline:

@Override public final boolean isParallel() {     return sourceStage.parallel; } 

As you see, the only thing isParallel does is checking the boolean flag in source stage:

/**  * True if pipeline is parallel, otherwise the pipeline is sequential; only  * valid for the source stage.  */ private boolean parallel;  

So what does the parallel() method do then? How does it turn a sequential stream into a parallel one?

@Override @SuppressWarnings("unchecked") public final S parallel() {     sourceStage.parallel = true;     return (S) this; } 

Well it only sets the parallel flag to true. That's all it does.

As you can see, in current implementation of Java Stream API it doesn't matter how you create a stream (or receive it as a method parameter). You can always turn a stream into a parallel one with zero cost.

like image 43
ETO Avatar answered Sep 24 '22 21:09

ETO