Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,980 --> 00:00:04,400
When you had an equals method, you also need to add a hash code method.
2
00:00:05,930 --> 00:00:11,370
I'm going to repeat this one more time, when you add an equals method, you must also add a hash code
3
00:00:11,370 --> 00:00:11,930
and method.
4
00:00:15,970 --> 00:00:18,250
In this lesson, you will learn to write a harsh code method.
5
00:00:23,070 --> 00:00:28,110
First things first inside of your Java Bootcamp resources, open up the folder for this lesson by following
6
00:00:28,110 --> 00:00:28,830
this path.
7
00:00:34,750 --> 00:00:38,980
OK, by default, hash code assigns every object a new integer.
8
00:00:43,550 --> 00:00:49,760
Inside main Java create three new objects of the car class and print their housecoats car, Nissan is
9
00:00:49,760 --> 00:00:50,620
equal to a new car.
10
00:00:50,660 --> 00:00:52,130
Nissan 20 16.
11
00:00:55,100 --> 00:00:56,240
I'll pinotage code.
12
00:00:57,610 --> 00:00:58,240
Print line.
13
00:01:00,810 --> 00:01:02,700
Nissan, Nissan on code.
14
00:01:11,530 --> 00:01:13,000
Copy and paste this twice.
15
00:01:15,350 --> 00:01:19,010
We'll make this one a dodge, change it up where appropriate.
16
00:01:22,540 --> 00:01:24,100
Let's give it a year, twenty, eighteen.
17
00:01:27,210 --> 00:01:28,560
We'll make this one a Toyota.
18
00:01:36,240 --> 00:01:42,180
Now create a copy of the first object and print its housecoat as well, CanI sinta is equal to a copy
19
00:01:42,180 --> 00:01:44,010
of Nissan, Nukaga, Nissan.
20
00:01:45,940 --> 00:01:47,460
And we'll print a tax code as well.
21
00:02:05,090 --> 00:02:08,539
Notice that every single object you create has a different passcode.
22
00:02:10,020 --> 00:02:15,300
That's fine, but what's bad is that objects that are deemed equal have different hash codes.
23
00:02:15,420 --> 00:02:16,380
This is not good.
24
00:02:21,150 --> 00:02:26,460
Equal objects should have the same harsh covid, otherwise you're going to get bugs with hash based
25
00:02:26,460 --> 00:02:28,170
collections like hash map.
26
00:02:30,300 --> 00:02:34,710
Let's see the bug, I'm going to create a hash map, hash map card, double prices.
27
00:02:42,190 --> 00:02:46,450
Is equal to a new object of the Hashmat class that can store card double entries.
28
00:02:53,490 --> 00:02:57,510
And to the Hashmat a minute, every single object except Nisan to.
29
00:03:05,020 --> 00:03:08,320
And the values can be anything, doesn't matter, let's say a price of ten thousand.
30
00:03:16,470 --> 00:03:19,620
So I think we're trying to pass in and end, let's just add a decimal at the end.
31
00:03:28,560 --> 00:03:29,820
OK, 12000.
32
00:03:32,660 --> 00:03:34,040
And 14000.
33
00:03:39,140 --> 00:03:44,810
Now, before I do anything, Nissan, too, and Nissan are equal, you can even test it.
34
00:04:06,470 --> 00:04:09,170
And according to our equals method, they are equal.
35
00:04:13,160 --> 00:04:19,640
So should I expect that printing prices that get Nissan, too, should return the value for the Nissan
36
00:04:19,640 --> 00:04:19,959
Key?
37
00:04:23,050 --> 00:04:23,760
Let's try out.
38
00:04:26,500 --> 00:04:28,210
Silly me, I forgot to print it.
39
00:04:36,550 --> 00:04:40,570
And it returns no, even though Nissan and Nissan, too, are deemed equal.
40
00:04:40,980 --> 00:04:43,710
So why isn't the Hashmat able to retrieve our key?
41
00:04:45,770 --> 00:04:47,930
Hash map sorts keys into buckets.
42
00:04:50,760 --> 00:04:54,520
Hash map uses the hash code to find which bucket the key is in.
43
00:04:56,790 --> 00:04:59,730
And then it can use equals to check for equality.
44
00:05:02,170 --> 00:05:07,210
In our case, it was looking for the object in bucket one eight three seven, whatever else the rest
45
00:05:07,210 --> 00:05:07,510
is.
46
00:05:09,020 --> 00:05:11,510
It couldn't find this bucket, so it returned no.
47
00:05:15,720 --> 00:05:21,900
That's why when you add an equals method, you must always, always add a hash code method that assigns
48
00:05:21,900 --> 00:05:23,880
equal objects the same hash code.
49
00:05:25,370 --> 00:05:31,490
When equal objects have the same harsh code, the Hashmat can easily find the bucket of the key or interested
50
00:05:31,490 --> 00:05:31,730
in.
51
00:05:36,000 --> 00:05:41,010
Luckily, there's a magical method called cash that returns a hash code based on the fields that it
52
00:05:41,010 --> 00:05:45,240
receives and it knows to assign equal object to the same hash code.
53
00:05:47,400 --> 00:05:53,340
So inside Kageyama, we're going to provide our own implementation of hash code, public interest code.
54
00:05:59,810 --> 00:06:03,830
And then a move to return objects, object, hash and add in our fields.
55
00:06:18,720 --> 00:06:24,150
This method is going to essentially use the fields to assign equal objects, the same housecoats and
56
00:06:24,150 --> 00:06:26,310
unequal objects, different housecoats.
57
00:06:29,560 --> 00:06:32,080
You can validate this by commenting the following.
58
00:06:33,810 --> 00:06:35,130
And running the code in main.
59
00:06:38,590 --> 00:06:44,560
And now notice that Nissan and Nissan to have the same code and the unequal objects have different hash
60
00:06:44,560 --> 00:06:45,100
codes.
61
00:06:48,420 --> 00:06:54,120
So now if you try this again, it gets the value for the Nissan Key Perfect's, so why did this work?
62
00:06:56,370 --> 00:07:00,030
Hashmark uses the hash code to find which bucket the key is in.
63
00:07:00,570 --> 00:07:05,290
It was looking for the object and bucket negative six, seven, three, whatever else the rest is.
64
00:07:05,630 --> 00:07:06,810
It found the bucket.
65
00:07:09,130 --> 00:07:11,990
And then it uses equals the check for equality.
66
00:07:12,520 --> 00:07:18,130
So the Take-Home message is, even if your object is equal to one of the Keys, hash map still needs
67
00:07:18,130 --> 00:07:20,000
the hoshko to look for the key.
68
00:07:20,500 --> 00:07:25,660
That's why when we add an equals method, you must also always, always add a hash code method that
69
00:07:25,660 --> 00:07:26,260
matches it.
70
00:07:31,230 --> 00:07:37,740
Let's recap, equal objects must share the same hash code when you add an equals method, you must also
71
00:07:37,740 --> 00:07:41,370
add a hash code method that assigns equal object to the same hash code.
72
00:07:50,940 --> 00:07:55,230
That's because Hashmark uses the hoshko to find which bucket the key in.
6837
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.