Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
When we override Object.GetHashCode we should also override Object.Equals just in case two different objects somehow have the same hash code.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
A hash code is only a semi
unique identifier for an object.
0:00
We've tried our best to make the hash
codes unique, but it is still possible for
0:04
two different objects to
have the same hash code.
0:09
Much like my name is Jeremy, but
there are also other people named Jeremy.
0:12
In fact, it's a rather common name.
0:16
If someone called out my
name in a large crowd,
0:18
there'd be a good chance that more
than one person would respond.
0:20
How could they know which person
is the Jeremy they're looking for?
0:23
Well, they'd have to rely
on more than just our name.
0:27
They'd probably need to get a look
at each of us in order to say
0:30
which person they wanted.
0:34
So whenever we override
the GetHashCode method
0:35
we should also override the Equals method.
0:38
The Equals method is called to
determine if two objects should be
0:41
considered equivalent.
0:44
If two objects map to the same hash code,
0:46
the only way to tell them apart
is to check if they're equal.
0:48
Let's override the Equals method here.
0:52
So say public override returns a bool and
0:54
it just takes a type of object.
1:00
The first thing we need to do is to make
sure that this object passed in is of
1:07
type student.
1:11
So we'll attempt to cast it to
a student using the as operator so
1:13
say Student call that
equals obj as Student.
1:19
Now if obj isn't a student,
then this as expression will return null.
1:25
So we can check for that.
1:32
Say if that equals null.
1:33
Then will return false.
1:39
If it isn't null,
then it must have been a Student.
1:44
So we can now check to see if each
of the properties is the same.
1:49
So say, return this.Name == that.Name.
1:53
And, this.GradeLevel
2:01
== that.GradeLevel.
2:05
Now the Equals method will return true
if all of the properties are the same.
2:10
Now that we've overridden the GetHashCode
and Equals methods, let's compile and
2:15
run this to see if we've
achieve the desired effect.
2:19
So now both hashCodes are the same, and
there's only one Joe in the Student set.
2:27
HashSets organize objects
internally by their hashCode.
2:33
Once the hashCode has been computed,
the set can quickly determine whether or
2:37
not the object with that
code is in the set.
2:41
Adding an item to a hash set
isn't necessarily faster than
2:44
adding it to a list.
2:48
However, the hash set is
substantially faster when it comes to
2:49
finding the item again.
2:53
Sets are very fast at determining if
an item is already in the collection.
2:55
We can do this by calling
the contains method.
3:00
So here we could say something like,
3:07
if students.Contains, Joe.
3:11
Then, do something.
3:15
The Contains method is defined
in the I Collection interface.
3:18
So every collection
implements this method.
3:21
Calling Contains on a list requires the
list to look for the item by looking at
3:24
each item one by one from
the beginning of the list to the end.
3:29
Calling Contains on a hash
set on the other hand
3:32
just requires computing
the hashCode of the object.
3:35
I've included more information about
how this works in the teacher's notes.
3:38
Likewise calling Remove on a hash
set is also extremely fast.
3:41
Remove requires first find the item and
then removing it.
3:45
We just learned that hash sets
can quickly find items and
3:49
since hash sets are unordered, there's
no shifting of items in the collection
3:52
after the item has been removed.
3:56
This makes hash sets remove method
substantially faster than lists.
3:58
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up