File size: 34,265 Bytes
07c2d26 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 | 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.
|