We know that new Stream in Java (introduced in Java 8) interface let us manipulate collections of data in a declarative way.
In this topic, we will discover how the Stream interface gives us the opportunity to execute operations in parallel on a collection of data without much effort. It lets us declaratively turn a sequential stream into a parallel one
Definition and Making into Parallel Streams:
A parallel stream is one that splits the elements into multiple streams and assigns them into multiple chunks on different threads. Thus we can divide the workload of a given operation on the core of multiprocessors and thus it will make the CPU busy. We can convert the stream into parallel by attaching the keyword ‘parallel’.
Following example just gives us the idea how we can convert a stream into a parallel one!
Sum is 55
In the next part, we will see the difference between the performance of parallel streams, sequential streams and iterative process and taking a review on certain more specialized methods in parallel streams.
This article is contributed by keshav_786. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Reverse elements of a Parallel Stream in Java
- Conversion from a Java Set to a Scala Set
- HashMap Class Methods in Java with Examples | Set 1 (put(), get(), isEmpty() and size())
- Hashmap methods in Java with Examples | Set 2 (keySet(), values(), containsKey()..)
- Arrays.binarySearch() in Java with examples | Set 1
- Arrays.binarySearch() in Java with examples | Set 2 (Search in subarray)
- Set in Java
- Output of Java programs | Set 13 (Collections)
- Internal working of Set/HashSet in Java
- Factory method to create Immutable Set in Java 9
- Set to Array in Java
- Set to List in Java
- List to Set in Java
- Convert Stream to Set in Java
- Convert a Set to Stream in Java
- LinkedList set() Method in Java
- Job Sequencing Problem | Set 3 (Using TreeSet in JAVA)
- Vector set() Method in Java
- Array set() method in Java
- ArrayList set() method in Java with Examples