WEBVTT

00:00.880 --> 00:01.480
Hi everyone.

00:01.480 --> 00:02.770
Welcome back to this tutorial.

00:02.770 --> 00:08.020
In this tutorial we will code and explore the default methods that got introduced as part of the list

00:08.020 --> 00:09.010
interface.

00:09.070 --> 00:15.670
For this purpose, what I'm going to do, I'm going to create a package called defaults.

00:16.210 --> 00:26.170
And then the next step is I'm going to create a class called Default Methods example.

00:26.830 --> 00:33.280
The next step is we are going to make this class executable by adding public static void main.

00:37.130 --> 00:38.570
Now the use cases.

00:39.790 --> 00:41.770
Sort the list of names.

00:42.950 --> 00:45.560
Basically, which are nothing but a list of strings.

00:46.370 --> 00:47.780
In alphabetical order.

00:49.230 --> 00:51.690
I'm just keeping this use case really simple.

00:53.350 --> 00:56.830
Now, the first step is we need to create the input list, right?

00:56.860 --> 00:58.870
Let's give a list of string.

01:01.750 --> 01:04.420
And then let's give it a name called String list.

01:04.810 --> 01:06.520
First up is we'll import this.

01:07.550 --> 01:14.900
As part of the Java.util package and we are going to use the arrays class dot as list.

01:15.140 --> 01:17.000
I'm going to give some list of names.

01:17.030 --> 01:19.220
The names are going to be Adam.

01:20.320 --> 01:22.240
The next one is going to be Jenny.

01:29.810 --> 01:30.410
Mike.

01:31.860 --> 01:32.250
Then.

01:33.680 --> 01:34.010
Rick.

01:35.090 --> 01:37.100
So the list is not in.

01:38.010 --> 01:38.640
Dotted order.

01:38.640 --> 01:43.050
Basically, we are going to start this list in alphabetical order.

01:43.200 --> 01:50.460
Let's talk about the prior Java eight way and then we will explore the new Java eight way using the

01:50.460 --> 01:53.610
default method that is part of the list interface itself.

01:55.870 --> 01:59.020
Are some code comments here prior to Java eight.

02:00.070 --> 02:01.600
Now the first purpose.

02:02.550 --> 02:04.050
Let's use a collections class.

02:04.050 --> 02:10.350
That is a way we normally do use a collection class collections class and then call the sort method

02:10.350 --> 02:13.020
and pass this input list as an input.

02:13.050 --> 02:19.800
So this is going to sort the result, basically sort the input in alphabetical order and give you the

02:19.800 --> 02:20.370
result.

02:22.430 --> 02:23.480
What comments here?

02:25.660 --> 02:26.650
Dotted list.

02:28.270 --> 02:29.080
Using.

02:32.080 --> 02:33.040
Elections.

02:35.630 --> 02:36.830
Dot sort method.

02:38.100 --> 02:38.640
Right.

02:38.820 --> 02:41.280
Then let's print the result in English.

02:41.910 --> 02:42.390
There you go.

02:42.420 --> 02:44.640
Now I'm going to run this program.

02:45.870 --> 02:47.640
Run default methods example.

02:48.210 --> 02:49.170
There we go.

02:49.920 --> 02:50.700
Gave you the result.

02:50.700 --> 02:54.090
If you take a look at the result, the result is in sorted order.

02:54.330 --> 02:55.590
We take a look at the Alex.

02:55.620 --> 02:59.490
Alex is at the third position, but here Alex is at the second position.

02:59.970 --> 03:04.380
The whole input list is sorted in ascending order.

03:04.380 --> 03:07.890
Now let's talk about the Java eight approach.

03:10.440 --> 03:11.660
So in Java eight.

03:12.820 --> 03:14.290
The list interface here.

03:14.320 --> 03:15.730
Go to the list interface.

03:15.970 --> 03:16.900
This is an interface.

03:16.900 --> 03:23.020
The list interface has a lot of default methods in it that got introduced as part of Java eight.

03:23.020 --> 03:30.220
And let's take a look at the methods, whatever that got introduced as part of the list interface.

03:31.480 --> 03:36.940
As soon as we dot is going to give you a lot of methods and sort is one of them.

03:36.940 --> 03:37.990
I'm going to select that.

03:39.370 --> 03:40.420
You go to this method.

03:40.750 --> 03:43.990
This method takes an input called comparator.

03:44.110 --> 03:50.980
But if you take a look at it, this is a default keyword which I was referring to in our presentation,

