Hi everyone.
Welcome back in this lecture.
What we are going to learn is that we are going to learn the object equality when you are dealing with
record classes.
Because if you all can recall in the slide I mentioned the record classes by default generate the hashCode
equals toString function.
We have looked into the toString function, but we have not looked into the equality check yet.
So that is what we are going to explore in this one.
So in this case what I'm going to do I'm going to create a test case.
So I'm going to name this one as Create product comparison okay.
So what I'm going to do I'm going to create new product named product one okay.
So if you want to add the type you are welcome to do it.
So what I'm going to do I'm going to add the type as electronics.
And I'll do the same thing over here.
Oops I made a mistake.
There you go.
And we'll do the same thing for the other one too.
So if I do a equality check how is this going to work.
So what I'm going to do, I'm going to be calling assert equals and then pass the product comma product
one.
So we are passing these two products.
Right.
What we are expecting is that each and every property which is iPhone, the cost and the electronics
will be compared against each other.
and that's how it performs a quality check.
But all these functions are automatically created for you behind the scenes when you create a class
with record.
So now if I execute this one, you're going to see a green signal.
This means the product comparison is worked okay.
So in order to break this what I'm going to do I'm going to change this value to electronics one, just
to show you the actual impact of this one.
So in this case let's go ahead and execute this one.
So you'll be able to see the error right.
So the error says expected was electronics one.
But the actual value that is expected is electronics.
So it clearly compares each and every property that is part of the record class.
Now what are the options.
Let's say you don't want to check all the properties.
That's part of the record class.
You want to check only the few properties that are part of the record class.
In this kind of scenarios, we can actually very well override the hashCode and equals function.
So in this case type in equals and press enter.
The IntelliJ has a way to automatically generate this for you.
And then select this option that's been shown to you.
And then press next over here.
And then it's going to check.
Hey these are the different properties that you want to apply equals against.
I'm just going to select as it is, and then we can modify it once we have the implementation ready.
And then it is going to ask you is hashCode also needed for all these different properties.
And then click create.
So this is the functionality that is automatically generated for you by our IntelliJ.
So in this case you can see right.
So we have the product name comparison first.
And we have the cost comparison next.
And then the type comparison.
Okay.
So let's say we decide okay I don't want to have type as part of the equality check.
All you got to do is actually comment out this code in your equals function.
And then if you go and change this one to electronics one, and then if you still try to execute this
particular test case this will still succeed.
The reason being we have removed the type out of the equality check.
So this is how you override the equals and hashcode function in a record class.
So the default behavior is the record classes are going to compare each and every property.
But if you want to override that property or override that functionality you have these options available.
I hope this makes sense.
I hope you all have a pretty good idea about how equality checks work in record classes.
This marks the end of this lecture.
Thank you for watching.