All language subtitles for 391 hashCode and the operator.en
Afrikaans
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bengali
Bosnian
Bulgarian
Catalan
Cebuano
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Filipino
Finnish
French
Frisian
Galician
Georgian
German
Greek
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Khmer
Korean
Kurdish (Kurmanji)
Kyrgyz
Lao
Latin
Latvian
Lithuanian
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mongolian
Myanmar (Burmese)
Nepali
Norwegian
Pashto
Persian
Polish
Portuguese
Punjabi
Romanian
Russian
Samoan
Scots Gaelic
Serbian
Sesotho
Shona
Sindhi
Sinhala
Slovak
Slovenian
Somali
Spanish
Sundanese
Swahili
Swedish
Tajik
Tamil
Telugu
Thai
Turkish
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Xhosa
Yiddish
Yoruba
Zulu
Odia (Oriya)
Kinyarwanda
Turkmen
Tatar
Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,450 --> 00:00:07,080
In the last video we have written a test to check that job done from map returns a job with the correct
2
00:00:07,080 --> 00:00:07,710
properties.
3
00:00:08,160 --> 00:00:13,800
However when we run this test we got a failure with this error message and the problem here is that
4
00:00:13,800 --> 00:00:20,550
the compiler doesn't know how to compare instances of type job because we haven't provided any quality
5
00:00:20,550 --> 00:00:22,750
operator for our job class.
6
00:00:22,950 --> 00:00:28,980
And because this is a custom class that we have created then we need to override two things in the base
7
00:00:29,070 --> 00:00:35,520
object class called the hash code and the equality operator and these look like this.
8
00:00:35,940 --> 00:00:39,940
We have a hash code and we have an equality operator.
9
00:00:40,500 --> 00:00:42,810
So what are these things exactly.
10
00:00:42,870 --> 00:00:44,490
And how do we use them.
11
00:00:44,490 --> 00:00:50,910
Well the hash code is an integer representation of an object and we can open the documentation to learn
12
00:00:50,910 --> 00:00:52,260
more about it.
13
00:00:52,650 --> 00:00:59,640
And the useful thing to know here is that hash code must be the same for objects that are equal to each
14
00:00:59,640 --> 00:01:03,020
other according to the operator equal.
15
00:01:03,030 --> 00:01:09,300
And the hash code of an object should only change if the object changes in a way that affects equality.
16
00:01:09,770 --> 00:01:10,110
OK.
17
00:01:10,140 --> 00:01:16,410
So this explanation gives us a vague idea about hash code but how should it be implemented.
18
00:01:16,410 --> 00:01:24,030
Well let's get back to our job file and in here we are going to use a special function called hash values
19
00:01:25,590 --> 00:01:32,940
like this and we need to import dark UI to make use of this and to this function we need to pass all
20
00:01:32,940 --> 00:01:35,460
the properties that we have in our job class.
21
00:01:35,490 --> 00:01:42,330
So in this case it's I.D. and name and rate but our like this.
22
00:01:42,330 --> 00:01:48,180
And just to be clear this is the way that hash code is implemented in the classes inside the flatter
23
00:01:48,180 --> 00:01:48,810
SDK.
24
00:01:49,200 --> 00:01:53,440
So I recommend that you use the same convention for your own classes.
25
00:01:53,490 --> 00:01:56,610
Next we need to implement the equality operator.
26
00:01:56,610 --> 00:02:01,260
And what I'm going to do now is to write some code and then I'm going to explain it.
27
00:02:01,260 --> 00:02:02,520
So here I can type.
28
00:02:02,610 --> 00:02:05,720
If and then identical.
29
00:02:05,720 --> 00:02:10,320
And then this with other than return true.
30
00:02:11,520 --> 00:02:22,930
And then if runtime type is not equal to other dot runtime type that return false.
31
00:02:22,950 --> 00:02:32,910
And finally I mean to type final job other job equals other and then return by the equals other job
32
00:02:34,730 --> 00:02:39,870
dot idea and name equals other job.
33
00:02:40,020 --> 00:02:46,540
The name and rate for our equals other job.
34
00:02:46,720 --> 00:02:47,440
Right.
35
00:02:47,580 --> 00:02:49,070
Our like this.
36
00:02:49,860 --> 00:02:55,790
And these is the way that the equality operator is implemented for classes in the the decay.
37
00:02:56,250 --> 00:03:02,970
So the first condition is to check if these two references are in fact the same object in memory.
38
00:03:03,090 --> 00:03:05,660
And in this case we can return true.
39
00:03:05,700 --> 00:03:11,910
And the second track is to verify if the other object is of the same type as the current one.
40
00:03:11,910 --> 00:03:17,530
And this is necessary because the equity the operator could be used to combat objects of different types.
41
00:03:17,670 --> 00:03:20,760
And if this is the case then we return false.
42
00:03:20,760 --> 00:03:27,750
And if we get to this line it means that the other object is of type job so we can assign it to a new
43
00:03:27,750 --> 00:03:29,770
variable of type job.
44
00:03:29,820 --> 00:03:33,180
And finally we return true if all the properties match.
45
00:03:33,540 --> 00:03:37,540
So this is the correct way of implementing the equality operator.
46
00:03:37,830 --> 00:03:44,280
And in general it is good practice to define the hash code and the equality operator for our own classes
47
00:03:44,550 --> 00:03:49,570
because this means that we can compare them directly and now that we have done this.
48
00:03:49,590 --> 00:03:55,310
We can run our test once again and this time we got a success.
49
00:03:55,350 --> 00:04:01,920
And just to be clear what happens when we call expect is that the equality operator is called between
50
00:04:01,920 --> 00:04:08,640
the left and the right hand side arguments and this works because we have implemented the equality operator
51
00:04:08,640 --> 00:04:10,040
for the job class.
52
00:04:10,260 --> 00:04:16,080
Before we move on I want to mention that adding the hash code and the equality operator for our classes
53
00:04:16,290 --> 00:04:22,710
can be a bit tedious because this is a bit of boilerplate code that we need to add for each class.
54
00:04:22,710 --> 00:04:28,980
And on top of that if we add or remove properties from our classes then we have to remember to update
55
00:04:29,010 --> 00:04:35,760
the hash code and the equality operator and especially if we have a lot of model classes that this can
56
00:04:35,760 --> 00:04:38,790
become a lot of work that is error prone.
57
00:04:38,790 --> 00:04:45,570
So one alternative approach to this is to use code generation tools that automate the process of creating
58
00:04:45,600 --> 00:04:47,590
and updating model classes.
59
00:04:47,610 --> 00:04:53,280
However we will not go where coding generation in this course and you can research this topic yourself
60
00:04:53,370 --> 00:04:54,210
if you're interested.
61
00:04:54,840 --> 00:04:57,060
OK so let's continue on the next video.
6366
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.