WEBVTT

00:00.110 --> 00:00.680
Hi everyone.

00:00.680 --> 00:01.280
Welcome back.

00:01.280 --> 00:04.400
In this lecture we will code and learn about pattern matching.

00:04.430 --> 00:08.060
I will start with some theory first and then we'll get to the coding part.

00:08.060 --> 00:11.540
Let's start with why pattern matching in the first place.

00:11.540 --> 00:14.210
Let me explain this using a simple example.

00:14.240 --> 00:18.980
You all might have seen a code like this or wrote one in your experience.

00:18.980 --> 00:24.320
So in this code, what we are doing here is that we check the type, and if the type matches, then

00:24.320 --> 00:28.760
we apply a cost and create a variable and then we act on that variable.

00:28.760 --> 00:33.830
So in this case what we are doing is that if the type is integer, we are building a string with the

00:33.830 --> 00:38.090
cost variable that we created at step number two, and then returning that value.

00:38.090 --> 00:43.280
And the code logic is similar for the next if block, which is if the instance is of type string, then

00:43.280 --> 00:48.710
we cast and create a variable, and then we return a string that represents what is a length of that

00:48.710 --> 00:49.070
string.

00:49.070 --> 00:55.280
And if the type is not string or integer, then we return a result stating not a string or integer.

00:55.280 --> 01:00.310
So this is a very simple and naive example to easily understand what I am trying to convey here.

01:04.090 --> 01:07.540
The one thing that you notice here is that this code is verbose.

01:09.040 --> 01:09.400
Okay.

01:09.430 --> 01:14.890
And another thing is that step one and step two can be combined into one single step.

01:14.890 --> 01:18.520
Because once you match a type, you already know what the type is.

01:18.520 --> 01:26.530
But still in this kind of code, we need to cast it to the type of integer and then create a variable

01:26.530 --> 01:28.330
which is actually redundant.

01:28.330 --> 01:34.090
So this is where pattern matching comes to the rescue to write concise and elegant code.

01:34.120 --> 01:37.810
Let's look at the same example using pattern matching in place.

01:37.840 --> 01:39.880
It is the same example on step one.

01:39.880 --> 01:41.650
What we do is we check the type.

01:41.650 --> 01:45.340
If there is a match, we cast and create a binding variable.

01:45.370 --> 01:51.460
You see in here, if the type is a match then we create a binding variable I over here itself okay.

01:51.490 --> 01:54.370
Which means we can directly act on that variable.

01:54.370 --> 01:57.100
So this is what pattern matching is all about.

01:57.100 --> 02:00.660
It helps the developers to write a more concise and clean code.

02:00.660 --> 02:06.690
It removes that redundant step of again, applying the cast and creating a variable to act on it.

02:06.690 --> 02:08.730
This is what pattern matching is all about.

02:08.730 --> 02:12.270
It helps the developers to write more concise and clean code.

02:12.300 --> 02:16.050
Pattern matching using Instanceof is available from Java 16.

02:16.050 --> 02:21.990
So in this case we are using Instanceof that applies a type check and then it creates a binding variable

02:22.020 --> 02:22.800
to okay.

02:22.830 --> 02:28.380
So this specific type of pattern matching is called a type pattern because we are applying the pattern

02:28.380 --> 02:32.010
on the type of the variable and see if there is a match.

02:32.010 --> 02:35.550
There are other patterns such as record patterns and guarded patterns.

02:35.550 --> 02:40.440
These are introduced as part of Java 21, which we'll talk about in the following lectures.

02:40.440 --> 02:46.470
But in Java 21, they have introduced a new way of applying pattern matching for the type pattern that

02:46.470 --> 02:47.250
we are talking about.

02:47.280 --> 02:51.780
So in this case, for the same logic it can be written in a more concise way.

02:51.780 --> 02:57.030
So let me show you one of my favorite way of applying pattern matching which is pattern matching using

02:57.030 --> 02:57.630
switch.

02:57.630 --> 03:02.000
So in this context, each case statement applies a pattern to match.

03:02.030 --> 03:02.420
Right.

03:02.450 --> 03:06.200
So each case label is going to apply a pattern match.

03:06.230 --> 03:11.870
So in this case if the string matches to the object O that is being passed, then it's going to apply

03:11.870 --> 03:12.440
the pattern.

03:12.440 --> 03:15.050
And then in this case block gets executed.

03:15.080 --> 03:15.500
Okay.

03:15.530 --> 03:18.770
Another nice thing is that we are using lambdas for the case body.

