Look at what is the amount we are going to demonstrate that you need to be very careful when using multiple 2
keys that is the objects that you use for keys when they are immutable. 3
You need to be very careful and that's what we're going to demonstrate. 4
And this particular Demel so far is amount a new method called immutable Keys has been added to the 5
map demo class. 6
So let's just go ahead and file this. 7
So the key has to be a mutable object. 8
So let's just go ahead and create an analysis for that. 9
So it's not a list off and Degesch. 10
So we have added this element 1. 11
Now let's create a hash map. 12
The key would be list of indigenous unvalued it's just habit as indigo it doesn't matter. 13
Now let's just our just do this. 14
So we have added the list that we created here. 15
The mutable list. 16
So now let's go ahead and I add a new element to the list. 17
So we already added it to to the other to the map after adding it we are changing the state we are in 18
or we are mutating it and we are adding more elements. 19
So let's just add one more element to. 20
Now let's say map Daut get. 21
OK let's do a search now and search by list. 22
Now if you do this you would expect that it will return this value 1. 23
But as you can see through it and send null value. 24
And that's because so then we you know when we when it inserted the list here. 25
It involved the hash map has worked the hash code method for inserting this mapping. 26
So I applied the hash code method on the key. 27
We know that it applies that and when it applied you know the hash code met third returned a particular 28
Hoshko. 29
OK but after that was added. 30
So we are adding a new element here to the list and then when we're in working this search method and 31
when you're run we are passing the key. 32
The hash map will once again invoke the hash code matter. 33
And when it is working here for the second time the hash code that is returned is different from the 34
hash code that is written. 35
Initially when we were inserting the image that is because it is dependent on the state of this object 36
. 37
OK so this other list actually or writes the hash code method in in the Object class. 38
OK. 39
So let's actually go ahead and look at it. 40
So let's just let me just do an EF 3 here. 41
So here as part of this glass and the Attila's class actually extends obstructionist. 42
So that's where the Hoshko matter is is all and here it is. 43
So here is the hash code matter. 44
So this is a list hardest and the hash code the way it is computing is. 45
So it is iterating through each element and it is computing the hash code. 46
OK. 47
So initially when it added up it is when it inserted the list which had only one element. 48
So it had you know you did it at once. 49
And so there was a certain Hoshko that got generated which was written and based on that the hash map 50
has inserted the mapping. 51
Now after that we inserted the second element and then when we were searching this method is called 52
Once again but this time it is going to do it twice. 53
So the hash code would be different would be different. 54
OK. 55
So it is using the elements off the list to compute the hash code. 56
OK so that's the reason that it gets a different dashboard and the hash function well hash to a different 57
bucket cancer treatment and null value here. 58
So if the hash card method if we are overloading the hash code method and if our hash card method is 59
dependent on the state of the the state then we need to be very careful. 60
If you if we have mutated state after inserting it after inserting it into the map. 61
Now this also applies to the equals method also because the equals method is used when two elements 62
you know read more than one element gets added into the same bucket. 63
So the class matter is used in the linked list. 64
The search for a particular mapping. 65
So if they close by their results are dependent on the objects date. 66
Then we need to be very careful. 67
OK. 68
So we need to be very careful when using mutated objects. 69
But if we do not write any hash code or equals matter if you are not overwriting the methods then we 70
may be fine. 71
OK. 72
So let's also look at that for instance here. 73
I created a custom class called student which is ID or name on the corresponding getters and setters 74
. 75
And here it is the student also has the constructor for initializing the state. 76
So let's go ahead and just add some more code here. 77
It's just a student 78
search the ID and name for name that's passed null. 79
And let me create a map of a student. 80
So student object is the key and it's just how integer doesn't matter. 81
It's called a map to 82
some random one. 83
So we added the student object you're not there to change the state. 84
It's just a set name 85
. 86
So here the object has been notated. 87
And here would be Map do not get s 88
neither does it are turning this round you run. 89
OK. 90
So it's not good none. 91
In this case that's because you know the student matter the Student class did not override either hash 92
code or the equals method. 93
So it is using just the just the default method from the Object class. 94
Both equals as well as hash. 95
And they just use the odd object identity which is the memory address. 96
OK so typically that is what it does is what those methods use. 97
And the object memory address remains the same. 98
So those methods of doing nothing with with the object state. 99
And so it would return the same value over here and also over here the hash code method would read on 100
the same value at both these places. 101
OK. 102
So that's the reason why it was able to extract the right value even though the object got muted. 103
So this is just to demonstrate that you don't need to be very careful when using mutated objects as 104
keys. 105
So typically you would use on the immutable objects. 106
So that's about it. 107
Thank you and you can find this court in the resources section. 108
Happy coding.