java-development-for-beginners-learnit / 31 - JDK API /002 java.util.Optional - Optional in Java_en.srt
Tan115's picture
Add files using upload-large-folder tool
07c2d26 verified
Raw
History Blame Contribute Delete
34.3 kB
1
00:00:06,000 --> 00:00:06,000
Hello, Jim.
2
00:00:06,000 --> 00:00:09,000
In this lesson, we're going to learn optional class API.
3
00:00:10,000 --> 00:00:16,000
We're going to start a lesson from the definition of optional class is understanding of the goal and
4
00:00:16,000 --> 00:00:24,000
purpose of this class will help you to understand that better known this will be an optional class API.
5
00:00:24,000 --> 00:00:31,000
And on real Covid examples I'm going to show you how to work was methods from optional class, and then
6
00:00:31,000 --> 00:00:33,000
to the lesson of explaining when to use optional.
7
00:00:33,000 --> 00:00:40,000
And you will understand in which cases use of optional will be efficient and will not bring more harm
8
00:00:40,000 --> 00:00:41,000
than good.
9
00:00:41,000 --> 00:00:48,000
Besides that, I will answer to the question what alternative techniques might be used instead of optional?
10
00:00:48,000 --> 00:00:49,000
Let's stop.
11
00:00:50,000 --> 00:00:53,000
And let me start from explanation of what optional class is.
12
00:00:53,000 --> 00:00:54,000
And why do we need it?
13
00:00:55,000 --> 00:00:56,000
Do you know what?
14
00:00:56,000 --> 00:00:58,000
No point or exception is?
15
00:00:58,000 --> 00:00:59,000
Probably.
16
00:00:59,000 --> 00:01:04,000
This is one of the most popular exceptions that interrupts our program execution.
17
00:01:04,000 --> 00:01:09,000
Just a reminder to all students that no point or exception is an exception.
18
00:01:09,000 --> 00:01:10,000
That's a strong one.
19
00:01:10,000 --> 00:01:13,000
We try to invoke some Masset on the reference.
20
00:01:13,000 --> 00:01:15,000
It is not initialized.
21
00:01:15,000 --> 00:01:17,000
In other words, we have variables.
22
00:01:17,000 --> 00:01:21,000
It doesn't store reference to any of objects in the heap memory.
23
00:01:22,000 --> 00:01:26,000
And when we call some Massart on the variable, we received no pointer exception.
24
00:01:26,000 --> 00:01:32,000
That tells us that we actually can't invoke any Massart on the nothing.
25
00:01:32,000 --> 00:01:33,000
Is that clear?
26
00:01:33,000 --> 00:01:40,000
And to avoid cases like this, we should never forget to perform no check before any Massart invocation.
27
00:01:41,000 --> 00:01:47,000
The new clause that has been introduced in Java eight is supposed to alleviate some problems.
28
00:01:47,000 --> 00:01:53,000
In my opinion, the introduction of this class is controversial from one side.
29
00:01:53,000 --> 00:02:00,000
It is grades that geoengineers has two legs, this one to indicate places where we may have no potentially.
30
00:02:00,000 --> 00:02:07,000
But from the other side, in my opinion, optional class, often used by developers inappropriately
31
00:02:07,000 --> 00:02:11,000
and brings into the code more complexity and harm than good.
32
00:02:11,000 --> 00:02:16,000
But in this lesson, I will share with you my tips and other you use optional class efficiently.
33
00:02:17,000 --> 00:02:23,000
Generally speaking written and no reference from any massata considered to be a bad practice.
34
00:02:23,000 --> 00:02:30,000
For example, you call getta on the object that doesn't have all fields initialized yet, and you receive
35
00:02:30,000 --> 00:02:31,000
no reference.
36
00:02:31,000 --> 00:02:32,000
Indeed.
37
00:02:32,000 --> 00:02:35,000
What else you should receive if there is nothing yet?
38
00:02:36,000 --> 00:02:41,000
What if a field is really empty and doesn't initialized with any reference yet?
39
00:02:41,000 --> 00:02:42,000
Thorney ha.
40
00:02:43,000 --> 00:02:48,000
One of the giants of computer science wrote I call it my billion dollar mistake.
41
00:02:49,000 --> 00:02:53,000
It was the invention of the no reference in nineteen sixty five.
42
00:02:53,000 --> 00:03:00,000
I couldn't resist the temptation to put in a no reference simply because it was so easy to implement.
43
00:03:01,000 --> 00:03:05,000
I can add to that it wasn't just so easy to implement.
44
00:03:05,000 --> 00:03:08,000
It was also so logical to implement.
45
00:03:09,000 --> 00:03:13,000
Let me start sharing my screen to explain the problems that we have today.
46
00:03:13,000 --> 00:03:14,000
Is it optional?
47
00:03:14,000 --> 00:03:21,000
Closs is intended to solve I open class problem statement from optional package of my little project.
48
00:03:21,000 --> 00:03:24,000
In this example, imagine that we have instance of the computer.
49
00:03:25,000 --> 00:03:32,000
We have some computer and USB drive, potentially USB drive, maybe plugged in or may not.
50
00:03:33,000 --> 00:03:40,000
I can call yet USB drive Massart on my computer object to get the reference to the USB drive in case
51
00:03:40,000 --> 00:03:42,000
Nassan is plugged in.
52
00:03:42,000 --> 00:03:44,000
I receive no correct.
53
00:03:44,000 --> 00:03:51,000
But in case there is a USB drive, I can get access to the right contacts switch to check it state.
54
00:03:52,000 --> 00:03:55,000
In this case, I need to perform no check again.
55
00:03:55,000 --> 00:03:57,000
Can you see how my code looks like?
56
00:03:58,000 --> 00:04:03,000
And the good thing about all this is that I am smart enough to perform such checks.
57
00:04:03,000 --> 00:04:07,000
It would be worse in case such checks wouldn't exist at all.
58
00:04:08,000 --> 00:04:10,000
And to my program would just fail.
59
00:04:10,000 --> 00:04:17,000
Units execution that understands the point, in addition, is just annoying that these checks get in
60
00:04:17,000 --> 00:04:19,000
the way of the business logic.
61
00:04:20,000 --> 00:04:24,000
In fact, Zedek reasons that overall are the beauty of our program.
62
00:04:25,000 --> 00:04:27,000
What would be better than this?
63
00:04:27,000 --> 00:04:33,000
It will be better to build data structure that would indicate presence or absence of the value.
64
00:04:34,000 --> 00:04:35,000
That's what we need.
65
00:04:35,000 --> 00:04:36,000
Optional Class four.
66
00:04:37,000 --> 00:04:44,000
So we agreed that we need a mechanism that allows a massive developer to explicitly denote there's a
67
00:04:44,000 --> 00:04:51,000
return value for a massive may or may not be present optional class acts as a prop for an object that
68
00:04:51,000 --> 00:04:53,000
may not be present.
69
00:04:53,000 --> 00:04:59,000
Therefore, if we know that our get USB drive mass, it may not return a desert object.
70
00:05:00,000 --> 00:05:02,000
We can change message, return type.
71
00:05:02,000 --> 00:05:10,000
Let me open solution class now in this class, you can see is that get USB drive message from my computer
72
00:05:10,000 --> 00:05:12,000
class, has the friends or Chen typed now?
73
00:05:13,000 --> 00:05:16,000
It is optional parametrized by the USB drive.
74
00:05:17,000 --> 00:05:18,000
And then my USB drive.
75
00:05:18,000 --> 00:05:20,000
I have also updated through Chintan.
76
00:05:21,000 --> 00:05:28,000
Now, is this is optional that this parametrized by rights protects, which did not pay a lot of attention
77
00:05:28,000 --> 00:05:35,000
to the details at this moment, namely how we create objects of optional type, why I use this method
78
00:05:35,000 --> 00:05:36,000
and didn't use another one.
79
00:05:36,000 --> 00:05:41,000
We're going to learn optional gloss in a minute was detailed examples.
80
00:05:41,000 --> 00:05:46,000
Now, I'd like you to understand the idea of problem and our solution.
81
00:05:46,000 --> 00:05:53,000
In my main mass, you can see that I use features of optional class and they check whether some value
82
00:05:53,000 --> 00:05:55,000
is present in my USB drive wrapper.
83
00:05:56,000 --> 00:06:01,000
And this present I execute functions that takes USB drive as my argument.
84
00:06:02,000 --> 00:06:06,000
Namely, I'm getting access to the switch, which is also optional.
85
00:06:06,000 --> 00:06:11,000
And in case it is present, I just print switches state to console.
86
00:06:12,000 --> 00:06:14,000
And if no, then nothing.
87
00:06:14,000 --> 00:06:20,000
And in case there is no USB drive object inside the wrapper, I execute another function.
88
00:06:20,000 --> 00:06:22,000
I just print to console.
89
00:06:22,000 --> 00:06:24,000
That USB drive wasn't found.
90
00:06:24,000 --> 00:06:30,000
As you can see on this example, optional is a similar to the wrapper that may or may not contains the
91
00:06:30,000 --> 00:06:33,000
actual value inside itself.
92
00:06:33,000 --> 00:06:40,000
In simple words, you can view optional as a single value container that is a contains a value of doesn't.
93
00:06:41,000 --> 00:06:44,000
It isn't set to be empty fullerenes.
94
00:06:44,000 --> 00:06:49,000
This construction, you make clients responsible to handle messy value.
95
00:06:49,000 --> 00:06:51,000
You're just walking.
96
00:06:51,000 --> 00:06:56,000
That value here may not be present and you decide what you want to do.
97
00:06:56,000 --> 00:07:04,000
In this case, this client's responsible approach means that creators and Massart is not in the position
98
00:07:04,000 --> 00:07:07,000
to define what should be done in case of missing value.
99
00:07:08,000 --> 00:07:12,000
Is a sink is to throw exception Maseru an exception?
100
00:07:12,000 --> 00:07:17,000
Probably not always the best case and proper behavior according to the business logic in the case of
101
00:07:17,000 --> 00:07:18,000
missing value.
102
00:07:19,000 --> 00:07:23,000
Now, I wonder, you look at this example with fresh eyes.
103
00:07:23,000 --> 00:07:29,000
If Java community and software engineers would like to call this more readable code and simple if URL
104
00:07:29,000 --> 00:07:32,000
statement, I am happy to accept it.
105
00:07:32,000 --> 00:07:36,000
Really, guys, I just want to we don't lie to ourselves.
106
00:07:36,000 --> 00:07:43,000
What we have, instead of if as blogs with a few lambda expressions that are also located inside each
107
00:07:43,000 --> 00:07:43,000
other.
108
00:07:43,000 --> 00:07:50,000
And moreover, all our mass is written optional now, and we can't just take value and work with it
109
00:07:50,000 --> 00:07:50,000
in order.
110
00:07:50,000 --> 00:07:52,000
We could avoid no point exception.
111
00:07:52,000 --> 00:07:56,000
Well, definitely optional is a very powerful tool.
112
00:07:56,000 --> 00:08:00,000
But guys, I can't see what we have want in this case.
113
00:08:01,000 --> 00:08:06,000
There are multiple other features in optional that breena's amazing flexibility.
114
00:08:06,000 --> 00:08:11,000
And let us write less lines of code with optional than without it.
115
00:08:12,000 --> 00:08:12,000
That is true.
116
00:08:13,000 --> 00:08:20,000
But again, it should be also a special case to apply those features of optional is a true story from
117
00:08:20,000 --> 00:08:26,000
my life is one developer from my team created few classes, was all massive rechanneled, optional type
118
00:08:26,000 --> 00:08:28,000
parameterized by other types.
119
00:08:28,000 --> 00:08:31,000
I asked him, why would you do that?
120
00:08:31,000 --> 00:08:37,000
And he replied, because we need to use optional everywhere where we may receive no.
121
00:08:37,000 --> 00:08:39,000
Well, guess not everywhere.
122
00:08:39,000 --> 00:08:41,000
And then again, use an optional everywhere.
123
00:08:42,000 --> 00:08:46,000
Wouldn't agree with us for the better with the abilities that we are so struggling for.
124
00:08:46,000 --> 00:08:54,000
So let's not API optional to better understand its power and understand, jaysus, when and where this
125
00:08:54,000 --> 00:08:54,000
might be applicable.
126
00:08:55,000 --> 00:08:59,000
I open optional demo class with a lot of examples here.
127
00:08:59,000 --> 00:09:01,000
Let me walk you through each example.
128
00:09:01,000 --> 00:09:09,000
Seeing the first ones that we are going to review is how to create an object of optional type in most
129
00:09:09,000 --> 00:09:15,000
of the cases to create object of type optional when it is static, factor in masses, optional class.
130
00:09:16,000 --> 00:09:19,000
The first way to create optional is to call static mass of empty.
131
00:09:20,000 --> 00:09:22,000
This is our chance imta optional.
132
00:09:22,000 --> 00:09:28,000
The empty mass is often used when it is known apro or is at no value exists.
133
00:09:28,000 --> 00:09:35,000
If you have some value and you need to create optional call, static method of attention, you need
134
00:09:35,000 --> 00:09:42,000
to have that object behind's a reference that you are going to use to create optional object was of
135
00:09:42,000 --> 00:09:49,000
mass, because in case you pass no value to the of method, you will get no point exception.
136
00:09:49,000 --> 00:09:55,000
In the case, you know its value behind the reference, Megan, now or may not be now, use another
137
00:09:55,000 --> 00:09:57,000
method of nullable.
138
00:09:58,000 --> 00:10:02,000
This will create optional object based on the no and no not.
139
00:10:02,000 --> 00:10:09,000
Various for example, in previous example, in solution, I used of nullable factor in mass because
140
00:10:09,000 --> 00:10:14,000
I knew that I may need to create optional object based on value.
141
00:10:15,000 --> 00:10:22,000
Now let's review methods that allow us to check if value is present or absent, that the masses that
142
00:10:22,000 --> 00:10:27,000
allow us to check the value inside the optional is present and is empty.
143
00:10:28,000 --> 00:10:31,000
Obviously, these masses are controversial.
144
00:10:31,000 --> 00:10:37,000
One is present, returns true, is empty, returns false, and vice versa.
145
00:10:37,000 --> 00:10:38,000
Here's an example.
146
00:10:38,000 --> 00:10:46,000
You can see that I have destry quail and check was an optional is empty or has value and is a similar
147
00:10:46,000 --> 00:10:48,000
test for optional that contains Nassan.
148
00:10:49,000 --> 00:10:53,000
As a result of method execution, you confront and conceal output.
149
00:10:54,000 --> 00:10:56,000
Now let's review example of conditional masses.
150
00:10:57,000 --> 00:11:03,000
I call these methods conditional because based on the specific condition, API allows us to perform
151
00:11:03,000 --> 00:11:05,000
one or another action.
152
00:11:06,000 --> 00:11:12,000
The first method in this example, if present you already you saw me using this method in the example
153
00:11:12,000 --> 00:11:18,000
in solution was use B drive F presence method takes consumer as method argument.
154
00:11:19,000 --> 00:11:25,000
If you're not familiar with lambda expressions and such function interfaces as consumer, I recommend
155
00:11:25,000 --> 00:11:26,000
you to pass my course.
156
00:11:26,000 --> 00:11:29,000
Guanciale programming for Giwa Engineers.
157
00:11:29,000 --> 00:11:32,000
I explained these topics in detail zere.
158
00:11:32,000 --> 00:11:37,000
Just to remind you, consumer takes some argument and returns nothing.
159
00:11:37,000 --> 00:11:41,000
So basically, if the value is present, we pass it.
160
00:11:41,000 --> 00:11:44,000
Does it function and perform any action?
161
00:11:44,000 --> 00:11:44,000
Was it.
162
00:11:44,000 --> 00:11:47,000
What we need in this particular example.
163
00:11:47,000 --> 00:11:51,000
I just printed to console in case there is no value in optional.
164
00:11:51,000 --> 00:11:55,000
We just ignore this function and just do nothing.
165
00:11:55,000 --> 00:11:59,000
Another conditional Massart is if present or else.
166
00:11:59,000 --> 00:12:03,000
I also use this one in solution of the examples that I have already showed.
167
00:12:03,000 --> 00:12:09,000
I believe that from the massive name, it is already clear for you what is this method for.
168
00:12:09,000 --> 00:12:14,000
So in case value is present, will use consumer and will work with the value.
169
00:12:15,000 --> 00:12:22,000
In the case, value is absent, will execute some random that contains lines of code to be executed.
170
00:12:22,000 --> 00:12:24,000
In case there is no value.
171
00:12:24,000 --> 00:12:28,000
The next example is simple and regular gets massive.
172
00:12:28,000 --> 00:12:34,000
You can extract value from optional Bikel and get mass, but be attentive.
173
00:12:34,000 --> 00:12:38,000
In case there is no value, you will get no such element.
174
00:12:38,000 --> 00:12:38,000
The exception.
175
00:12:39,000 --> 00:12:43,000
And here, in example, you can see this just in case optional is empty.
176
00:12:43,000 --> 00:12:45,000
We are catching exception.
177
00:12:45,000 --> 00:12:49,000
In practice, it is recommended to use is present.
178
00:12:49,000 --> 00:12:53,000
Massett, that guarantees that there is a value inside optional.
179
00:12:53,000 --> 00:12:55,000
So just remember this.
180
00:12:56,000 --> 00:12:58,000
Let's imagine that we are using is present Massart.
181
00:12:58,000 --> 00:12:59,000
OK.
182
00:12:59,000 --> 00:13:01,000
Just look at this example.
183
00:13:01,000 --> 00:13:09,000
How far it is from no check from our problem statement was if we again have the similar Evelio.
184
00:13:09,000 --> 00:13:16,000
That's why in most cases we intentionally award to use the get Masset and use one of the other methods.
185
00:13:16,000 --> 00:13:17,000
Such as.
186
00:13:17,000 --> 00:13:23,000
Or else or else through top things of value associated with a populated, optional.
187
00:13:24,000 --> 00:13:25,000
Or perform another action.
188
00:13:25,000 --> 00:13:30,000
Let me show you this in the next example, you can see how to use or else mass.
189
00:13:31,000 --> 00:13:36,000
Basically, it returns a value if at this present and in case it does not.
190
00:13:36,000 --> 00:13:39,000
It returns the values that they passed as a message.
191
00:13:39,000 --> 00:13:41,000
Also, we have or else get method.
192
00:13:42,000 --> 00:13:45,000
What is the difference between orals and this?
193
00:13:45,000 --> 00:13:48,000
Massively different parameters oils.
194
00:13:48,000 --> 00:13:51,000
Yet they supply as methods argument.
195
00:13:51,000 --> 00:13:54,000
Supply is a function that produce something.
196
00:13:54,000 --> 00:13:57,000
It takes nothing and returns some value.
197
00:13:57,000 --> 00:13:59,000
And what does it give us?
198
00:14:00,000 --> 00:14:03,000
Well, probably it is not obvious in this particular example.
199
00:14:04,000 --> 00:14:06,000
But let's look at the next lines.
200
00:14:07,000 --> 00:14:14,000
Imagine that you have some massive that performs a lot of work and calculations and as a result, recharges
201
00:14:14,000 --> 00:14:19,000
your reference to string value and then use your call or else Massett.
202
00:14:19,000 --> 00:14:26,000
And you would like to parse the string returned by these massive PZM Massett is actually involved.
203
00:14:27,000 --> 00:14:31,000
And you spent some time to wait until the mass of the got finished.
204
00:14:32,000 --> 00:14:35,000
But is it the same with or else get method?
205
00:14:35,000 --> 00:14:36,000
No.
206
00:14:36,000 --> 00:14:45,000
Lukins this line you're actually not invoking is a massive you just Parsons's reference to the function
207
00:14:45,000 --> 00:14:48,000
that knows how to get the value for you.
208
00:14:48,000 --> 00:14:50,000
In case this is needed.
209
00:14:51,000 --> 00:14:52,000
This is the main difference.
210
00:14:53,000 --> 00:15:00,000
Lasing insolate Zeeshan in the first case we're invoking Massart no matter was an optional has a value
211
00:15:00,000 --> 00:15:00,000
on.
212
00:15:00,000 --> 00:15:00,000
No.
213
00:15:01,000 --> 00:15:01,000
And in.
214
00:15:01,000 --> 00:15:08,000
The second case would just use functions that knows how to provide us with value only when it is needed,
215
00:15:09,000 --> 00:15:11,000
only when optional has no value.
216
00:15:12,000 --> 00:15:18,000
Does it make sense now in case the value is absent and you want to throw an exception.
217
00:15:18,000 --> 00:15:20,000
You can use or else throw massive.
218
00:15:21,000 --> 00:15:23,000
This method is overloaded.
219
00:15:23,000 --> 00:15:26,000
The version without arguments will throw no such element.
220
00:15:26,000 --> 00:15:28,000
The exception by default.
221
00:15:28,000 --> 00:15:35,000
Or you can post supplier as Mussert argument to generate object of the exception that you would like
222
00:15:35,000 --> 00:15:35,000
to throw.
223
00:15:36,000 --> 00:15:38,000
Now let's somehow filter Massart works.
224
00:15:39,000 --> 00:15:45,000
In case you have the reference to the optional object, you can check it first to understand whether
225
00:15:45,000 --> 00:15:46,000
it is much your predicate.
226
00:15:47,000 --> 00:15:52,000
And in case verification this past, you receive optional was that value.
227
00:15:52,000 --> 00:15:55,000
Otherwise, you receive empty optional.
228
00:15:55,000 --> 00:15:57,000
Let me explain this example.
229
00:15:57,000 --> 00:16:01,000
Imagine that I have optional that contains my full name.
230
00:16:01,000 --> 00:16:07,000
After that, I can call Filton Masset on the optional object and pass predicate.
231
00:16:07,000 --> 00:16:10,000
Let me quickly remind you what Prickett is.
232
00:16:11,000 --> 00:16:14,000
CreditCard is a functional interface that declares one.
233
00:16:14,000 --> 00:16:15,000
Massart is.
234
00:16:15,000 --> 00:16:19,000
It takes one argument and the returns is a true or false.
235
00:16:19,000 --> 00:16:21,000
So here is my function.
236
00:16:21,000 --> 00:16:25,000
I pass full name and I check whether it contains Andry.
237
00:16:25,000 --> 00:16:33,000
If yes, then I receive optional with this name when I call, or else I get the reference to my whole
238
00:16:33,000 --> 00:16:36,000
name and bring that to console in a similar example.
239
00:16:36,000 --> 00:16:39,000
But with a different name, I will get give empty optional.
240
00:16:40,000 --> 00:16:46,000
And you can charge this by console output in the second case paren to console the false string that
241
00:16:46,000 --> 00:16:48,000
they used in or else methods.
242
00:16:48,000 --> 00:16:56,000
Do you understand why we may need to field the method that is normally used to reject wrapt values based
243
00:16:56,000 --> 00:16:57,000
on the predefined tool?
244
00:16:58,000 --> 00:17:04,000
Imagine now in another case we have a user object, and we need to make sure that the users that we
245
00:17:04,000 --> 00:17:11,000
received from database or from another method has Mercier's less than one number than the more than
246
00:17:11,000 --> 00:17:12,000
another number.
247
00:17:13,000 --> 00:17:14,000
How to do that?
248
00:17:14,000 --> 00:17:22,000
We can use map mass simply to convert the rabbit value to another type, Zimet, Mass, that allows
249
00:17:22,000 --> 00:17:26,000
us to convert the value from one object to another.
250
00:17:26,000 --> 00:17:33,000
If an optional is populated, this method can be thought of as a pipeline method, whereas the rabbit
251
00:17:33,000 --> 00:17:37,000
value is passed along the pipeline and transform.
252
00:17:37,000 --> 00:17:41,000
Can you value the remember how Stream API works?
253
00:17:42,000 --> 00:17:43,000
This similar concept is here.
254
00:17:44,000 --> 00:17:50,000
This method works by accepting the function object as it is applied to the rabbit value to produce the
255
00:17:50,000 --> 00:17:51,000
mapped value.
256
00:17:52,000 --> 00:17:58,000
Even the optional is empty, is a function object is never called, and an empty optional is returned
257
00:17:58,000 --> 00:18:00,000
from the map massively.
258
00:18:00,000 --> 00:18:02,000
Let me explain your own example.
259
00:18:02,000 --> 00:18:05,000
I have optional parametrized by type user.
260
00:18:05,000 --> 00:18:07,000
I have arrived here.
261
00:18:07,000 --> 00:18:08,000
User object.
262
00:18:08,000 --> 00:18:14,000
This user has named Igor and his universe is 1990s.
263
00:18:14,000 --> 00:18:21,000
I call map Massart on my optional object to convert data in the pipeline from user to an end.
264
00:18:22,000 --> 00:18:27,000
I need to pass functions that knows how to transform the current type thing.
265
00:18:28,000 --> 00:18:35,000
And when I have int value in pipeline, I can use filter mass to check was a year of birth of the current
266
00:18:35,000 --> 00:18:38,000
user is within the expected range.
267
00:18:38,000 --> 00:18:44,000
In this case, my user matches the current filters and optional was very is returned.
268
00:18:45,000 --> 00:18:49,000
That's why I can print the user's Berthier tokens.
269
00:18:49,000 --> 00:18:55,000
So what does this example please let me know in case you have any questions in case something is still
270
00:18:55,000 --> 00:18:56,000
unclear.
271
00:18:56,000 --> 00:18:57,000
Please.
272
00:18:57,000 --> 00:18:59,000
What's your question below this video?
273
00:18:59,000 --> 00:19:02,000
And there will be happy to answer it.
274
00:19:02,000 --> 00:19:06,000
Let's review another method that is similar to those in Map Mass.
275
00:19:06,000 --> 00:19:11,000
Let's have the flat map Masset as an alternative for values transformation.
276
00:19:12,000 --> 00:19:15,000
There is one main difference between these two masses.
277
00:19:15,000 --> 00:19:21,000
The difference is that map transforms values only when Xe unwrap.
278
00:19:21,000 --> 00:19:27,000
Whereas flat map takes a wrapped value and unwraps it before transforming it.
279
00:19:28,000 --> 00:19:32,000
Let's make sure that you understood this following Z example.
280
00:19:32,000 --> 00:19:41,000
So imagine that we keep working with the same user, but interested in sync in a mass returns, optional
281
00:19:41,000 --> 00:19:45,000
to, for example, get name Massart, which is optional.
282
00:19:45,000 --> 00:19:47,000
It is parametrized by string.
283
00:19:48,000 --> 00:19:48,000
No.
284
00:19:48,000 --> 00:19:54,000
And all what we know that's extracted from the optional parameterized by type user.
285
00:19:55,000 --> 00:20:00,000
The first thing that I do, I want to convert optional of user to the just name.
286
00:20:00,000 --> 00:20:01,000
What I got instead.
287
00:20:02,000 --> 00:20:07,000
Instead, I got optional parameterized by optional the disbursements surprised by string.
288
00:20:07,000 --> 00:20:11,000
That is because my get name Masset returns optional.
289
00:20:11,000 --> 00:20:16,000
Having this wrapper, I can extract optional parameterized by a string.
290
00:20:17,000 --> 00:20:22,000
It should contain the name of the user, and only after that I can extract user.
291
00:20:23,000 --> 00:20:30,000
Don't you think that they added a lot of redundant steps similar to the Stream API from Jaedicke?
292
00:20:30,000 --> 00:20:35,000
So that map doesn't rabs the value was additional optional.
293
00:20:35,000 --> 00:20:42,000
So to achieve the same goal in a little bit simpler way is to call threads, map and parse the references
294
00:20:42,000 --> 00:20:43,000
to get name message.
295
00:20:44,000 --> 00:20:49,000
And now I can apply Ozz masses to extract value like or message.
296
00:20:50,000 --> 00:20:54,000
And in console, you can see that the results are the same.
297
00:20:54,000 --> 00:20:59,000
But the second option, Dukas, less lines of code to achieve is the same result.
298
00:20:59,000 --> 00:21:00,000
Does it make sense?
299
00:21:01,000 --> 00:21:08,000
In my opinion, we have removed all the most important masses from optional API that might come in handy.
300
00:21:08,000 --> 00:21:15,000
But any way opens a source code of GMU to optional class by yourself and investigated.
301
00:21:15,000 --> 00:21:21,000
Now let's discuss when and how to use optional to make sure it is not dangerous and you don't bring
302
00:21:21,000 --> 00:21:23,000
more harm than good.
303
00:21:24,000 --> 00:21:27,000
Let's review different parts of our program and different use cases.
304
00:21:28,000 --> 00:21:30,000
And I will give you my advice.
305
00:21:30,000 --> 00:21:31,000
Return values.
306
00:21:32,000 --> 00:21:35,000
Often optionals are well suited for a chance values.
307
00:21:36,000 --> 00:21:43,000
Originally, this was intended to be zem main use case in documentation that we can find.
308
00:21:43,000 --> 00:21:44,000
Is it optional?
309
00:21:44,000 --> 00:21:52,000
Type is primarily intended for use as a message, Chantae, where there is a growing need to represent
310
00:21:52,000 --> 00:21:53,000
no result.
311
00:21:53,000 --> 00:21:57,000
And we're using now is likely to cause errors.
312
00:21:57,000 --> 00:22:04,000
And here I'd like to pay you extra attention in order you wouldn't follow code style of colleague of
313
00:22:04,000 --> 00:22:08,000
mine who has all methods developed this optional type return.
314
00:22:09,000 --> 00:22:16,000
You should return optional as of this set where there is a clear need to represent no result.
315
00:22:17,000 --> 00:22:22,000
And we're using now is likely to cause errors, not everywhere.
316
00:22:23,000 --> 00:22:27,000
But still, this recommendation leaves error for interpretations.
317
00:22:27,000 --> 00:22:30,000
I would also add one more important thing to consider.
318
00:22:31,000 --> 00:22:38,000
Use optional only in cases when you want to make client of your code responsible for handling of absent
319
00:22:38,000 --> 00:22:39,000
value.
320
00:22:39,000 --> 00:22:43,000
And this is really a good reason not to put optional the chandab everywhere.
321
00:22:44,000 --> 00:22:44,000
Why?
322
00:22:45,000 --> 00:22:52,000
Because in most cases, we need to provide ready to use API, but not make our clients seeing about
323
00:22:52,000 --> 00:22:58,000
each return value, what to do in case value wasn't returned regarding the fields.
324
00:22:59,000 --> 00:23:00,000
Let me put it simple.
325
00:23:00,000 --> 00:23:04,000
You can create fields of optional type, but you shouldn't.
326
00:23:04,000 --> 00:23:05,000
Why?
327
00:23:05,000 --> 00:23:08,000
For me, it is a sign of a bad architecture.
328
00:23:08,000 --> 00:23:13,000
You have fields known from the beginning that they may not be initialized.
329
00:23:13,000 --> 00:23:20,000
But in this case, you have met Iraqui and that or the architecture was not implemented.
330
00:23:20,000 --> 00:23:21,000
Solid principles.
331
00:23:21,000 --> 00:23:27,000
Probably there are some classes that don't need all these fields, and it is just a matter of building
332
00:23:27,000 --> 00:23:29,000
of proper Iraqui.
333
00:23:29,000 --> 00:23:30,000
A lot of questions.
334
00:23:30,000 --> 00:23:36,000
So I'll put it simple, the not to use optionals in fields parameters.
335
00:23:36,000 --> 00:23:39,000
You can use optional methods, parameters.
336
00:23:39,000 --> 00:23:42,000
But again, it is not recommended to do that.
337
00:23:42,000 --> 00:23:45,000
It is recommended instead to have law that.
338
00:23:46,000 --> 00:23:53,000
If you know that, you might need to invoke this method with no parameter or this empty optional, in
339
00:23:53,000 --> 00:23:53,000
other words.
340
00:23:54,000 --> 00:23:57,000
So just with mass, that takes no parameter.
341
00:23:58,000 --> 00:24:04,000
But you also can have our law that was the same name, but which takes parameter.
342
00:24:04,000 --> 00:24:05,000
Is that clear?
343
00:24:05,000 --> 00:24:09,000
Some students ask me what is alternative to use an optional.
344
00:24:10,000 --> 00:24:15,000
Well, the simplest alternative is to leave like our ancestors left.
345
00:24:15,000 --> 00:24:19,000
Just use no value and do not forget, put no checks.
346
00:24:20,000 --> 00:24:23,000
This is still the case in a lot of programs nowadays.
347
00:24:23,000 --> 00:24:30,000
Another alternative might be a sort of an exception if absence of the value really brigg's the way of
348
00:24:30,000 --> 00:24:31,000
your program execution.
349
00:24:32,000 --> 00:24:37,000
Then you need to be prepared to handle specific exception and to handle it properly.
350
00:24:37,000 --> 00:24:42,000
During the handling of this exception, you specify 70 fractions.
351
00:24:42,000 --> 00:24:45,000
What is needed to be done in case the value is absent?
352
00:24:46,000 --> 00:24:48,000
Almost a similar way you do with optional.
353
00:24:49,000 --> 00:24:51,000
And another alternative is no objects.
354
00:24:51,000 --> 00:24:58,000
You can create a time that has the same API, but it is empty inside and all my are error safe.
355
00:24:59,000 --> 00:25:01,000
And when it is seen, you just create.
356
00:25:01,000 --> 00:25:07,000
Instance of the empty object or just parcels, a reference to that empty constantly, for example,
357
00:25:07,000 --> 00:25:10,000
in collections, glossaries, empty, list constant.
358
00:25:11,000 --> 00:25:14,000
And instead of returning, no one least is expected.
359
00:25:15,000 --> 00:25:16,000
You can just return empty.
360
00:25:16,000 --> 00:25:22,000
These constant that will save you from no point exception remembers use of devices.
361
00:25:23,000 --> 00:25:27,000
They're years of experience and millions of dollars behind these advices.
362
00:25:28,000 --> 00:25:31,000
Let's recap what we have learned in this lesson.
363
00:25:31,000 --> 00:25:32,000
In this lesson we learned.
364
00:25:33,000 --> 00:25:35,000
Optional class API and its methods.
365
00:25:36,000 --> 00:25:39,000
It knows the purpose of the optional and when to use it.
366
00:25:39,000 --> 00:25:46,000
And at the end of the lesson, I also explained few alternative techniques to use instead of optional.
367
00:25:46,000 --> 00:25:48,000
That's all for this lesson.
368
00:25:48,000 --> 00:25:49,000
Thanks a lot for your attention.
369
00:25:50,000 --> 00:25:52,000
Have a great day and see you in the next lesson.