1

00:00:00,800  -->  00:00:07,260
Look at what is the amount we are going to demonstrate that you need to be very careful when using multiple

2

00:00:07,260  -->  00:00:12,140
keys that is the objects that you use for keys when they are immutable.

3

00:00:12,150  -->  00:00:15,130
You need to be very careful and that's what we're going to demonstrate.

4

00:00:15,150  -->  00:00:21,450
And this particular Demel so far is amount a new method called immutable Keys has been added to the

5

00:00:21,450  -->  00:00:22,980
map demo class.

6

00:00:23,100  -->  00:00:24,820
So let's just go ahead and file this.

7

00:00:24,930  -->  00:00:27,560
So the key has to be a mutable object.

8

00:00:27,810  -->  00:00:34,580
So let's just go ahead and create an analysis for that.

9

00:00:38,670  -->  00:00:42,300
So it's not a list off and Degesch.

10

00:00:44,250  -->  00:00:46,730
So we have added this element 1.

11

00:00:46,770  -->  00:00:50,820
Now let's create a hash map.

12

00:00:51,000  -->  00:01:04,230
The key would be list of indigenous unvalued it's just habit as indigo it doesn't matter.

13

00:01:04,230  -->  00:01:08,720
Now let's just our just do this.

14

00:01:09,540  -->  00:01:11,960
So we have added the list that we created here.

15

00:01:11,970  -->  00:01:13,740
The mutable list.

16

00:01:13,770  -->  00:01:18,080
So now let's go ahead and I add a new element to the list.

17

00:01:18,090  -->  00:01:24,810
So we already added it to to the other to the map after adding it we are changing the state we are in

18

00:01:24,810  -->  00:01:27,380
or we are mutating it and we are adding more elements.

19

00:01:27,390  -->  00:01:30,620
So let's just add one more element to.

20

00:01:31,040  -->  00:01:36,990
Now let's say map Daut get.

21

00:01:37,380  -->  00:01:44,700
OK let's do a search now and search by list.

22

00:01:45,720  -->  00:01:50,290
Now if you do this you would expect that it will return this value 1.

23

00:01:50,680  -->  00:01:55,010
But as you can see through it and send null value.

24

00:01:55,500  -->  00:02:02,580
And that's because so then we you know when we when it inserted the list here.

25

00:02:02,580  -->  00:02:08,630
It involved the hash map has worked the hash code method for inserting this mapping.

26

00:02:08,640  -->  00:02:10,800
So I applied the hash code method on the key.

27

00:02:10,800  -->  00:02:16,860
We know that it applies that and when it applied you know the hash code met third returned a particular

28

00:02:16,860  -->  00:02:17,590
Hoshko.

29

00:02:17,760  -->  00:02:20,810
OK but after that was added.

30

00:02:20,850  -->  00:02:27,240
So we are adding a new element here to the list and then when we're in working this search method and

31

00:02:27,340  -->  00:02:29,430
when you're run we are passing the key.

32

00:02:29,550  -->  00:02:32,890
The hash map will once again invoke the hash code matter.

33

00:02:33,150  -->  00:02:38,580
And when it is working here for the second time the hash code that is returned is different from the

34

00:02:38,580  -->  00:02:40,180
hash code that is written.

35

00:02:40,620  -->  00:02:47,260
Initially when we were inserting the image that is because it is dependent on the state of this object

36

00:02:47,350  -->  00:02:47,530
.

37

00:02:47,660  -->  00:02:54,650
OK so this other list actually or writes the hash code method in in the Object class.

38

00:02:54,650  -->  00:02:54,890
OK.

39

00:02:54,900  -->  00:02:57,170
So let's actually go ahead and look at it.

40

00:02:57,390  -->  00:02:59,940
So let's just let me just do an EF 3 here.

41

00:03:00,780  -->  00:03:06,950
So here as part of this glass and the Attila's class actually extends obstructionist.

42

00:03:07,020  -->  00:03:15,310
So that's where the Hoshko matter is is all and here it is.

43

00:03:15,330  -->  00:03:17,630
So here is the hash code matter.

44

00:03:17,670  -->  00:03:21,940
So this is a list hardest and the hash code the way it is computing is.

45

00:03:22,260  -->  00:03:27,230
So it is iterating through each element and it is computing the hash code.

46

00:03:27,460  -->  00:03:27,810
OK.

47

00:03:27,810  -->  00:03:34,940
So initially when it added up it is when it inserted the list which had only one element.

48

00:03:35,090  -->  00:03:37,680
So it had you know you did it at once.

49

00:03:37,680  -->  00:03:43,830
And so there was a certain Hoshko that got generated which was written and based on that the hash map

50

00:03:43,920  -->  00:03:46,180
has inserted the mapping.

51

00:03:46,200  -->  00:03:51,540
Now after that we inserted the second element and then when we were searching this method is called

52

00:03:51,540  -->  00:03:54,990
Once again but this time it is going to do it twice.