03:18.770 --> 03:23.180
So which is going to simplify your code instead of having this multiple if checks.

03:23.180 --> 03:25.520
So this is basically an enhanced switch.

03:25.520 --> 03:30.140
If we have covered the enhanced switch section already you might be able to understand this code really

03:30.140 --> 03:30.410
well.

03:30.440 --> 03:34.760
Another important thing is that you have a case label to handle null.

03:34.760 --> 03:41.180
So you can have case null as a case label, which is a really nice addition to avoid dealing with the

03:41.180 --> 03:42.320
null pointer exception.

03:42.350 --> 03:45.770
I hope I have provided the necessary information about pattern matching.

03:45.770 --> 03:47.360
Let's explore this in action.

03:47.360 --> 03:53.060
This may be a trivial example, but I have a separate section in which we will build a real time example

03:53.060 --> 03:55.730
using all the concepts that we have learned so far.

03:55.730 --> 04:00.140
With this knowledge in place, Let's go ahead and explore the pattern matching in action.

04:00.320 --> 04:00.800
There you go.

04:00.800 --> 04:02.300
We are back in IntelliJ.

04:02.330 --> 04:08.480
So in here in the Explore latest Java Features project you will notice the pattern matching package.

04:08.480 --> 04:12.770
And in the pattern matching package you will find a class named Pattern Matching example.

04:12.800 --> 04:14.000
Let's go ahead and open this one.

04:14.000 --> 04:17.330
So in this case what we are doing here is that we have a pattern function.

04:17.330 --> 04:20.240
So this is similar to what we saw in the slides.

04:20.270 --> 04:20.660
Right.

04:20.660 --> 04:24.710
So what we are going to do we are going to implement the pattern matching using Instanceof.

04:24.710 --> 04:27.710
So this Instanceof is available from Java 16.

04:27.710 --> 04:32.210
So if you're running in Java 17 you can still use pattern matching using the approach that I'm going

04:32.210 --> 04:33.380
to show you first.

04:33.380 --> 04:38.000
And then we will implement the enhanced switch way of implementing pattern matching.

04:38.000 --> 04:40.520
So in this case let's copy this and put it over here.

04:40.520 --> 04:44.810
So before I go ahead and provide the implementation let's go ahead and rename this one.

04:44.840 --> 04:47.570
It's going to be using Instanceof.

04:47.570 --> 04:50.030
And in here there is a test case already present.

04:50.060 --> 04:51.440
Let's go ahead and open that.

04:51.980 --> 04:52.700
So there you go.

04:52.700 --> 04:53.600
This is a test case.

04:53.600 --> 04:57.220
So in this test case we are still using the parameterized test option.

04:57.220 --> 05:00.760
So what this function does is that it's going to pass the object value.

05:00.760 --> 05:05.290
So in this case the object value is going to be the number one here.

05:05.290 --> 05:06.460
And then we are passing null.

05:06.460 --> 05:10.390
So if the value is null then we are going to expect this value as a result.

05:10.420 --> 05:10.690
Right.

05:10.720 --> 05:12.100
So that's the second argument.

05:12.100 --> 05:14.200
In this case that's the expected result okay.

05:14.230 --> 05:18.790
And in the case of the leap the value is going to be a string of length colon five.

05:18.790 --> 05:21.040
So that's what this particular code logic does.

05:21.070 --> 05:23.980
So in this case string of length which is returning the length okay.

05:24.010 --> 05:29.140
So the next thing is if the type is integer the value that it is going to return is integer colon one.

05:29.140 --> 05:30.490
That's what we have over here.

05:30.490 --> 05:31.420
So now what we will do.

05:31.420 --> 05:34.060
We'll go ahead and write the implementation using Instanceof.

05:34.060 --> 05:39.610
So if the object is of type instanceof then what we can do we can assign a variable over here.

05:39.640 --> 05:41.050
We can remove this okay.

05:41.080 --> 05:43.300
That's the first thing and the same thing over here.

05:43.300 --> 05:45.250
So we can give this value as yes.

05:45.250 --> 05:47.950
And then we can add this value over here.

05:47.950 --> 05:53.230
So basically using Instanceof we have removed one additional step compared to this okay.

05:53.260 --> 05:57.270
So that's the benefit with using pattern matching using Instanceof.

05:57.270 --> 06:00.570
So it creates this binding variable automatically for you.

06:00.600 --> 06:01.710
So now what we will do.