03:50.980 --> 03:55.510
and this default keyword got introduced as part of Java 1.8.

03:56.050 --> 03:56.800
We take a look at it.

03:56.800 --> 03:59.800
Interfaces now have a complete method.

03:59.800 --> 04:02.770
Implementation was never possible before Java eight.

04:02.800 --> 04:06.030
You can take a look at some of the prior methods in the list interface.

04:06.040 --> 04:12.400
This is again another default method which got introduced as part of Java eight in list interface.

04:13.150 --> 04:16.540
And if you take a look at this one, do you see any method implementation here?

04:17.350 --> 04:19.450
This is just declaring the method.

04:19.480 --> 04:24.800
The array list implementation of this list interface will implement the logic behind this.

04:24.850 --> 04:31.780
So now with the introduction of default methods, ideally this is helping the Java creators to introduce

04:31.780 --> 04:39.520
new functionalities into the interfaces so that this interface can evolve according to the technology

04:39.520 --> 04:41.980
advancements and new use cases.

04:42.370 --> 04:47.350
Now what we are going to do, the Sort method takes an input called comparator.

04:47.350 --> 04:53.020
So we are going to create a comparator here, take a look at the comparator, has a comparing method,

04:53.020 --> 04:55.000
but we are not going to use that method.

04:56.080 --> 04:59.380
Comparator are something called the natural order.

04:59.410 --> 05:08.320
The natural order is something which is going to inform the Sort method to sort the result in ascending

05:08.320 --> 05:08.920
order.

05:09.250 --> 05:14.080
Order to make sure this isn't going to impact our sort operation here.

05:14.080 --> 05:15.640
I'm going to comment this code.

05:15.910 --> 05:17.830
Now let's run this and check the result.

05:17.830 --> 05:19.390
I'm going to copy this same.

05:23.720 --> 05:28.970
And then I'm going to give it a different name because we are using the sort method that is part of

05:28.970 --> 05:30.680
the list interface.

05:30.710 --> 05:34.280
Now what I'm going to do, I'm going to run this and check the result.

05:35.650 --> 05:36.160
There you go.

05:36.250 --> 05:36.890
Take a look at it.

05:36.910 --> 05:39.300
It gave you the same result as before.

05:39.310 --> 05:46.270
But if you are using the sort method that is part of the list interface, and this sort method is a

05:46.270 --> 05:50.140
default method that is part of the list interface.

05:51.110 --> 05:51.640
No.

05:51.650 --> 05:54.350
You want to reverse the result?

05:54.380 --> 05:55.430
How do we do it?

05:55.700 --> 06:01.310
So if we take a look at it start method, we are going to use the same start method, but we are going

06:01.310 --> 06:04.050
to change the comparator implementation.

06:04.070 --> 06:06.580
So here we are going to pass a reverse order.

06:06.590 --> 06:11.240
What this is going to do, this is going to reverse the result.

06:14.210 --> 06:15.010
And thus.

06:17.430 --> 06:18.240
Take a look at it.

06:18.390 --> 06:21.840
It actually reversed the result, basically.

06:22.560 --> 06:24.190
This one is in alphabetical order.

06:24.210 --> 06:26.130
This one is in reverse order.

06:27.040 --> 06:33.520
So I would highly recommend you guys to explore the other default method, which is nothing but the

06:33.520 --> 06:34.600
replace all.

06:36.470 --> 06:38.510
Search for default in this one.

06:39.430 --> 06:44.590
Ideally it is giving you six matches, but actually there are three different methods that got introduced

06:44.590 --> 06:47.560
as part of the Java eight.

06:47.590 --> 06:49.520
So this is called Splitter Splitter.

06:50.110 --> 06:53.050
This can be used for parallel programming.

06:53.080 --> 06:55.090
We have seen the parallel streams, right?

06:55.090 --> 06:58.270
If you want to manually do it, then we can use Splitter.

06:58.500 --> 07:04.000
We'll discuss about this in one of the future tutorials, but for now just need to know, okay, these

07:04.000 --> 07:06.340
are the default methods that are part of the.

07:07.670 --> 07:11.750
This interface would highly recommend you guys to explore this.

07:13.740 --> 07:14.730
Go back to the code.

07:15.040 --> 07:15.170
Okay.

07:15.180 --> 07:17.300
With this, we came to the end of this tutorial.

07:17.310 --> 07:21.570
Let's explore another complex example in the next tutorial.

07:21.690 --> 07:22.890
Thank you for watching.
