When you had an equals method, you also need to add a hash code method.
I'm going to repeat this one more time, when you add an equals method, you must also add a hash code
and method.
In this lesson, you will learn to write a harsh code method.
First things first inside of your Java Bootcamp resources, open up the folder for this lesson by following
this path.
OK, by default, hash code assigns every object a new integer.
Inside main Java create three new objects of the car class and print their housecoats car, Nissan is
equal to a new car.
Nissan 20 16.
I'll pinotage code.
Print line.
Nissan, Nissan on code.
Copy and paste this twice.
We'll make this one a dodge, change it up where appropriate.
Let's give it a year, twenty, eighteen.
We'll make this one a Toyota.
Now create a copy of the first object and print its housecoat as well, CanI sinta is equal to a copy
of Nissan, Nukaga, Nissan.
And we'll print a tax code as well.
Notice that every single object you create has a different passcode.
That's fine, but what's bad is that objects that are deemed equal have different hash codes.
This is not good.
Equal objects should have the same harsh covid, otherwise you're going to get bugs with hash based
collections like hash map.
Let's see the bug, I'm going to create a hash map, hash map card, double prices.
Is equal to a new object of the Hashmat class that can store card double entries.
And to the Hashmat a minute, every single object except Nisan to.
And the values can be anything, doesn't matter, let's say a price of ten thousand.
So I think we're trying to pass in and end, let's just add a decimal at the end.
OK, 12000.
And 14000.
Now, before I do anything, Nissan, too, and Nissan are equal, you can even test it.
And according to our equals method, they are equal.
So should I expect that printing prices that get Nissan, too, should return the value for the Nissan
Key?
Let's try out.
Silly me, I forgot to print it.
And it returns no, even though Nissan and Nissan, too, are deemed equal.
So why isn't the Hashmat able to retrieve our key?
Hash map sorts keys into buckets.
Hash map uses the hash code to find which bucket the key is in.
And then it can use equals to check for equality.
In our case, it was looking for the object in bucket one eight three seven, whatever else the rest
is.
It couldn't find this bucket, so it returned no.
That's why when you add an equals method, you must always, always add a hash code method that assigns
equal objects the same hash code.
When equal objects have the same harsh code, the Hashmat can easily find the bucket of the key or interested
in.
Luckily, there's a magical method called cash that returns a hash code based on the fields that it
receives and it knows to assign equal object to the same hash code.
So inside Kageyama, we're going to provide our own implementation of hash code, public interest code.
And then a move to return objects, object, hash and add in our fields.
This method is going to essentially use the fields to assign equal objects, the same housecoats and
unequal objects, different housecoats.
You can validate this by commenting the following.
And running the code in main.
And now notice that Nissan and Nissan to have the same code and the unequal objects have different hash
codes.
So now if you try this again, it gets the value for the Nissan Key Perfect's, so why did this work?
Hashmark uses the hash code to find which bucket the key is in.
It was looking for the object and bucket negative six, seven, three, whatever else the rest is.
It found the bucket.
And then it uses equals the check for equality.
So the Take-Home message is, even if your object is equal to one of the Keys, hash map still needs
the hoshko to look for the key.
That's why when we add an equals method, you must also always, always add a hash code method that
matches it.
Let's recap, equal objects must share the same hash code when you add an equals method, you must also
add a hash code method that assigns equal object to the same hash code.
That's because Hashmark uses the hoshko to find which bucket the key in.