IntStream parallel() method in Java


The parallel() method of the IntStream class in Java returns an equivalent parallel stream. The method may return itself, either because the stream was already parallel, or because the underlying stream state was modified to be parallel.

The syntax is as follows:

IntStream parallel()

Create an IntStream and you can use the range() method as well to set the range of elements:

IntStream intStream = IntStream.range(20, 35);

Now, use the parallel() method:

intStream.parallel()

The following is an example to implement IntStream parallel() method in Java

Example

 Live Demo

import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.range(20, 35);
      System.out.println("Parallel IntStream = ");
      intStream.parallel().forEach(System.out::println);
   }
}

output

Parallel IntStream =
29
30
28
27
31
25
32
22
34
33
21
26
23
20
24

Updated on: 30-Jul-2019

356 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements