LongStream parallel() in Java
LongStream parallel() is a method in java.util.stream.LongStream. This method returns a parallel LongStream, i.e, it may return itself, either because the stream was already present, or because the underlying stream state was modified to be parallel.
LongStream 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 :
LongStream parallel() Where, LongStream is a sequence of primitive long-valued elements and the function returns a parallel LongStream.
Below given are some examples to understand the function in a better way.
Example 1 :
// Java program to demonstrate working of // LongStream parallel() on a given range import java.util.*; import java.util.stream.LongStream; class GFG { // Driver code public static void main(String[] args) { // Creating a stream of longs LongStream stream = LongStream.range(5L, 12L); System.out.println("The corresponding " + "parallel LongStream is :"); stream.parallel().forEach(System.out::println); } } |
Output :
The corresponding parallel LongStream is : 9 8 11 10 6 5 7
Example 2 :
// Printing sequential stream for the // same input as above example 1. import java.util.*; import java.util.stream.LongStream; class GFG { // Driver code public static void main(String[] args) { LongStream stream = LongStream.range(5L, 12L); System.out.println("The corresponding " + "sequential LongStream is :"); stream.sequential().forEach(System.out::println); } } |
Output :
The corresponding sequential LongStream is : 5 6 7 8 9 10 11
Example 3 :
// Java program to show sorted output // of parallel stream. import java.util.*; import java.util.stream.LongStream; class GFG { // Driver code public static void main(String[] args) { // Creating a stream of longs LongStream stream = LongStream.of(3L, 4L, 1L, 5L, 2L, 3L, 9L); System.out.println("The sorted parallel" + " LongStream is :"); stream.parallel().sorted().forEach(System.out::println); } } |
Output :
The sorted parallel LongStream is : 4 2 3 9 3 5 1
Recommended Posts:
- LongStream of() in Java
- LongStream sum() in Java
- LongStream builder() in Java
- LongStream asDoubleStream() in Java
- LongStream boxed() in Java
- LongStream concat() in Java
- LongStream findFirst() in Java
- LongStream iterator() in Java
- LongStream limit() in Java
- LongStream summaryStatistics() in Java
- LongStream skip() in Java
- LongStream sequential() in Java
- LongStream range() in Java
- LongStream rangeClosed() in Java
- LongStream mapToObj() in Java
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.



