Hi everyone.
Welcome back to this tutorial.
In this tutorial, we will explore the parallel streams.
What is a parallel stream?
A parallel stream is something which is going to split the source data into multiple parts.
And process them parallely and combine the result and give you the result as an output.
That's what is called a parallel stream.
How to create a parallel stream.
Have an example here for a sequential stream.
What it is doing here is that in stream dot range closed, it is going to create an empty stream of
1000 elements and then it is going to perform a summation and return you.
The result.
If you do not call the parallel method explicitly, then that is a sequential stream.
So this is an example for a parallel stream.
What is happening here is as soon as we call the method parallel behind the scenes, what it is going
to do, this is going to split the stream into multiple parts and process them concurrently.
Basically, it is going to perform the summation concurrently on each and every part.
It had split and then combine the result and give you the result as an output.
Let's go ahead and explore this using an example.
When to open the int.
There you go.
Have the open.
We put this in full screen and in here what I'm going to do, I'm going to create a package called Parallel
String and I'm going to create a class called Parallel stream example, parallel stream example.
But let us make this class executable by adding public static void main.
There you go.
I'll put this in presentation mode.
So I'm going to create two different methods.
One is the.
Some
sequential cream.
We are going to make this class a static one and it is going to return the result.
Right?
And then what the stream is going to do in stream dot range closed.
I'm going to give the value as 100,000.
And perform the sum.
And we are going to return the result.
Rate.
Let's put the proper keyword here.
Return keyword.
And what I'm going to do, I'm going to create another method here.
I'm going to name this one as some parallel string.
Okay.
So in here.
I'm going to call this and check the result to find out whether both are giving us the same result or
not.
Going to be.
Some parallel string and I'm going to run this example.
You take a look at it.
Both returned the same result.
Right.
But the idea is to find out which is performing better.
Right.
So we will explore how to check the performance of these two methods and compare which one is better
in the next tutorial.
With this, we came to the end of this tutorial.
Thank you for watching.