WEBVTT

00:00.460 --> 00:01.030
Hi everyone.

00:01.030 --> 00:02.320
Welcome back to this tutorial.

00:02.320 --> 00:09.520
In this tutorial we will code and explore the orals method, the get method and the orals throw method

00:09.520 --> 00:11.740
that are part of the optional class.

00:11.770 --> 00:15.850
The first step is I'm going to create a class called Optional.

00:17.450 --> 00:18.230
Or else.

00:20.060 --> 00:20.930
Example.

00:22.110 --> 00:23.970
And let's make this class executable.

00:25.030 --> 00:28.090
The adding public static void main method.

00:29.640 --> 00:32.280
So the methods that we are going to explore are.

00:33.830 --> 00:34.700
Arrows.

00:35.710 --> 00:37.810
And then get.

00:38.520 --> 00:40.190
And then else.

00:41.230 --> 00:41.440
So.

00:42.610 --> 00:45.730
The first method that we are going to explore is a orals method.

00:46.270 --> 00:49.090
I'm going to create a method.

00:51.030 --> 00:52.050
Oblique static.

00:52.050 --> 00:54.630
And this method is going to return a string.

00:55.640 --> 00:59.270
And it's going to be optional or else.

01:00.820 --> 01:01.420
Right.

01:01.420 --> 01:03.270
So what is the first step?

01:03.280 --> 01:05.140
We need an optional object.

01:05.140 --> 01:09.850
I'm going to use optional dot optional label.

01:09.850 --> 01:14.530
I'm going to make use of the student database class as part of this tutorial.

01:14.890 --> 01:22.240
Student base dot students supplier dot get this is going to give you a valid student.

01:22.660 --> 01:28.690
So if you go to the students player, this will give you a student who has the name as Adam grade level

01:28.690 --> 01:31.270
as two and the GPA as 3.6.

01:31.270 --> 01:32.620
And the gender is male.

01:32.620 --> 01:36.040
And these are the activities that Adam is participating.

01:37.100 --> 01:41.450
So since we do a lot of nullable, we have seen this in the previous tutorial.

01:41.480 --> 01:50.510
This is going to return you an optional object of type student because the input to the the input to

01:50.510 --> 01:53.810
the of nullable method is a student object.

01:54.650 --> 01:55.790
Let's give it a name.

01:56.210 --> 01:57.050
Student optional.

01:57.050 --> 01:58.520
So there we go.

01:58.520 --> 02:00.620
We have the student optional.

02:01.220 --> 02:06.410
The next thing is we are going to explore the method, right?

02:06.410 --> 02:07.940
So I want the name of the student.

02:07.940 --> 02:10.520
That's the use case, which I'm trying to achieve here.

02:10.520 --> 02:11.660
What would I do?

02:11.930 --> 02:13.010
Optional.

02:14.160 --> 02:15.690
A student optional dot.

02:15.700 --> 02:19.330
I'm going to use a map method which we explored in our first tutorial.

02:19.330 --> 02:23.080
We'll be exploring more on the map in the future tutorial.

02:23.080 --> 02:26.200
For now, we are going to use a map method in the student.

02:26.200 --> 02:27.520
We want the name right?

02:27.520 --> 02:29.560
So we'll give Student Colon.

02:29.560 --> 02:29.860
Colon.

02:29.860 --> 02:35.170
We are going to use the method reference syntax here and then give the get name.

02:35.680 --> 02:42.400
In the place of this, you get access to all these different methods that optional supports.

02:42.430 --> 02:45.160
In here we are going to use the orals.

02:45.160 --> 02:51.160
What I'm going to do, I'm going to return default under what scenario this code will get executed.

02:51.160 --> 02:53.170
I will explain that in a moment.

02:53.860 --> 02:55.540
So this is going to be string.

02:56.930 --> 02:59.990
Name equal to this and return name.

03:02.120 --> 03:07.010
So in here we get the student that is going to map the student name.

03:07.070 --> 03:11.300
So if there is a valid value, then it is going to return the actual name.

03:11.300 --> 03:13.200
Let's run this and check the result.

03:13.220 --> 03:14.690
So what I'm going to do.

