Hi there. 2
Now let's look at Greece. 3
It will also be doing a demographic. 4
We will have to look at comparable uncompetitive interfaces that are probably popular among interviewers 5
. 6
One question could be what the difference is in between these two interfaces. 7
So this lesson will surely help you answer such questions. 8
It reset as the name implies uses a the data structure. 9
Specifically it uses something called the red black tree. 10
So it is a red black tree. 11
Best implementation of a navigable search interface. 12
OK. 13
And internally it uses a tree map just like the. 14
Set uses it Hashima on tree map stores key value based. 15
But resect has only elements so it will start the elements Eskies while it will store an empty object 16
that is an instance of Object class as the value. 17
Now if you recall Hastert also does the. 18
And if you're interested you can check out the source code of the augmented within that reset Plus we 19
know the preset elements will be unique are not also sorted. 20
And it is unique because it is a set although it is slightly slower than Hasek it still offers fast 21
Look up with a logarithmic complexity for I'd read more on contains methods for Hasek. 22
I recall that these operations were supported in constant time. 23
And here's an example where we are creating a Chrisette offsprings on adding three strings. 24
When we paint them we can see that they are sorted under string. 25
One one one gets printed as the first element which is followed by ABC which is then followed by D on 26
the rest of the lesson. 27
Let's head over to our editor and learn by writing some code. 28
OK here we are in the second more class so we look at this class and set them all on we had this method 29
call us Hassidim. 30
So for them all we are going to have a new method called Greaser's them. 31
OK. 32
And here it is. 33
So in this light we saw how we can create a set of strings. 34
Now let's see how we can create a three set off user defined object which is the book. 35
So in the Hassidim all we looked at this class called book which is defined here at the bottom of the 36
class and it had this state like title author on ear. 37
If you recall. 38
We also wrote a few methods like that to string hash called an equals method which you are needed for 39
Hasek. 40
OK. 41
They were auto generated. 42
So that's up on the cross on we are going to use the same class. 43
Quarte Chrisette also. 44
OK here we are creating four instances of book and just two of them have the same speed. 45
OK. 46
So they are Harry Potter books written by the same author and published in the same year and the third 47
book is valid and the fourth book is effective Djala. 48
Now here we are creating an instance of Chrisette. 49
OK. 50
Undestroyed set needs to store only books. 51
So here we are using the notation the gender X notation on saying that it needs to be every set of books 52
and that reference type is set. 53
The countries that implements a set and we are writing the four books year and we are simply rating 54
on printing the books. 55
And when we pass the book it's going to end with the postering matter. 56
We know that the posting method has been implemented in the book class so it overrides the string and 57
the objects. 58
So let's go ahead and run this. 59
Here it is. 60
So it is throwing an exception. 61
OK this class gasp exception and we'll see what light is being generated. 62
And it says it has something to do with comparable. 63
So when it comes to objects user defined objects we are encountering this problem which was not there 64
in the light which was about three set off strengths. 65
OK. 66
So there there was no exception. 67
But here we are seeing an exception. 68
So it has set the main concern was uniqueness. 69
OK. 70
And we were able to achieve it using hash Gaut unequals method. 71
And they also helped us with the Hoshko method. 72
Also how does it fast lookup bucket 3 set the how an additional requirement which is softening. 73
OK so it needs to be unique it needs to be fast and it needs to also socket. 74
So then Mrs. sautéing. 75
So there is some additional thing that we need to do which means that there needs to be an order among 76
the elements. 77
Right because we need to sort those elements in a certain order. 78
OK. 79
There are two ways to do it. 80
One is called us an eternal ordering which uses an interface called compatable. 81
We need to implement that interface call comparable which is from Damodar like package. 82
And the second approach is to use competitor OK it's also an interface from Dollar Audiard. 83
So keep this in mind. 84
So you have two approaches for this one is using the comparable which we are calling it as nature or 85
ordering. 86
On the other one is using competitor. 87
OK. 88
So you may have some interview questions on that. 89
So pay attention to that. 90
So keep that in mind. 91
Now first let's look at the comparable interface which is a natural auguring. 92
OK. 93
Uncomfortable interface is from the dollar not like package and it has exactly one muttered card compared 94
to OK. 95
Uncompiled boot will tell us where to place the element. 96
So compared to a third basically returns and value and that the anger value can be zero or one or it 97
can be zero or a positive integer or a negative integer. 98
Let's actually go ahead and look at the comparator in the API. 99
So this is the comparable interface from the Java 8 API and it has this method compare to. 100
OK. 101
Now what happens is let's assume that there is already an element in the reset and you want to add a 102
new element. 103
So what research is going to do as Treece it is going to invoke the comparable method on the new element 104
that you want to add and that will pass the existing element. 105
The only element that is there and the reason it will be passed under compared to amateur we'll see 106
whether we are too pleased or element if it returns a zero. 107
It means that the new element is a duplicate and hence will not be added but effort returns a negative 108
integer. 109
It means that a new element has to be placed before the element that is already there. 110
OK before the input element OK which means that it is done the input element. 111
But if it returns a positive integer it means that it is greater than. 112
So it needs to be placed after this element. 113
Now if there are more than one element in that reset then the comparative method will be in bold on 114
the new element that needs to be added but it will be added for each of the. 115
It will be invoked for each of the elements. 116
So that's what is going to happen. 117
And so far one of the elements if Cresap finds that if that element that we want to add is a duplicate 118
then it's going to roll down to zero. 119
It's not going to do any for the comparisons with the subsequent elements. 120
OK but if it returns or minus 1 x and replaces the element right before the existing element which is 121
being compared. 122
But if it returns a positive integer then it means that the newly added element has to be compared with 123
the next element. 124
OK. 125
So that's the comparable method. 126
And you can later when you have time you can also read about it. 127
One thing I want to bring your attention to is this statement here. 128
It says that if compared to Materi dunces zero when you do run ex-drug compared to white returns is 129
zero which means that the new element is a duplicate. 130
So in that case if the sample elements are also compared using equals mentored then equals also should 131
read neutral. 132
So they're saying don't try to have this kind of this kind of behavior. 133
And they are strongly recommending it but it's not mandatory. 134
And this is not the case. 135
So in that case you would have to indicate that whether you are implementing this method compared to 136
. 137
OK. 138
And here you need to have this kind of message. 139
It says that this class has a natural ordering that is inconsistent with equals. 140
So the next question is who is going to implement this comparable interface. 141
So the compatible interface will be implemented by the bookless. 142
OK. 143
So the book Class whose objects will be stored in a preset is going to implement to implement a compatible 144
interface and is going to override the compare to method. 145
So let's go ahead and do that. 146
It's actually done already. 147
So here it is implements comparable. 148
So this is from the law Gelada dot like package uncomparable net that is right here at the bottom. 149
So compared to object is taking an object an instance of Object as input. 150
So this compared to as we mentioned earlier is be really being worked on the new element that is going 151
to be added. 152
So this get Dido's right under title of the new element and we are calling the compare to method on 153
that idea. 154
So that title is a strength. 155
So there is a comparative method in the string class also. 156
So he's being in work and the object that we are passing as the input object since this is an off object 157
type of object class. 158
We don't just get the book and we are crutching. 159
It's vital so that both the titles are being compared. 160
So we are trying to sort by that item. 161
OK. 162
So that's the reason. 163
So let's just go ahead and run this. 164
Now if you see the elements got added on the second book did not get it because it was a duplicate. 165
So the uniqueness was uniqueness is there unmoral where the elements are also sorted. 166
So you can see the last one as a factor of Java then how the parts are done right. 167
OK. 168
Now in the slide we have seen a set of strengths and we did not see any issue with that. 169
And that's because as the string class also implements comparable. 170
Ok that's why we are able to invoke the comparative method on the string over here. 171
So that's why we did not see any issue with that and it's just like compair just like string we might 172
also want to add integers into it. 173
So the integer class which is the boxed but I do also implements the comparable interface. 174
Same also applies with same also goes with other boxes primitives like double float and on. 175
So all of them implement it so that those elements can be stored and treated and there can be are dead 176
. 177
They can be sorted. 178
Now the reason it is qualis natural ordering is because the elements themselves are seeing how they 179
have to be sorted. 180
OK so they are defining the actual sorting logic. 181
So think after you it as just a framework. 182
And it needs to be told how to suck OK. 183
And the first approach. 184
So which is the natural ordering the elements themselves which are going to be stored are seeing how 185
hard they need to be Socky. 186
OK that's why it's called us natural Adoni. 187
OK. 188
Because the sorting criterion is coming from the elements themselves. 189
So that's about nature ordering. 190
Now let's look at the second approach which is to use a competitor which is from the job what are your 191
package and with competitor unlike in the comparable case the sautéing criteria comes externally. 192
So the book objects will not define this art incredibly I forgot. 193
OK. 194
So let's also look at that not before doing that. 195
Let me just mention one more thing. 196
So the comparable method you know it is returning and which can be zero or it can be negative or positive 197
integer so that negative or positive integer is what is differentiating it from the equals method and 198
has said we looked at the he equals metric. 199
So in this guess since it's more than equal it needs to be more than equal. 200
So the goal is to not just detect uniqueness but also to sort. 201
So that's the reason we how the negative integer on positive integer being returned by this matter. 202
Now let's look at competitor. 203
So here is a competitor method. 204
And here is the compare matter undertakes two objects and it compares them. 205
OK. 206
So we need competitor needs to be implemented separately by a new class and you are going to do that 207
. 208
And it also has some other matters some static models but let's not worry about it. 209
The only abstract matter is the Complera matter. 210
Let's just go ahead and implement that. 211
And like I said earlier even the integer class has the compare to matter. 212
So let's go back here and let's implement a new class. 213
It's called a spiteful competitor which implements a better interface and much control. 214
Oh I do competitor now has to add an unimplemented method and unimplemented methods compare. 215
And it is comparing the two objects on one off the object will be the element that needs to be added 216
the new element and the other object will be an element that is already there. 217
OK. 218
So let's go ahead and use the same logic. 219
Use that title. 220
So you will see object 1. 221
It needs to be Doncaster to book normally to wrap this here. 222
And we need to get by in which are done a string and we can use a compare method of that. 223
So let's do the same thing but it will be. 224
So we are once again comparing that bitless. 225
So we are making use of the Compared to them and then we can actually use a compare to my girdled some 226
of the string class. 227
So that's sorting criteria and so on. 228
And when you do that's just common this out here it's actually not needed. 229
And we need to pass the competitor here. 230
Unique Retallack researched what logic has to be used for sorting since there is no natural ordering 231
anymore because we commentor are comparable. 232
What say I don't know let's go ahead and run this. 233
And as you can see you're looking fine. 234
Now if you do this which means that it is implementing compatible. 235
But we are also passing the competitor. 236
So in this case it will still use this one. 237
It will still use that I do competitor. 238
So the competitor implementation will override the natural ordering. 239
OK so if you put a print statement here you will be able to see that the computer that is being used 240
. 241
OK. 242
Now one advantage of using competitors is that in this case we are passing this particular competitor 243
but let's say if you want to also if you also define the author competitor so you can also sort by alter 244
. 245
So in which case at runtime you can specify which competitor to use if you want a competitor. 246
You'll say that I do compare let's say there is a user interface and the user chooses sort by title 247
. 248
In that case you will run this you will pass this instance to a competitor. 249
But if the user selects sort by author then you will pass on to a competitor or if the user selects 250
sort by publication date then you will write to support a new class for that and your competitor and 251
they will pass on instance of done. 252
So this kind of passing off competitors at runtime. 253
Basically the unpartisan strategy is being Basit at runtime. 254
So this kind of a strategy is going to this kind of an approach is called US strategy partan. 255
So that's a design by the end where the strategy the actual algorithm is being passed at runtime. 256
So in this gives the sorting strategy will be set at runtime. 257
So that's what I'd want is that you have using the competitor interface. 258
Now one other thing I want to tell us this is regarding the mission ordering. 259
So if I call Amandus somebody moiling the competitor and I'm also D-Mo. incompatible. 260
Now we saw that we have this exception. 261
Now this exception is generated when we are adding the first element Ritchson. 262
So that's because the trees are actually ensures that even the first element that is being added has 263
to be a subtype of compatible. 264
And in this case when it was trying because we are we have commented this out. 265
So it was trying to see if it can be Kostikov compatible it could not cost and so it generated that 266
last gasp exception. 267
So that's about it. 268
Let me stop here. 269
So we looked at what competitor uncomfortable and in the next lesson we will also look at the other 270
methods in the unnavigable set interface which And there are few other matters. 271
So they're in the next lesson we will try them out. 272
They are not very commonly used. 273
So let's just keep them as a separate lesson. 274
OK. 275
So that's about it. 276
So just keep in mind that we have embeddable on competitor compatibilism a total ordering and it has 277
only one metor compared to one competitor. 278
How does that compare to mentor underdose competitors from job to deal package. 279
Okay so that's about it. 280
Thank you. 281
And the court is available in the resource section happy coding.