WEBVTT

00:00.050 --> 00:00.620
Hi everyone.

00:00.620 --> 00:01.850
Welcome back in this lecture.

00:01.850 --> 00:06.290
What we are going to learn is that we are going to learn the object equality when you are dealing with

00:06.320 --> 00:07.370
record classes.

00:07.610 --> 00:14.450
Because if you all can recall in the slide I mentioned the record classes by default generate the hashCode

00:14.450 --> 00:15.980
equals toString function.

00:15.980 --> 00:20.450
We have looked into the toString function, but we have not looked into the equality check yet.

00:20.450 --> 00:22.880
So that is what we are going to explore in this one.

00:22.880 --> 00:26.180
So in this case what I'm going to do I'm going to create a test case.

00:26.180 --> 00:29.840
So I'm going to name this one as Create product comparison okay.

00:29.870 --> 00:34.880
So what I'm going to do I'm going to create new product named product one okay.

00:34.910 --> 00:36.890
So if you want to add the type you are welcome to do it.

00:36.890 --> 00:40.010
So what I'm going to do I'm going to add the type as electronics.

00:40.010 --> 00:42.110
And I'll do the same thing over here.

00:43.550 --> 00:45.380
Oops I made a mistake.

00:46.970 --> 00:47.720
There you go.

00:47.750 --> 00:50.330
And we'll do the same thing for the other one too.

00:50.360 --> 00:53.990
So if I do a equality check how is this going to work.

00:53.990 --> 01:00.260
So what I'm going to do, I'm going to be calling assert equals and then pass the product comma product

01:00.260 --> 01:00.740
one.

01:01.010 --> 01:02.840
So we are passing these two products.

01:02.870 --> 01:03.230
Right.

01:03.260 --> 01:09.140
What we are expecting is that each and every property which is iPhone, the cost and the electronics

01:09.140 --> 01:10.940
will be compared against each other.

01:10.940 --> 01:13.450
and that's how it performs a quality check.

01:13.450 --> 01:18.670
But all these functions are automatically created for you behind the scenes when you create a class

01:18.670 --> 01:19.360
with record.

01:19.360 --> 01:22.720
So now if I execute this one, you're going to see a green signal.

01:22.720 --> 01:25.840
This means the product comparison is worked okay.

01:25.870 --> 01:30.910
So in order to break this what I'm going to do I'm going to change this value to electronics one, just

01:30.910 --> 01:33.370
to show you the actual impact of this one.

01:33.370 --> 01:35.680
So in this case let's go ahead and execute this one.

01:35.680 --> 01:37.690
So you'll be able to see the error right.

01:37.690 --> 01:41.440
So the error says expected was electronics one.

01:41.440 --> 01:44.320
But the actual value that is expected is electronics.

01:44.320 --> 01:49.090
So it clearly compares each and every property that is part of the record class.

01:49.120 --> 01:50.620
Now what are the options.

01:50.620 --> 01:53.260
Let's say you don't want to check all the properties.

01:53.260 --> 01:54.550
That's part of the record class.

01:54.550 --> 01:58.270
You want to check only the few properties that are part of the record class.

01:58.270 --> 02:03.460
In this kind of scenarios, we can actually very well override the hashCode and equals function.

02:03.460 --> 02:06.970
So in this case type in equals and press enter.

02:07.000 --> 02:09.850
The IntelliJ has a way to automatically generate this for you.

02:09.850 --> 02:12.760
And then select this option that's been shown to you.

02:12.760 --> 02:15.130
And then press next over here.

02:15.130 --> 02:16.300
And then it's going to check.

02:16.300 --> 02:20.110
Hey these are the different properties that you want to apply equals against.

02:20.110 --> 02:25.070
I'm just going to select as it is, and then we can modify it once we have the implementation ready.

02:25.070 --> 02:30.140
And then it is going to ask you is hashCode also needed for all these different properties.

02:30.140 --> 02:31.430
And then click create.

02:31.430 --> 02:37.040
So this is the functionality that is automatically generated for you by our IntelliJ.

02:37.070 --> 02:38.360
So in this case you can see right.

02:38.360 --> 02:41.330
So we have the product name comparison first.

02:41.330 --> 02:43.280
And we have the cost comparison next.

02:43.280 --> 02:44.900
And then the type comparison.

02:45.230 --> 02:45.530
Okay.

02:45.560 --> 02:50.150
So let's say we decide okay I don't want to have type as part of the equality check.

02:50.150 --> 02:53.870
All you got to do is actually comment out this code in your equals function.

02:53.870 --> 02:58.940
And then if you go and change this one to electronics one, and then if you still try to execute this

02:58.940 --> 03:01.490
particular test case this will still succeed.

03:01.490 --> 03:05.810
The reason being we have removed the type out of the equality check.

03:05.810 --> 03:11.780
So this is how you override the equals and hashcode function in a record class.

03:11.780 --> 03:16.370
So the default behavior is the record classes are going to compare each and every property.

03:16.370 --> 03:22.610
But if you want to override that property or override that functionality you have these options available.

03:22.640 --> 03:23.810
I hope this makes sense.

03:23.840 --> 03:29.060
I hope you all have a pretty good idea about how equality checks work in record classes.

03:29.090 --> 03:30.560
This marks the end of this lecture.

03:30.560 --> 03:31.580
Thank you for watching.