03:15.940 --> 03:20.940
I'm going to do System.out.println and then call optional.

03:21.190 --> 03:22.660
Else Right.

03:22.660 --> 03:24.580
Let's run this and check the result.

03:24.610 --> 03:26.650
The result is going to be Adam.

03:26.980 --> 03:28.540
So under what scenarios?

03:28.540 --> 03:29.920
It would return default.

03:30.160 --> 03:32.440
So I'm going to comment this line.

03:34.100 --> 03:36.200
Or I want to come on these two lines.

03:36.380 --> 03:38.540
What I'm going to do, I'm going to do.

03:39.860 --> 03:45.830
Thus an optional dot of nullable and then I'm going to pass null.

03:46.520 --> 03:51.680
If you pass null, we all know this is going to return the optional dot empty.

03:51.710 --> 03:56.120
So it's going to return optional dot empty object.

03:57.120 --> 03:58.290
Basically option empty.

03:59.810 --> 04:00.290
Right.

04:00.290 --> 04:06.440
So when it checks this, it is going to try to map the student object and try to get the name.

04:06.440 --> 04:13.190
But since it is empty, it won't be able to execute this line and then it is going to execute the else

04:13.220 --> 04:15.530
and return you the default as a result.

04:16.100 --> 04:17.720
So I'm going to run this example.

04:18.500 --> 04:19.010
There you go.

04:19.010 --> 04:21.980
We got the default as a result.

04:22.100 --> 04:29.270
So if you want to return a default values in your use cases, if the actual value is null in those kind

04:29.270 --> 04:33.530
of scenarios, you can use the or else method.

04:34.520 --> 04:38.750
Okay, let's go ahead and explore the next one, which is the get.

04:40.410 --> 04:41.820
I'm going to copy the same method.

04:41.820 --> 04:47.610
This method is also going to return you the string, but it is going to be or else get.

04:49.110 --> 04:52.860
And I'm going to use the same code here, whatever we had.

04:54.900 --> 04:57.480
And then what we are going to do.

04:58.930 --> 05:02.140
We're going to do the same logic not changing anything here.

05:05.880 --> 05:12.090
That this is going to be orals get So orals get if you take a look at it, it it accepts a supplier

05:12.090 --> 05:16.200
instead of a simple string, it is going to accept a supplier.

05:16.200 --> 05:20.760
So the supplier we know the syntax for supplier supplier does.

05:20.760 --> 05:22.620
I'm going to use a lambda syntax here.

05:22.620 --> 05:27.300
Supplier doesn't take any input, but it is going to return you an output, right?

05:27.300 --> 05:28.710
That's what supplier is for.

05:28.710 --> 05:36.000
So here I have created the simple lambda implementation of the supplier, which doesn't take any input,

05:36.000 --> 05:38.220
but it is going to return you the output.

05:39.100 --> 05:42.970
So this is all about supplier.

05:43.000 --> 05:46.570
This is all about sorry, optional or else get method.

05:46.630 --> 05:48.880
It's going to return the name as an output.

05:49.900 --> 05:51.820
So go ahead and run this.

05:53.220 --> 05:55.210
I'm going to name it accordingly.

05:55.870 --> 05:56.870
So this is orals.

05:56.890 --> 05:57.670
Get right.

06:00.490 --> 06:01.570
This is Aurel's get.

06:02.350 --> 06:04.300
And this is Aurel's.

06:11.830 --> 06:13.630
Gonna run this and check the result.

06:16.040 --> 06:17.840
So the result is going to be default.

06:18.380 --> 06:19.790
Here what's happening?

06:19.790 --> 06:23.570
So here, if we take a look at it, we are not calling this.

06:23.570 --> 06:29.450
That's the reason why it printed default because I'm expecting the name a student because I'm supplying

06:29.450 --> 06:30.680
a valid student here.

06:30.860 --> 06:32.480
Let's run this and check the result.

06:33.390 --> 06:34.150
We take a look at it.

06:34.150 --> 06:34.750
The result is.

06:34.780 --> 06:36.190
Adam Right.

06:36.190 --> 06:40.720
So under what scenarios this would return default if this is of.