53

00:03:55,070  -->  00:03:57,640
So the hash code would be different would be different.

54

00:03:57,660  -->  00:03:57,960
OK.

55

00:03:57,960  -->  00:04:02,510
So it is using the elements off the list to compute the hash code.

56

00:04:02,600  -->  00:04:08,940
OK so that's the reason that it gets a different dashboard and the hash function well hash to a different

57

00:04:08,940  -->  00:04:12,140
bucket cancer treatment and null value here.

58

00:04:12,960  -->  00:04:18,780
So if the hash card method if we are overloading the hash code method and if our hash card method is

59

00:04:18,780  -->  00:04:23,910
dependent on the state of the the state then we need to be very careful.

60

00:04:24,060  -->  00:04:29,780
If you if we have mutated state after inserting it after inserting it into the map.

61

00:04:30,090  -->  00:04:38,400
Now this also applies to the equals method also because the equals method is used when two elements

62

00:04:38,530  -->  00:04:42,710
you know read more than one element gets added into the same bucket.

63

00:04:42,810  -->  00:04:45,090
So the class matter is used in the linked list.

64

00:04:45,210  -->  00:04:47,470
The search for a particular mapping.

65

00:04:47,820  -->  00:04:50,560
So if they close by their results are dependent on the objects date.

66

00:04:50,640  -->  00:04:52,660
Then we need to be very careful.

67

00:04:53,100  -->  00:04:53,640
OK.

68

00:04:53,640  -->  00:04:56,820
So we need to be very careful when using mutated objects.

69

00:04:57,060  -->  00:05:03,060
But if we do not write any hash code or equals matter if you are not overwriting the methods then we

70

00:05:03,060  -->  00:05:03,680
may be fine.

71

00:05:03,720  -->  00:05:04,130
OK.

72

00:05:04,170  -->  00:05:07,090
So let's also look at that for instance here.

73

00:05:07,110  -->  00:05:12,660
I created a custom class called student which is ID or name on the corresponding getters and setters

74

00:05:12,670  -->  00:05:12,990
.

75

00:05:13,540  -->  00:05:18,720
And here it is the student also has the constructor for initializing the state.

76

00:05:18,720  -->  00:05:23,030
So let's go ahead and just add some more code here.

77

00:05:23,550  -->  00:05:24,740
It's just a student

78

00:05:31,050  -->  00:05:35,700
search the ID and name for name that's passed null.

79

00:05:37,230  -->  00:05:42,210
And let me create a map of a student.

80

00:05:42,240  -->  00:05:47,290
So student object is the key and it's just how integer doesn't matter.

81

00:05:47,550  -->  00:05:48,660
It's called a map to

82

00:05:57,810  -->  00:05:59,280
some random one.

83

00:05:59,430  -->  00:06:05,330
So we added the student object you're not there to change the state.

84

00:06:05,400  -->  00:06:06,840
It's just a set name

85

00:06:06,840  -->  00:06:12,410
.

86

00:06:13,200  -->  00:06:16,020
So here the object has been notated.

87

00:06:16,660  -->  00:06:21,660
And here would be Map do not get s

88

00:06:25,590  -->  00:06:27,840
neither does it are turning this round you run.

89

00:06:27,960  -->  00:06:28,460
OK.

90

00:06:28,650  -->  00:06:29,870
So it's not good none.

91

00:06:30,090  -->  00:06:36,930
In this case that's because you know the student matter the Student class did not override either hash

92

00:06:36,930  -->  00:06:39,000
code or the equals method.

93

00:06:39,000  -->  00:06:44,290
So it is using just the just the default method from the Object class.

94

00:06:44,310  -->  00:06:46,530
Both equals as well as hash.

95

00:06:46,860  -->  00:06:51,190
And they just use the odd object identity which is the memory address.

96

00:06:51,390  -->  00:06:54,920
OK so typically that is what it does is what those methods use.

97

00:06:55,020  -->  00:06:58,630
And the object memory address remains the same.

98

00:06:58,650  -->  00:07:03,480
So those methods of doing nothing with with the object state.

99

00:07:03,710  -->  00:07:09,330
And so it would return the same value over here and also over here the hash code method would read on

100

00:07:09,330  -->  00:07:12,640
the same value at both these places.

101

00:07:12,690  -->  00:07:13,210
OK.

102

00:07:13,350  -->  00:07:20,340
So that's the reason why it was able to extract the right value even though the object got muted.

103

00:07:20,640  -->  00:07:26,030
So this is just to demonstrate that you don't need to be very careful when using mutated objects as

104

00:07:26,050  -->  00:07:26,820
keys.

105

00:07:26,880  -->  00:07:30,120
So typically you would use on the immutable objects.

106

00:07:30,420  -->  00:07:31,210
So that's about it.

107

00:07:31,260  -->  00:07:34,080
Thank you and you can find this court in the resources section.

108

00:07:34,080  -->  00:07:35,220
Happy coding.
