WEBVTT

00:00.110 --> 00:00.650
Hi everyone.

00:00.650 --> 00:01.880
Welcome back to this tutorial.

00:01.880 --> 00:07.790
In this tutorial, we will code and explore a scenario where your collection value is null, but you're

00:07.790 --> 00:12.860
trying to perform the sort using the comparator on that collection for that purpose.

00:12.860 --> 00:19.700
What I'm going to do, I'm going to use the default methods example to class and then in the get all

00:19.700 --> 00:20.570
students.

00:21.810 --> 00:23.370
Let me put this in presentation mode.

00:26.290 --> 00:30.250
Your collection element is going to have a null, right?

00:31.400 --> 00:33.620
And then if you try to perform.

00:34.770 --> 00:36.450
Here we are calling the comparator training.

00:36.450 --> 00:36.660
Right?

00:36.660 --> 00:39.030
Let's go ahead and run this and then see what happens.

00:40.250 --> 00:43.850
If you try to run it, you're going to get the null pointer exception.

00:43.880 --> 00:49.190
How do we handle these kind of scenarios when you're trying to perform a sort?

00:49.220 --> 00:55.940
Using the comparator comparator as a handy method called nulls first, nulls last, we are going to

00:55.940 --> 00:57.080
go ahead and explore that.

00:58.390 --> 01:02.170
I'm going to name this method as public static void.

01:03.390 --> 01:04.110
Sort.

01:06.680 --> 01:07.970
It null values.

01:12.740 --> 01:16.190
So just going to accept the list of students.

01:18.820 --> 01:20.290
Trying to be student list.

01:21.300 --> 01:25.500
And then what we are going to do, we are going to perform the.

01:26.280 --> 01:26.580
Null.

01:26.580 --> 01:27.030
Check here.

01:27.030 --> 01:31.320
We're going to perform the sort on this list which has a null value.

01:32.100 --> 01:32.520
How do do.

01:33.030 --> 01:35.040
So comparator.

01:37.340 --> 01:45.320
Create an instance of comparator has a method called nulls as last nulls as first I'm going to use nulls

01:45.500 --> 01:49.910
first method and then this is going to take in a comparator.

01:49.910 --> 01:52.310
So we are going to use the name comparator.

01:52.310 --> 01:55.460
We are going to basically sort the collection by name.

01:56.380 --> 01:57.370
Let's give it a name.

01:57.370 --> 01:58.570
Comparator.

02:00.510 --> 02:03.480
Student and then student comparator.

02:05.310 --> 02:10.560
Okay, now what we are going to do, we are going to do Student Comparator dot.

02:11.750 --> 02:14.120
Now the certain competitor, the student list.

02:15.240 --> 02:19.140
Dot chart and pass the student comparator.

02:19.710 --> 02:28.650
Now, instead of calling this, I'm going to call the dot with null values and student list.

02:29.600 --> 02:37.040
Okay, Now what we will do, we will call the consumer and then get the result printed in the console.

02:38.020 --> 02:39.100
Let's run this.

02:40.360 --> 02:42.310
Do you see any nullpointerexception here?

02:42.340 --> 02:43.840
No null pointer exception.

02:43.840 --> 02:45.830
And then it printed the result.

02:45.850 --> 02:47.860
I'm going to have a code comment here.

02:50.430 --> 02:52.050
Is going to be after.

02:52.840 --> 02:53.380
Thought.

02:54.410 --> 02:55.550
But null values.

02:57.030 --> 02:58.440
On this example again.

02:58.710 --> 03:03.600
So the program executed without any issues, but let's validate what's happening with the result.

03:03.630 --> 03:07.050
In the result, if you take a look at it, this is a before shot.

03:07.080 --> 03:13.440
In the before shot, we have the null value towards the end because that's where we added the null value

03:13.440 --> 03:14.190
to the collection.

03:14.190 --> 03:18.420
So the array list has a null value added as a last element.

03:19.120 --> 03:19.740
Into it.

03:19.750 --> 03:20.440
Right.

03:20.440 --> 03:27.070
But in the short operation, what we are doing is we are asking the null value to be present at the

03:27.070 --> 03:27.790
first element.

03:27.820 --> 03:30.280
That's the reason why we are calling the nulls first.

03:30.310 --> 03:34.000
If you call nulls last, what will happen?

03:34.000 --> 03:37.270
So this null value will be pushed towards the end.

03:37.690 --> 03:38.890
That's what is going to happen.

03:38.890 --> 03:40.090
Let's go ahead and run this.

03:42.000 --> 03:42.930
Take a look at the result.

03:42.930 --> 03:47.850
The after sort with null values pushed the null value towards the end.

03:48.030 --> 03:52.770
So this is one handy way of handling the null values.

03:52.770 --> 03:57.180
If your collection is going to hold a null value in it.

03:58.590 --> 03:58.890
With this.

03:58.890 --> 04:00.510
We came to the end of this tutorial.

04:00.540 --> 04:01.620
Thank you for watching.