06:41.710 --> 06:42.010
Type.

06:42.010 --> 06:49.510
Now, in that case, it is going to call this or else get method and the supplier is going to return

06:49.510 --> 06:52.330
the default as output.

06:52.360 --> 06:53.170
There you go.

06:53.620 --> 06:55.300
Both are printing the same result.

06:55.330 --> 06:58.810
So the only difference is that this accepts the actual string.

06:58.810 --> 07:00.370
This accepts a supplier.

07:00.400 --> 07:04.210
That's the only difference between Orioles and Orioles get.

07:04.240 --> 07:07.330
Now let's go ahead and explore the Orioles throw.

07:07.480 --> 07:08.800
That's the next one, right?

07:10.850 --> 07:12.140
When I record this.

07:13.120 --> 07:15.130
There's going to be or else.

07:16.050 --> 07:18.690
Row and in here.

07:20.170 --> 07:20.870
Let's go.

07:20.880 --> 07:21.660
This one.

07:21.990 --> 07:27.030
I'm going to copy this line of code where it's going to return a valid student first, and then we'll

07:27.030 --> 07:28.860
go back to the null student.

07:30.210 --> 07:32.370
And then it's going to be the same code.

07:35.920 --> 07:43.210
In the place of orals get we are going to use orals through orals through is again a it is accepting

07:43.210 --> 07:43.720
a supplier.

07:43.720 --> 07:44.260
Right.

07:44.290 --> 07:45.610
What we will do.

07:47.300 --> 07:51.120
Will implement the supplier using the lambda expression.

07:51.140 --> 07:54.650
And then this is going to return a runtime exception.

07:55.010 --> 07:57.260
We all know Throw is going to.

07:58.490 --> 08:00.170
Throw an exception back to the caller.

08:00.170 --> 08:00.470
Right?

08:00.470 --> 08:01.550
That's what it is.

08:01.970 --> 08:02.510
So.

08:03.640 --> 08:06.460
New runtime exception.

08:07.400 --> 08:11.450
And I'm going to give let me put this below so that the code will be clear on the screen.

08:11.700 --> 08:15.050
It is going to return you the value as no data.

08:16.390 --> 08:16.900
Available.

08:18.500 --> 08:19.940
Now let's run this first.

08:20.090 --> 08:26.180
Basically, it is going to return the name of the student and then we'll pass the value as null in here.

08:26.180 --> 08:29.870
It is going to return the runtime exception return name.

08:30.940 --> 08:31.260
So.

08:32.920 --> 08:33.640
This one.

08:34.720 --> 08:35.560
Aurel Stroe.

08:36.460 --> 08:37.900
Copy this and put it here.

08:38.440 --> 08:41.500
The method that we are going to call is optional.

08:43.510 --> 08:43.910
Stone.

08:44.980 --> 08:45.760
Let's run this.

08:46.150 --> 08:51.250
In this run, it is going to return you the actual name of the student, because we are getting the

08:51.250 --> 08:53.050
name of the student from the student database.

08:53.050 --> 08:54.340
So it printed this.

08:54.370 --> 08:57.130
Now what I'm going to do, I'm going to return.

08:57.280 --> 09:01.750
I'm going to pass null In this case, it is not going to execute this code.

09:01.750 --> 09:04.390
It is going to execute the throw code.

09:04.960 --> 09:06.550
Let's go ahead and run this one.

09:07.150 --> 09:10.520
If you run this one, this gave you the exception back.

09:10.540 --> 09:13.360
So basically, Java dot Lang dot runtime exception.

09:13.360 --> 09:15.790
No data available in your use case.

09:15.790 --> 09:20.620
If you feel like if there is no data available, then I want to throw the exception back to the caller

09:20.620 --> 09:23.800
and those kind of use cases you would use or else throw.

09:23.860 --> 09:31.030
If you want the default value in those kind of use cases, you would either use a or else or else get.

09:31.120 --> 09:38.470
So this is all about the or else or else get and or else throw methods.

09:38.860 --> 09:40.840
With this, we came to the end of this tutorial.

09:40.870 --> 09:42.040
Thank you for watching.
