Equatable, Comparable and Hashable Protocols in Swift

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to the soup tutorial and in this we are going to talk about the comparison of two custom objects of class for a structure so we have integers we have doubles we have strings and we can easily compare them with double equals to greater than less than less than equals to nor equals to anything like that so we are not able to use these operators with our custom objects let's say I'm creating a class of student and I'm gonna give a name man and ID okay and just make a constructor which has a name and ID in it and self dot name equals to named self dot ID equals to ID okay so I will making two objects s1 student let's say the name is Rajan the IDS 1 and s2 let's say the name is ROM and ID is 2 okay so if I'm using s1 double equals to s 2 and if I want to check the Equality of the two operators s1 double equals to s 2 so it will not work probably and we'll throw an error that the double equals to operator cannot be used with the student operands because the student operand has nothing in it which can lead to a comparison whereas two different type properties and there is no way we can compare it so for this we have three protocols available we can use any of them and with the help of them we can easily make our custom classes which can easily make use of the binary operators okay so let's start with equitable first how we can use equitable and compare the two specific objects so let us start with equitable how equitable works so let us confirm our class with equitable and see what happens let us run it so it's saying that the protocol equitable as the student does not conform to protocol akuto because we might have missed some function which needs a definition so let us go into the equitable class the protocol sorry and see what function its it has so it has a function double equals to just like operator overloading so basically we have to define the double equals to you know what is actual definition of double equals to and what are the RHS and Alexis values to be compared with so let's define that as well so I'm going to add the stub so here I'm going to write this beneath the constructor okay so I will be doing something like return LHS dot name double equal to RHS dot name and LHS got ID equal to RHS dot ID so it's saying that the name and ID are both compared with the two of the two objects on the right hand side and the left hand side and if and only if both the objects are similar of the two both the properties of the objects are similar then only it will return true otherwise it will not now as soon as I described the double equals to operator you can see the error goes away and I can easily compare the objects without any errors currently it's coming in else block you can see let's say s1 not equals to that's two and it will print definitely s 1 not equal stress 2 because as soon as Rajan as named andromus s 2 in s 2 has the RAM as name and s 1 has ID 1 and s 2 is ID 2 so let us make the name and the ID seen just to make sure that our equal to operator so what nobody codes to operator 1 so yes it's working the binary operator double equals 2 is working so now we can compare the objects of my student class as well using the binary operator double equals 2 which will yield a true or false result so this means that I can also add s1 and s2 in a set and before that I couldn't because set is something which needs something to calculate whether the objects are same or different right so that should be confirmed to equitable okay so for the first part equitable we did some experimenting and we are successfully through to the double equals to thing we provided our definition and we use that double equals to operator with the two objects of student class fine now let us suppose I want to check the greater than in less than operator as well so how can I make use of that suppose if s1 is it greater than s 2 now if I run this it will say we cannot apply the greater than operator because equatorial protocol only has a single function double equals 2 it does not have a greater than or less than or greater than equals to less than operator so for that instead of compare equal table we will use comparable so switching to comparable and what is comparable comparable is nothing but a protocol which is again conforming an equitable protocol so we have to keep this as well because it is a need of equitable protocol but comparable has its own stops less than less than equals to greater than and greater than equals two so now we have to define the operation for greater than or less than so I will be just you know adding as protocol stuff and I will be just using that for less than operator and you will see the magic what happens so let us make a less than operator and in this case let us you know compare the two objects on the basis of ID suppose if a legislature in our H s dot ID so your s1 is less than s2 so this will pass so in this case s so on has ID 1 and let us make the ID 2 here irrespective of name and let us print what happens s 1 is greater than s2 else so I Oh write it less than sign so I will use this as so on is less than s2 and the else block s 1 is greater than s 2 for the time being I will comment this code out fine now let us run the object [Music] okay so it's printing that as zone is less than s2 which it should because the ID of s1 is less than the ID of s2 and what I have written in the definition is the same as soon is the LHS and s2 is the RHS so it will succeed in a true since one which is s 1 so s 1 dot ID means 1 is less than s 2 dot ID which is true so yes the definition is working fine worth less than also now let us implement greater than operator whether it will work or not so I think it will also work you can see that as so on is it's printing that s 1 is greater than s2 but it should not actually because s1 is less than s2 so what will happen here since you know it's a else block and we are not handling any situation so we have two methods here so let us write the complete closure the complete if as thing to make sure whether the statements are fine so first I'll write 4 equal to there right for let us say greater than s 1 is greater than s 2 and finally I'll write else and the third condition is s1 is less than s2 okay I'm going to remove this code and let me run this so what happens so s1 is less than s2 it comes since the ID of s1 is less than s2 let me decrease this and increase s2 to s1 so it will come that s 1 is greater than s2 so you can see that even the greater than check is also working now LHS means s1 has ID 2 and s2 has ID 1 and it is successfully working for the greater than check as well since s 1 should be greater since it has I so you can see that we have to implement just two operators any of the two operators not any of the basically we have to implement equals 2w question we have to implement either the less than or the greater than and it will automatically be functioning as per the it for the other operators okay so let us also write something like let us comment all this and let us check for greater than or less then equals to s right so s 1 greater than equals to s 2 so let me write that s 1 is greater than equals to s 2 else s 1 is less than a strictly so let us check for greater than equals to s true it means that it will certainly pass with both the checks Creator them as well as equals to so obviously we are first checking for greater than since the ID of s 1 is greater LHS you get so yes we are getting into it now let us make both the properties the name is already common and the IDS should be the same as well to check whether we are going getting into this block as well so again yes we are coming into this block so this means that greater than equals to operator is also working fine let us change the values so s 2 will be now greater so this check will fail and we will directly come to s1 is less than s2 so perfect so why implementing just two operators we have actually implemented five other operators like less than equals 2 greater than equals 2 less than so in total we have implemented five operators obviously we have also implemented the not equals to operator s1 not equals to s 2 and we can certainly check I'm going to comment that and print as so not equals to s 2 so here it will print s 1 or equals to s 2 because they are not equal let us make them equal so it is not printing anything basically in the else block it should be that's one double equals to Esther for the time being I'm assuming that if it's not equal then it's equal yes they are equal okay so by just using these operators define these operators we have already defined automatically what less then equals to greater than greater than equals two and naught equals two so it's so cool that just providing the definition of two we have provided the definition of four other operators as well and now our class student will know all the objects of the student class we'll use the boolean operator and can perform the comparisons very easily that's great now come to the third part hash table this is somewhat very interesting so I'm going to delete everything I will be keeping just the objects okay and let us get let's get rid of this less than operator as well now hashable hash okay so hash oil is what hash table is another protocol which is conforming to equitable so if it is conforming to it equitable then we have to again provide a definition for double equals to which we are doing okay now what else hash table same as Shiva says that it provides a hash value which is an integer and nothing else so it can be a very long integer and this hash value is you know very helpful in knowing exactly whether the two instances are same or not so you can see that this hash value which you used to write earlier is now deprecated and instead they are provided a function haha sure so we will be implementing that function so let us start with the function so it's saying that it is not conforming to protocol hashable the reason is that we are not implementing this function so let us implement this function okay yes and the error is gone now you can see that we have a student class which is conforming to hashable and we have double equals to function because since hashable is conforming to equitable and we need this definition and we need a definition for this hash function as well so for the time being I am leaving this hash function empty and now I'm going to print the hash value of both the objects s1 and s2 and let us see what happens you can see the hash values are same now let us modify the object and make it 2 for the ID of s2 and also the name so in that case also you can see the hash value same so how it is helpful it's not doing anything it's giving the same hash value again and again because we haven't provided any definition to the hash function here so we have to provide we have to provide the combine to the hasher for which properties which we are interested in comparing so hash dot combined cheshire sorry asher or combine and name dot hash value we are if we are taking the name into the consideration and I read out hash value and now let us run the application so you can see the hash values are different now let me make the ID same and name different and let me see what happens again the hash values are different let me make the name and ID same for s1 and s2 and now you will see the hash values are exactly the same for both the objects so this is the way how hash value works you have to provide the combination here along with the hash are in which values you are interested now for example I am commenting this ID and I'm changing this ID to 10 for example and I am running then also it will provide the same hash value because it is just checking with the name now name should be equivalent this will guarantee the same hash value okay so we don't want to keep it like that and we want to add the ID as well okay so it's it will be different since the ID is 10 and now again running the result will be same in this case the hash shudders will be same okay so now a very important note regarding hash table if the two instances are saying the objects basically if the two objects are seen then the hash value is always same this is guaranteed but the vice versa is false means if the hash values are same it is not necessary that the objects are same so you gated why why this line is the first line I'm assuming you get it because since we are using this hash function and we are making use of the name and ID hash values so it is assuring that the if the objects properties and IDs the basically the properties of the objects are same and if we have defined this in the function then it will guarantee the same hash value which it is doing so this line is perfectly fine now comes the second part so it means that let us suppose I am using I'm making different objects let's say I change this into ID and I run the project and you can see that we have different hash value so hash values are determined dynamically so there might be a possibility which is actually very rare but there might be a possibility that that two hash values can be same for s1 and s2 although this point is zero zero one percent that this possibility can be true since you know dynamically both of the objects are assigned to the same hash value but it can be it can be possible so this line states exactly the same thing that vice-a-versa is false and the hash if the hash values are same means if this these are same so it is not guaranteed that the objects are seen as shown in a stream so we have to take the first obviously if the two objects are same then we can say that the hash values are same so better to use the double equals to operator in that case to you know check the Equality of the operators rather than a hash value so you should avoid this hash thing as far as possible but obviously you can use it since I am again saying that it's very super rare that the two hash values can be same for the two different objects but yeah they can be there is a possibility so it's up to you guys you can use hash values it can be different it is actually different every time you know if I will run this project for thousands of time but yeah you know who knows that whether you know dynamically the same actually is assigned to two different objects so it's better to use equals to operator because it guarantees the equality of the objects and this you know can deceive you for a moment maybe for a single time in LAC or roles over time so again it's your choice but you know we can make use of hash values like this so hashable protocol is all about the hash values and obviously it is conforming to equitable so we can obviously write this double equals to operator and we can perform the Equality check in that case so this is all about hash table equitable and comparable and all these things can also be used with structs and it will ill the same thing okay trying to write struct here my playground might be hand it's not responding but overall you got the idea if i just rename that to strut then also it will work let me just wait for a second and check for the structs as well okay now it's working struct and obviously we are using struct then we have to make that as we're since it's a value type and I am modifying the properties and these should be we're always okay and yes it will be working same for the structs and I'm going to make the objects same and let's check the hash values and yes they are the same so it doesn't matter it's a class or a struct so all these protocols equitable comparable and hash table will work for structs as well our enum says well okay this is all about the three important protocols which we can use to modify our code and we can use to you know compare the two different objects of different of our custom class or structure so thank you thank you so much for watching and do leave your comments if you know you find anything which is not you don't catch that thing or maybe there is improvement if you want in that ok so please please do watch and do let me know thank you so much
Info
Channel: Rajan Maheshwari
Views: 1,386
Rating: undefined out of 5
Keywords: Swift, iOS, Hashable, Equatable, Comparable, Protocols
Id: 5zfhRnketTM
Channel Id: undefined
Length: 23min 21sec (1401 seconds)
Published: Thu Jan 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.