Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the partitioning by collector.
The partitioning by collector is also a kind of grouping by collector.
The partitioning by accepts a predicate as an input and the output is going to be a map.
And the key of the return type is always going to be a Boolean.
And then there are two different versions of partitioning by one with a one argument version which accepts
a predicate as an input, and the another one is a predicate.
And the downstream, which is the two parameter version.
Let's go ahead and code this one.
What I'm going to do.
I'm going to create a class called Streams.
Partitioning by sample, and then I'm going to make this class executable.
By adding public static void main.
Understand presentation mode.
And then what I'm going to do, I'm going to create an example.
I'm going to create a method, basically public static void.
Partitioning by.
Want to give it a name as one, because I'm going to quote a couple of examples here.
And let's call the student database class and get all the students.
Call the stream method and then perform the collect method.
So we are going to explore the partitioning by.
Right.
Partitioning.
Thanks to this partitioning by is are going to do a static import of this one from the collectors.
As you see, we have the partitioning by here.
Let's create a predicate here.
The predicate is going to be because we all know partitioning.
If you go and take a look at it, it accepts there are two different versions, but it accepts.
The.
Predicate as an input.
Let's create a predicate.
The predicate is going to be of type student.
I'm going to create a predicate.
This predicate is going to be.
Student dot.
Didn't dot get greater than or equal to 3.8.
So I'm going to parse this predicate here.
The output that it is going to create is going to be a map.
If you go to the partitioning by, it's going to create a map of type boolean.
You cannot override that.
Right.
So it's going to be map.
To import the class which is of type Java.util.
The key is going to be Boolean and the result is going to be a list of prints.
Basically student.
So I'm going to name this one as partitioning map.
Right.
And then let's print this one out.
Partitioning map equal to.
I'm going to call this method.
So the output is going to be whatever that matches this predicate is going to be under the true key.
Whatever the doesn't match, this one is going to be under the false key.
Let's execute this and validate the result.
If you take a look at it, there are two keys.
One is the false key and another one is the true key.
So in the false key, whoever has the GPA less than or equal to 3.8, if you take a look at the GPA
of this one, it is 3.6.
And the GPA of this one is 3.5.
Under the true category, whatever that has the GPA greater than or equal to 3.8, all those things
falls under the true key.
So this is the one argument version.
Let's take a look at the two argument version.
Gonna name this one as partitioning.
Underscore two.
Auditioning underscore two in here.
What we are going to do.
It is going to take in a second argument.
The second argument is a downstream basically, this is again another collector.
So what I'm going to do, I'm going to override this by passing the two set collector.
So what's going to happen instead of this list we are going to have set?
Imported.
There you go.
And then we need to.
What's happening here?
This should not be any issues, actually.
So in here we are passing the two set.
I believe we need to provide one more.
Some issues here.
Is this a comma?
Yep.
You have it.
Okay.
Uh, we should have the comma here.
We should not close that, actually.
Because partitioning by is the one which is going to take two arguments, right?
The first argument is a predicate, and second argument is a downstream.
Basically, there is nothing but a collector, right?
So to set is one of the collector.
Then what I'm going to do, I'm going to call this method, I'm going to comment this one so that it
can avoid the confusion.
The result is going to be the same here In this use case.
It is just that we are overriding the default type of list to to set.
You can pass any kind of collector here.
Gonna run this one.
But on this one, it is going to give me the output as similar to the previous scenario, but it is
of type set instead of a list.
So this is all about the partitioning by.
With this we came to the end of this tutorial.
Thank you for watching.