DoubleStream parallel() in Java

DoubleStream parallel() is a method in java.util.stream.DoubleStream. This method returns a parallel DoubleStream, i.e, it may return itself, either because the stream was already present, or because the underlying stream state was modified to be parallel.

DoubleStream parallel() is an intermediate operation. These operations are always lazy. Intermediate operations are invoked on a Stream instance and after they finish their processing, they give a Stream instance as output.

Syntax :



DoubleStream parallel()

Where, DoubleStream is a sequence of primitive 
double-valued elements and the function 
returns a parallel DoubleStream.

Example 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working of
// DoubleStream parallel() on a given range
import java.util.*;
import java.util.stream.DoubleStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating a stream of doubles
        DoubleStream stream = 
              DoubleStream.of(5.2, 12.6, 4.5, 7.8);
  
        System.out.println("The corresponding "
                           + "parallel DoubleStream is :");
        stream.parallel().forEach(System.out::println);
    }
}

chevron_right


Output :

The corresponding parallel DoubleStream is :
4.5
7.8
12.6
5.2

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Printing sequential stream for the
// same input as above example 1.
import java.util.*;
import java.util.stream.DoubleStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating a stream of doubles
        DoubleStream stream = 
                 DoubleStream.of(5.2, 12.6, 4.5, 7.8);
  
        System.out.println("The corresponding "
                           + "sequential DoubleStream is :");
        stream.sequential().forEach(System.out::println);
    }
}

chevron_right


Output :

The corresponding sequential DoubleStream is :
5.2
12.6
4.5
7.8

Example 3 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to show sorted output
// of parallel stream.
import java.util.*;
import java.util.stream.DoubleStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating a stream of doubles
        DoubleStream stream =
                DoubleStream.of(2.3, 3.0, 4.5, 6.6);
  
        System.out.println("The sorted parallel"
                           + " DoubleStream is :");
        stream.parallel().sorted().forEach(System.out::println);
    }
}

chevron_right


Output :

The sorted parallel DoubleStream is :
4.5
6.6
2.3
3.0


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.