Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,690 --> 00:00:03,810
When you override equals, you need to always override harsh code.
2
00:00:05,890 --> 00:00:09,700
Objects that are deemed equal must always share the same hash code.
3
00:00:11,960 --> 00:00:17,180
So when you override equals, you need to always override a matching hash code method.
4
00:00:21,080 --> 00:00:23,480
In this lesson, you're going to override the harsh code method.
5
00:00:26,150 --> 00:00:31,160
As we continue this lesson, I really want you to keep this in mind, objects that are deemed equal
6
00:00:31,160 --> 00:00:33,410
must always share the same hash code.
7
00:00:33,710 --> 00:00:35,960
If you break this rule, you're going to get bugs.
8
00:00:38,560 --> 00:00:43,870
OK, inside person, not Java, highlight the equals method and commented out press command slightly
9
00:00:43,870 --> 00:00:46,420
for using a Mac or control for Windows.
10
00:00:48,780 --> 00:00:52,320
And now inside Maine, we're going to set the two variables directly equal to each other.
11
00:00:55,720 --> 00:00:57,340
First, we're going to test for equality.
12
00:01:04,129 --> 00:01:06,470
And then we're going to print the hash code of each object.
13
00:01:23,520 --> 00:01:28,860
OK, now we're on the debugger, both variables, a reference that points to the same object.
14
00:01:35,220 --> 00:01:40,190
And the equals method that every class inherits by default, it compares the reference of each object,
15
00:01:40,500 --> 00:01:42,030
so naturally it's going to return.
16
00:01:42,030 --> 00:01:42,390
True.
17
00:01:47,480 --> 00:01:53,900
And the default passcode method returns system DOT, identity hash code, this system, dot identity
18
00:01:53,900 --> 00:01:59,930
hash code, it takes the object, this the current object and it returns a number that never changes
19
00:01:59,930 --> 00:02:01,740
during that object's lifetime.
20
00:02:02,300 --> 00:02:08,720
So because those two variables share a reference to the exact same object, then the hash code method
21
00:02:08,720 --> 00:02:10,699
is going to return the same number twice.
22
00:02:17,350 --> 00:02:20,860
OK, now we're going to set the variable equal to a new copy of the first object.
23
00:02:25,810 --> 00:02:30,970
Run the debugger, each variable story is a unique reference that points to a new object.
24
00:02:38,670 --> 00:02:44,490
The default equals method that every class inherits, it compares each reference there are not equals
25
00:02:44,490 --> 00:02:45,570
or returns false.
26
00:02:52,380 --> 00:02:56,340
And it seems the high school method returns a different hash code for each object.
27
00:02:56,520 --> 00:02:57,710
OK, that makes sense.
28
00:02:59,960 --> 00:03:05,900
Before we keep going, remember this rule, the equality contract states that objects deemed equal must
29
00:03:05,900 --> 00:03:07,760
always share the same hash code.
30
00:03:09,860 --> 00:03:14,480
So what happens when we override the equals method without overriding the hash code method?
31
00:03:17,120 --> 00:03:17,930
Uncommented.
32
00:03:22,260 --> 00:03:24,510
Set the two objects directly equal to each other.
33
00:03:25,020 --> 00:03:26,130
Now run the debugger.
34
00:03:37,970 --> 00:03:43,220
Both variables share a reference that points to the same object and the equals method that we override
35
00:03:43,220 --> 00:03:47,390
returns true because well, obviously the two variables point to the exact same object.
36
00:03:47,420 --> 00:03:48,090
So that's fine.
37
00:03:48,770 --> 00:03:53,900
Now, Hoshko, to remember that system, that identity has code takes the current object that's calling
38
00:03:53,900 --> 00:03:59,540
this method this, and it returns a number that never changes during that object's lifetime.
39
00:04:00,050 --> 00:04:02,840
Both variables point to the exact same object.
40
00:04:03,110 --> 00:04:06,080
So the hash code method is going to return the same number twice.
41
00:04:09,130 --> 00:04:13,150
So far, we haven't broken this rule, we haven't broken the equality contract.
42
00:04:15,480 --> 00:04:18,750
The objects are deemed equal and they share the same hash code.
43
00:04:19,680 --> 00:04:23,310
Now, what happens if I set the variable equal to a new copy of the object?
44
00:04:26,920 --> 00:04:31,810
Run the debugger, each variable store is a unique reference that points to a new object.
45
00:04:39,660 --> 00:04:42,810
And our equals method compares the fields for each object.
46
00:04:50,890 --> 00:04:55,720
But now the high skilled method, it gets, the current object that calls it and it returns a number
47
00:04:55,720 --> 00:05:01,210
and identity has good, that never changes for the objects entire lifetime, and each variable points
48
00:05:01,210 --> 00:05:02,370
to a different object.
49
00:05:04,590 --> 00:05:07,740
So that gives one object a housecoat, a few for whatever.
50
00:05:10,700 --> 00:05:17,010
It gives another object a harsh code of seven zero eight, et cetera, and that's not good.
51
00:05:17,030 --> 00:05:18,050
We broke the rule.
52
00:05:18,050 --> 00:05:19,760
We broke the equality contract.
53
00:05:22,040 --> 00:05:27,080
Objects that are deemed equal must always share the same hash code, otherwise you're going to get a
54
00:05:27,080 --> 00:05:30,420
lot of bugs, as you saw in the hash map part of this course.
55
00:05:30,770 --> 00:05:36,230
So what's the solution when you override equals just always remember to override hash code.
56
00:05:36,500 --> 00:05:38,620
If you do that, you're going to get peace of mind.
57
00:05:46,920 --> 00:05:52,380
So the hash good method that we overrode, it needs to assign equal objects, the same hash code, we're
58
00:05:52,380 --> 00:05:55,290
going to use the familiar objects that hash method.
59
00:06:06,280 --> 00:06:11,350
And what that's going to do is assign equal objects, objects that have similar fields, the same house
60
00:06:11,350 --> 00:06:11,680
code.
61
00:06:16,960 --> 00:06:19,300
And inside, you have to add in your fields, of course.
62
00:06:28,080 --> 00:06:29,710
And now if you rerun the debugger.
63
00:06:33,920 --> 00:06:36,650
The two objects are equal according to the equals method.
64
00:06:39,800 --> 00:06:40,850
Will step inside.
65
00:06:44,130 --> 00:06:46,620
And it calls the harsh code method we overrode.
66
00:06:48,780 --> 00:06:51,870
The method returns a number based on the objects fields.
67
00:07:02,790 --> 00:07:07,860
The second object has the exact same field, so the hash function is going to return the same number.
68
00:07:11,350 --> 00:07:16,240
And beautiful, now we're good, equal objects share the same housecoat, and that's how it should be.
69
00:07:19,600 --> 00:07:22,300
Let's recap, you overrode the hash code method.
70
00:07:24,730 --> 00:07:31,000
Objects that are deemed equal must also share the same hash code, so when you override equals always
71
00:07:31,000 --> 00:07:35,410
remember to override hash code, otherwise you're going to get bugs.
7351
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.