IntStream parallel() method in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 28-Mar-2019 11:06:48
Advertisements