Hi everyone.
Welcome back to this tutorial.
In this tutorial, we will code and explore a scenario where your collection value is null, but you're
trying to perform the sort using the comparator on that collection for that purpose.
What I'm going to do, I'm going to use the default methods example to class and then in the get all
students.
Let me put this in presentation mode.
Your collection element is going to have a null, right?
And then if you try to perform.
Here we are calling the comparator training.
Right?
Let's go ahead and run this and then see what happens.
If you try to run it, you're going to get the null pointer exception.
How do we handle these kind of scenarios when you're trying to perform a sort?
Using the comparator comparator as a handy method called nulls first, nulls last, we are going to
go ahead and explore that.
I'm going to name this method as public static void.
Sort.
It null values.
So just going to accept the list of students.
Trying to be student list.
And then what we are going to do, we are going to perform the.
Null.
Check here.
We're going to perform the sort on this list which has a null value.
How do do.
So comparator.
Create an instance of comparator has a method called nulls as last nulls as first I'm going to use nulls
first method and then this is going to take in a comparator.
So we are going to use the name comparator.
We are going to basically sort the collection by name.
Let's give it a name.
Comparator.
Student and then student comparator.
Okay, now what we are going to do, we are going to do Student Comparator dot.
Now the certain competitor, the student list.
Dot chart and pass the student comparator.
Now, instead of calling this, I'm going to call the dot with null values and student list.
Okay, Now what we will do, we will call the consumer and then get the result printed in the console.
Let's run this.
Do you see any nullpointerexception here?
No null pointer exception.
And then it printed the result.
I'm going to have a code comment here.
Is going to be after.
Thought.
But null values.
On this example again.
So the program executed without any issues, but let's validate what's happening with the result.
In the result, if you take a look at it, this is a before shot.
In the before shot, we have the null value towards the end because that's where we added the null value
to the collection.
So the array list has a null value added as a last element.
Into it.
Right.
But in the short operation, what we are doing is we are asking the null value to be present at the
first element.
That's the reason why we are calling the nulls first.
If you call nulls last, what will happen?
So this null value will be pushed towards the end.
That's what is going to happen.
Let's go ahead and run this.
Take a look at the result.
The after sort with null values pushed the null value towards the end.
So this is one handy way of handling the null values.
If your collection is going to hold a null value in it.
With this.
We came to the end of this tutorial.
Thank you for watching.