06:01.710 --> 06:03.840
We'll go ahead and write a test case for this one.

06:04.140 --> 06:05.370
Split and move right over here.

06:05.370 --> 06:09.120
So this is where we have the actual code in here we are going to write the test.

06:09.150 --> 06:12.240
So in this case let's go ahead and create a similar test.

06:12.240 --> 06:15.960
Because the input and output is going to be very much the same.

06:15.990 --> 06:19.620
So in this case copy this over here and pass this over here.

06:19.620 --> 06:23.850
So now if I execute this test case, as long as it gives me a green check mark.

06:24.390 --> 06:24.990
There you go.

06:25.020 --> 06:25.830
That's what we got.

06:25.830 --> 06:30.300
So this code works as expected with a pattern matching using Instanceof.

06:30.300 --> 06:34.140
Now the next code is we are going to implement the similar functionality using switch.

06:34.170 --> 06:37.650
Probably this is my favorite way of implementing pattern matching.

06:37.650 --> 06:40.530
So in this case let's copy this and put this over here.

06:40.530 --> 06:45.300
And then I'm going to be naming this function as pattern matching using switch okay.

06:45.330 --> 06:46.800
So let's remove all this code.

06:46.800 --> 06:49.860
So what we are going to do is that we're going to start with the return statement.

06:49.860 --> 06:52.380
Because we know switch is an expression by now.

06:52.380 --> 06:58.070
So in this case we pass it over here, and then what we are going to do is we are going to write case

06:58.070 --> 06:58.550
labels.

06:58.550 --> 07:01.460
So the first thing is we will write a case for string.

07:01.460 --> 07:07.340
So in this case string s if the value is this then we are going to be returning this value.

07:07.490 --> 07:07.940
There you go.

07:07.970 --> 07:10.460
Now let's go ahead and cover the remaining scenarios.

07:10.460 --> 07:14.990
In this case if it's an integer then I'm going to have the binding variable as I.

07:14.990 --> 07:18.290
And then the code logic is going to be integer I.

07:19.160 --> 07:21.200
So in this case let's go ahead and add this one.

07:21.200 --> 07:24.800
And in the case of default we are going to return a different one.

07:24.800 --> 07:29.390
So in this case you see right as soon as I type in the case you have different options available.

07:29.390 --> 07:35.420
So you have a case label that's going to allow you to handle null also, which is a really nice addition

07:35.420 --> 07:36.710
to handling null.

07:36.710 --> 07:38.360
So in this case you do this.

07:38.360 --> 07:43.040
And in the case of this one what we are going to do, we are going to return this value as not a string

07:43.040 --> 07:44.450
or not an integer.

07:44.450 --> 07:47.000
So in this case let's go ahead and add this semicolon over here.

07:47.000 --> 07:52.850
So this is how we implement the pattern matching using the enhanced switch.

07:52.870 --> 07:53.890
So now what we will do.

07:53.890 --> 07:58.360
We'll go ahead and write test cases to test this logic using the enhanced switch.

07:58.390 --> 08:01.600
So in this case I'm going to pretty much use the same test case.

08:01.600 --> 08:05.530
But we are going to be referencing to the new function that we created.

08:05.740 --> 08:08.080
So in this case pattern using switch.

08:08.110 --> 08:10.120
Put it over here and call this one.

08:10.120 --> 08:11.980
So now I'm going to execute the test case.

08:11.980 --> 08:13.930
As long as it gives me a green check.

08:13.960 --> 08:16.090
That means this is working as expected.

08:16.120 --> 08:17.860
Okay let's go ahead and execute this.

08:19.180 --> 08:19.750
There you go.

08:19.750 --> 08:22.450
So we got the green check mark as expected.

08:22.450 --> 08:28.000
That means we have successfully implemented the pattern matching using the enhanced switch.

08:28.150 --> 08:28.510
Okay.

08:28.540 --> 08:34.180
So as far as this tutorial goes or this lecture goes, we have learned about implementing pattern matching

08:34.180 --> 08:37.810
using the instance of and pattern matching using switch.

08:37.810 --> 08:40.420
So in a nutshell this is what pattern matching is.

08:40.420 --> 08:43.630
So this is going to enhance the code readability.

08:43.630 --> 08:49.300
At the same time it is going to allow you to write the concise code compared to what we used to write

08:49.300 --> 08:49.990
over here.

08:50.020 --> 08:51.460
This marks the end of this lecture.

08:51.460 --> 08:52.660
Thank you for watching.
