Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the default methods that got introduced as part of the list
interface.
For this purpose, what I'm going to do, I'm going to create a package called defaults.
And then the next step is I'm going to create a class called Default Methods example.
The next step is we are going to make this class executable by adding public static void main.
Now the use cases.
Sort the list of names.
Basically, which are nothing but a list of strings.
In alphabetical order.
I'm just keeping this use case really simple.
Now, the first step is we need to create the input list, right?
Let's give a list of string.
And then let's give it a name called String list.
First up is we'll import this.
As part of the Java.util package and we are going to use the arrays class dot as list.
I'm going to give some list of names.
The names are going to be Adam.
The next one is going to be Jenny.
Mike.
Then.
Rick.
So the list is not in.
Dotted order.
Basically, we are going to start this list in alphabetical order.
Let's talk about the prior Java eight way and then we will explore the new Java eight way using the
default method that is part of the list interface itself.
Are some code comments here prior to Java eight.
Now the first purpose.
Let's use a collections class.
That is a way we normally do use a collection class collections class and then call the sort method
and pass this input list as an input.
So this is going to sort the result, basically sort the input in alphabetical order and give you the
result.
What comments here?
Dotted list.
Using.
Elections.
Dot sort method.
Right.
Then let's print the result in English.
There you go.
Now I'm going to run this program.
Run default methods example.
There we go.
Gave you the result.
If you take a look at the result, the result is in sorted order.
We take a look at the Alex.
Alex is at the third position, but here Alex is at the second position.
The whole input list is sorted in ascending order.
Now let's talk about the Java eight approach.
So in Java eight.
The list interface here.
Go to the list interface.
This is an interface.
The list interface has a lot of default methods in it that got introduced as part of Java eight.
And let's take a look at the methods, whatever that got introduced as part of the list interface.
As soon as we dot is going to give you a lot of methods and sort is one of them.
I'm going to select that.
You go to this method.
This method takes an input called comparator.
But if you take a look at it, this is a default keyword which I was referring to in our presentation,
and this default keyword got introduced as part of Java 1.8.
We take a look at it.
Interfaces now have a complete method.
Implementation was never possible before Java eight.
You can take a look at some of the prior methods in the list interface.
This is again another default method which got introduced as part of Java eight in list interface.
And if you take a look at this one, do you see any method implementation here?
This is just declaring the method.
The array list implementation of this list interface will implement the logic behind this.
So now with the introduction of default methods, ideally this is helping the Java creators to introduce
new functionalities into the interfaces so that this interface can evolve according to the technology
advancements and new use cases.
Now what we are going to do, the Sort method takes an input called comparator.
So we are going to create a comparator here, take a look at the comparator, has a comparing method,
but we are not going to use that method.
Comparator are something called the natural order.
The natural order is something which is going to inform the Sort method to sort the result in ascending
order.
Order to make sure this isn't going to impact our sort operation here.
I'm going to comment this code.
Now let's run this and check the result.
I'm going to copy this same.
And then I'm going to give it a different name because we are using the sort method that is part of
the list interface.
Now what I'm going to do, I'm going to run this and check the result.
There you go.
Take a look at it.
It gave you the same result as before.
But if you are using the sort method that is part of the list interface, and this sort method is a
default method that is part of the list interface.
No.
You want to reverse the result?
How do we do it?
So if we take a look at it start method, we are going to use the same start method, but we are going
to change the comparator implementation.
So here we are going to pass a reverse order.
What this is going to do, this is going to reverse the result.
And thus.
Take a look at it.
It actually reversed the result, basically.
This one is in alphabetical order.
This one is in reverse order.
So I would highly recommend you guys to explore the other default method, which is nothing but the
replace all.
Search for default in this one.
Ideally it is giving you six matches, but actually there are three different methods that got introduced
as part of the Java eight.
So this is called Splitter Splitter.
This can be used for parallel programming.
We have seen the parallel streams, right?
If you want to manually do it, then we can use Splitter.
We'll discuss about this in one of the future tutorials, but for now just need to know, okay, these
are the default methods that are part of the.
This interface would highly recommend you guys to explore this.
Go back to the code.
Okay.
With this, we came to the end of this tutorial.
Let's explore another complex example in the next tutorial.
Thank you for watching.