Let's circle back to the Quidditch project.
I ended the last Quidditch lesson by saying that for Hashmark to retrieve the key, it's not enough
for an object to be equal to it.
It must also share the same hash code.
Hash map uses the hash code to find which bucket the key is in.
And after it finds the bucket it can use equals to compare your object against the key.
So when you add an equals method, you must always add a hash code method that assigns equal object
to the same hash code.
If you don't do that, you're going to get bugs.
So here in Team Not Java, since we have an equals method, we must also add a hash code method, public
and tache code.
And here we can use the hash method to assign an object to hash code based on its fields'.
In this case, house.
Keeper seeker.
And from experience, the hash method doesn't work with a race, so pass the string representation of
the array, which is the same thing because equal objects are going to end up having the same Chaser's
string.
Ultimately, this hash method is going to assign equal object to the same hash code.
And now I'm confident that our code should work.
And it does work get score returns, the score for Gryffindor, the parameter we're passing in is equal
to the key that we're interested in, which means they have the same hash code.
We can test set score as well.
And it works at Update's Gryffindor score to 10, the hash map was able to find a key that matches the
hash code and a quality of the one we're passing in.
So it knows that we're trying to update this particular key.
So I really want to stress this.
When you add an equals method, you must always, always add a hash code method.
Otherwise you're going to get bugs from hash based collections.