YouTube2Text

Java 8 Crash Course in One Shot πŸ”₯ | From Basics to Advanced β€” Transcript

by The Curious Coder Β· 27,821 words Β· 4,033 segments Β· language en Β· Watch on YouTube

Full transcript

  1. 0:00Hello and welcome to my channel, The
  2. 0:02Curious Coder. If you're preparing for
  3. 0:04Java interviews or want to finally
  4. 0:06understand Java 8 the right way, this
  5. 0:09course is for you. In the next 3 hours,
  6. 0:12we are not just going to learn concepts.
  7. 0:15We are going to code everything live
  8. 0:17with real world examples that actually
  9. 0:20make sense. We'll start with powerful
  10. 0:22upgrades to interfaces like default
  11. 0:25methods and static methods. Then move on
  12. 0:27to the core of Java 8 functional
  13. 0:29interfaces and lambda expressions. From
  14. 0:32there we'll break down the most
  15. 0:34important built-in functional interfaces
  16. 0:36predicate consumer function and
  17. 0:38supplier. You'll also learn method
  18. 0:41references then optional and then master
  19. 0:45the most asked topic in interviews
  20. 0:47streams API. And finally, we'll cover
  21. 0:50the modern date and time API so you can
  22. 0:52stop struggling with legacy classes once
  23. 0:54and for all. All right, so let's get
  24. 0:57started. So what is a default method?
  25. 1:00Default method is a method inside an
  26. 1:03interface that has some implementation.
  27. 1:06Before Java 8, we could only declare
  28. 1:09abstract methods inside an interface. We
  29. 1:11could not give them any body. But Java 8
  30. 1:14allowed us to have fully implemented
  31. 1:16methods inside an interface. Now these
  32. 1:19methods can be defined using the default
  33. 1:21keyword. Hence they are called default
  34. 1:24methods. All right. So let me show you
  35. 1:27some examples. So we have an interface
  36. 1:29here named my interface. And let's say I
  37. 1:32just simply declare one method. Okay.
  38. 1:36Like this. Now this is an abstract
  39. 1:38method because it does not have any
  40. 1:40implementation. All right. So this is
  41. 1:43how we used to declare abstract methods
  42. 1:45in an interface. But now if I try to
  43. 1:48give it some implementation, you will
  44. 1:50see that it gives me an error that says
  45. 1:53interface abstract methods cannot have a
  46. 1:55body. All right. So inside an interface
  47. 1:58we are not allowed to give body to a
  48. 2:00method. Okay. Now that was before Java
  49. 2:038. Now using Java 8, I can just simply
  50. 2:06add a keyword default. And now there is
  51. 2:09no compilation issue. All right. So
  52. 2:12using this default keyword I can declare
  53. 2:14the methods and I can also give them
  54. 2:16some body. Let's so I'll just add a
  55. 2:18statement here. Let's say
  56. 2:20system.out.print ln this is a default
  57. 2:26method.
  58. 2:27Okay something like that. All right and
  59. 2:31I'll just call it using some child
  60. 2:33class. So let me create some my child
  61. 2:37class
  62. 2:41and this implements the interface
  63. 2:45and if I create an object of this class
  64. 2:48my child class obj is equal to new my
  65. 2:53child class using this I can simply call
  66. 2:56this method. All right
  67. 3:00great. So this statement is printed. All
  68. 3:03right. So we have declared the method
  69. 3:05inside an interface only. I didn't give
  70. 3:08any implementation here in the child
  71. 3:09class and using this I can call the
  72. 3:12method. All right. Now why was default
  73. 3:15method introduced? What is the need for
  74. 3:17it? Default methods were introduced for
  75. 3:20backward compatibility. Before Java 8,
  76. 3:24if a new method was added to an
  77. 3:26interface, all the implementing classes
  78. 3:28were forced to implement it. Otherwise
  79. 3:31the code would fail to compile. But now
  80. 3:34with default methods we can provide an
  81. 3:36implementation directly inside the
  82. 3:38interface without breaking the existing
  83. 3:40code. If a child class wants to override
  84. 3:43the method and give its own
  85. 3:45implementation, it can do so. If not,
  86. 3:48the default method implementation
  87. 3:50defined in the interface will be used
  88. 3:52automatically.
  89. 3:53This means no changes are required in
  90. 3:55the existing implementing classes. All
  91. 3:58right. So let me show you. I have an
  92. 4:01interface named payment and it has this
  93. 4:04method void pay. All right. So this is
  94. 4:07an abstract method which has no
  95. 4:09implementation. Okay. So we have three
  96. 4:11child classes of this interface. Credit
  97. 4:13card, net banking and UPI. And in all
  98. 4:16these classes this method has been
  99. 4:18implemented. Okay. They have given their
  100. 4:21each implementation. Okay. In net
  101. 4:23banking similarly in UPI as well. Okay.
  102. 4:26So so far everything seems fine but
  103. 4:29let's say I want to add one more
  104. 4:32functionality. Let's say now now I want
  105. 4:34to add a refund functionality. Okay. So
  106. 4:36I just add another method void refund.
  107. 4:40Okay. So what happens is that it has
  108. 4:43created a certain problems. Okay. If
  109. 4:45I'll go inside this implementation you
  110. 4:47can see that there is a compilation
  111. 4:49error that says class credit card must
  112. 4:52either be declared abstract or implement
  113. 4:54the abstract method refund. Okay, so
  114. 4:56it's asking this this class to implement
  115. 5:00this method. Okay, this one the refund
  116. 5:03that we just added. Similarly, UPI will
  117. 5:06also have to implement it and net
  118. 5:08banking will also have to implement it.
  119. 5:11Okay, so first of all, why why do they
  120. 5:14have to implement this this method?
  121. 5:16Because let's say I just create an
  122. 5:18object of net banking and I try to call
  123. 5:20this method refund. So what exactly will
  124. 5:23be called? there is no implementation
  125. 5:25right now. Correct? So that is why
  126. 5:28compiler will not know what exactly to
  127. 5:30call. So that's why there is a need for
  128. 5:32some implementation. Okay. So now what
  129. 5:36what is the solution for this? I can
  130. 5:38simply just implement this method. Let's
  131. 5:40say I just implement refund. I can give
  132. 5:43some print statement. Similarly in net
  133. 5:46banking as well I can implement this
  134. 5:48method. All right. And in UPI also we'll
  135. 5:52just implement the method. Okay. So that
  136. 5:56is one way. But the problem is let's say
  137. 5:59there is an interface with hundreds of
  138. 6:01implementations and you just want to add
  139. 6:03some functionalities. So all the
  140. 6:05implementations will have to implement
  141. 6:07that method. Okay. So before adding that
  142. 6:10functionality you will have to give all
  143. 6:12that logic. Okay. So now that is not
  144. 6:15backward compatible and that is a bit
  145. 6:17tedious. So to solve this problem what
  146. 6:20Java did was let me first just remove
  147. 6:23this. Okay. We will not give
  148. 6:24implementation here.
  149. 6:27All right.
  150. 6:30Okay. So here we have not given in UPI
  151. 6:33and in net banking also I will remove
  152. 6:35it. Yeah. So instead of giving the
  153. 6:37implementations there what we can do
  154. 6:40simply we can make this method as
  155. 6:42default
  156. 6:44and we can provide our own
  157. 6:46implementation.
  158. 6:48Okay. So now you can see that there is
  159. 6:50no issue in any of these classes because
  160. 6:53now if we create an object of credit
  161. 6:55card and we try to call this payment uh
  162. 6:58this refund method. So this default
  163. 7:00implementation will be called. Okay. So
  164. 7:02as of now I've not written anything. I
  165. 7:04can write something as well. Let's say
  166. 7:06default
  167. 7:08refund flow executed.
  168. 7:12All right. Something like this. Okay. So
  169. 7:15this makes the code backward compatible
  170. 7:18and now we can just add any of the
  171. 7:20methods that we want to without breaking
  172. 7:22the existing code. Okay. If these
  173. 7:25classes want to give some some of their
  174. 7:28own implementation for refund, they are
  175. 7:30allowed to do that. They can just
  176. 7:31override this method and give their own
  177. 7:33implementation. If they don't want to,
  178. 7:35then there is no necessity for them to
  179. 7:37do it. Okay? The code will not break.
  180. 7:41All right? And also like I'll give one
  181. 7:43more example. Let's consider this list
  182. 7:45do on Java. Okay. So this is a class
  183. 7:48which is of this package java.util. So
  184. 7:51this class belongs to java itself. And
  185. 7:53actually they were also facing the same
  186. 7:55problem. They wanted to add some
  187. 7:57functionalities in this interface list.
  188. 7:59But you can see that list has so many
  189. 8:01implementations. There are a lot of
  190. 8:03implementations and Java is used
  191. 8:05worldwide. So everyone have to implement
  192. 8:07it. Okay. So that's why they introduce
  193. 8:11default methods. So I'll show you some
  194. 8:13examples like
  195. 8:17default
  196. 8:18here we have like we have this default
  197. 8:20method split iterator or we have this
  198. 8:24sort method okay and we have the replace
  199. 8:27all. So all these methods were added
  200. 8:30with a default keyword so that the
  201. 8:31existing code would not break. Okay. So
  202. 8:35the answer is that Java this default
  203. 8:37method was introduced this default
  204. 8:39keyword was introduced for backward
  205. 8:41compatibility.
  206. 8:43Now there are certain rules associated
  207. 8:45with default methods. So let's look at
  208. 8:48them one by one. First rule says that we
  209. 8:51can have multiple default methods inside
  210. 8:53an interface. Okay. So let's say I want
  211. 8:55to create one more default method. Let's
  212. 8:57say something is valid payment. Okay.
  213. 9:01Something like this. And I just want to
  214. 9:03give an implementation here as well. So
  215. 9:06I can also create a default I can use a
  216. 9:09default keyword and create method like
  217. 9:11this. Okay. Let's say I'm just simply
  218. 9:12returning true for now.
  219. 9:15Okay. So in the same interface we have
  220. 9:18multiple default methods. Now second
  221. 9:21rule says that these default methods can
  222. 9:24be overridden as well. Okay. So as I
  223. 9:27told you if these child classes want to
  224. 9:29give their own implementation they can
  225. 9:31do that. Okay. So I will just implement
  226. 9:34it. Let's say I want to implement
  227. 9:36refund. Okay. So
  228. 9:39here this class has its own
  229. 9:42implementation. It says CC refund flow
  230. 9:48executed.
  231. 9:50All right. Now in the main class if I
  232. 9:54just create an object of credit card
  233. 9:58let's say OPJ1 is equal to new credit
  234. 10:01card and using this object
  235. 10:04if I call refund okay and I create one
  236. 10:09more let's say for UPI
  237. 10:11opJ2 is equal to new UPI
  238. 10:15and using UPI also I will call okay
  239. 10:19using this object also
  240. 10:21I will call the refund method. Okay. So
  241. 10:24in credit card I have given some
  242. 10:26implementation. In UPI I have not
  243. 10:28implemented refund. Okay. I've not
  244. 10:30overridden it. All right. So if we run
  245. 10:32this.
  246. 10:34Great. So for this credit card the CC
  247. 10:37refund flow executed. But for UPI the
  248. 10:40default refund flow executed.
  249. 10:42All right. Now uh there is one more
  250. 10:45issue. There is actually an issue with
  251. 10:47this default keyword as well. Let me
  252. 10:50tell you about that. So it is called
  253. 10:52multiple inheritance problem or we also
  254. 10:55call it as a diamond problem. Okay. So
  255. 10:58I'll take one more example. Let's say I
  256. 11:00have a calculator
  257. 11:06interface.
  258. 11:07Here we have a simple method int
  259. 11:10operate. Okay. It takes two arguments
  260. 11:12int a comma int b. All right. So we have
  261. 11:18this calculator interface and let's say
  262. 11:20this is a default method. I simply give
  263. 11:23an implementation where I'm returning a
  264. 11:26+ b. Okay. So I'm returning the sum of
  265. 11:30these two values. And let's say there is
  266. 11:32one more interface
  267. 11:35calculator one.
  268. 11:38Let's say it's an interface.
  269. 11:41And here as well we create this default
  270. 11:44method operate. It also has the same
  271. 11:46name but the implementation is
  272. 11:49different. Okay. Here we are multiplying
  273. 11:51the two fields. All right. Now there is
  274. 11:54a child class
  275. 11:57right. Let's say child calculator or let
  276. 12:01me call it simple
  277. 12:03calculator impl calculator
  278. 12:06implementation.
  279. 12:07Okay. And this implements
  280. 12:11calculator as well as calculator one.
  281. 12:15Okay. So this child class implements
  282. 12:17both these calculators. So now there is
  283. 12:20an issue. There is a diamond problem.
  284. 12:23How? When I'll create an object of this
  285. 12:26class calculator impl. Now compiler will
  286. 12:29again won't know which method to be
  287. 12:31called. If I try to call this operate
  288. 12:33method, this implementation is
  289. 12:36implementing both the calculators. So
  290. 12:38the compiler will not know which operate
  291. 12:40to be called. Okay. So the rule says
  292. 12:43that whenever a child class is
  293. 12:45implementing multiple interfaces and
  294. 12:47those interfaces have the same method,
  295. 12:50method with the same name. So in that
  296. 12:52case it has to implement that default
  297. 12:54method. Okay. So now it's a necessity
  298. 12:56for calculator implement for calculator
  299. 12:59impl.
  300. 13:01And here we have to tell which one we
  301. 13:04want to actually call. Okay. So using
  302. 13:07calculator.sup super.operate we are
  303. 13:09saying we are actually telling compiler
  304. 13:12that the operate of this calculator
  305. 13:14interface has to be called. Okay. So let
  306. 13:16me show you in the main. Let me just
  307. 13:18remove all this.
  308. 13:20We have calculator implementation.
  309. 13:23C is equal to new calculator
  310. 13:25implementation. using this if I call the
  311. 13:28operate method 10, 20 and I simply run
  312. 13:32it.
  313. 13:34Okay, we have not printed this. So,
  314. 13:36system.out.print
  315. 13:37ln just print the result.
  316. 13:43Okay, so you will see that these two
  317. 13:44values have been added because we have
  318. 13:46written calculator.sup super.operate.
  319. 13:49So, this one will be called. Now,
  320. 13:51instead of this, if I write
  321. 13:54calculator 1.sup.operate operate and
  322. 13:56then I run it. So now it will be a
  323. 13:59multiplication because this method was
  324. 14:02called the pres the one present in
  325. 14:04interface calculator one. Okay. So this
  326. 14:07rule was that whenever multiple
  327. 14:08interface whenever a class is
  328. 14:10implementing multiple interfaces and
  329. 14:11those interfaces have a method with the
  330. 14:13same name then the child class will have
  331. 14:15to give some implementation of its own.
  332. 14:18Okay. Now let's consider one more
  333. 14:20scenario. Let's say this calculator one
  334. 14:23was not an interface. it was just a
  335. 14:25class. Okay. And I'll not create a
  336. 14:28default method here. This will be a
  337. 14:29normal public int operate. Okay. So this
  338. 14:32is a class calculator which has this
  339. 14:34operate method. And calculator is an
  340. 14:36interface which also has operate method
  341. 14:38but this one is a default one. Okay. So
  342. 14:41this calculator impl
  343. 14:45not an interface. So we will just extend
  344. 14:48calculator one and implement calculator
  345. 14:52interface. Okay. And let me remove this.
  346. 14:56All right. So now the child class is
  347. 14:59extending one class and it is
  348. 15:01implementing an interface. And both the
  349. 15:04interface as well as this class have the
  350. 15:07same method. Okay. Method with the same
  351. 15:09name but they have some different
  352. 15:11implementations.
  353. 15:12Okay. So now if I try to call this
  354. 15:15method operate using the implementations
  355. 15:17object. Okay. So what will be called
  356. 15:20whether it will be this interfaces
  357. 15:22method or will it be this classes method
  358. 15:26okay this one sorry calculator one this
  359. 15:28classes method or this interfaces method
  360. 15:31okay so let's see if I run my
  361. 15:33application
  362. 15:35great so you will see that
  363. 15:36multiplication has been performed which
  364. 15:39means that the method of this class
  365. 15:41calculator one was called okay so this
  366. 15:45is rule number four it says that
  367. 15:47whenever a child class is implementing
  368. 15:50one class is implementing one interface
  369. 15:52and extending another class. So the
  370. 15:55class will get precedence in case there
  371. 15:57are two methods with the same name.
  372. 15:59Okay. So this class will get precedence
  373. 16:02which in this case is calculator one and
  374. 16:05this interface will not get will be at
  375. 16:07the priority two. All right. Now there
  376. 16:11is one more rule a last rule that I want
  377. 16:13to tell you and mostly people miss this
  378. 16:15that we have a class object
  379. 16:19okay which is the parent class of all
  380. 16:22the classes all right and you can see it
  381. 16:24has some methods like it has a method
  382. 16:27equals okay I'll show you
  383. 16:34okay this is there is this method equals
  384. 16:36so let's say I just take this method
  385. 16:40And this is my interface here. Let's say
  386. 16:45I just try to override this method.
  387. 16:48Okay. I'll give some other
  388. 16:50implementation and I'll add a default
  389. 16:52keyword. Okay. Simply return false.
  390. 16:57All right. So this I cannot do. All
  391. 17:01right. In any other class I can override
  392. 17:04the method of object class because
  393. 17:06object class is the parent class of all
  394. 17:08the classes. So in any other class I can
  395. 17:10override that method and give my own
  396. 17:12implementation. But I cannot do that in
  397. 17:15an interface where I'm using the default
  398. 17:17keyword. Okay. So you can see that it
  399. 17:19gives me an error that says default
  400. 17:21method equal overrides a member of
  401. 17:23object. All right. So this is one more
  402. 17:25rule which you must keep in mind that
  403. 17:27you cannot override a method of object
  404. 17:29class and give the default keyword okay
  405. 17:32in this interface.
  406. 17:34All right. Hello and welcome to my
  407. 17:36channel the curious coder. Today I'll be
  408. 17:40talking about another very interesting
  409. 17:42feature that was introduced in Java 8.
  410. 17:44Static methods inside interfaces.
  411. 17:48Before Java 8 interfaces were very
  412. 17:50strict. We were not allowed to add
  413. 17:52static method inside an interface. But
  414. 17:54starting from Java 8, Java allowed us to
  415. 17:57do so. Now the question is why was this
  416. 18:01needed? Think about it. Sometimes we
  417. 18:04want a method that logically belongs
  418. 18:07only to the interface. We do not want
  419. 18:10child classes to override it and have
  420. 18:12their own implementation.
  421. 18:14It could be a helper method or some
  422. 18:16validation method. But the point is that
  423. 18:19we want that behavior to stay with the
  424. 18:21interface itself, not with the
  425. 18:23implementing class. That is exactly
  426. 18:26where static methods come into the
  427. 18:28picture. Let's move to Intelligj and
  428. 18:31I'll help you understand how this
  429. 18:33actually works. So I have taken an
  430. 18:36interface named payment. As of now it
  431. 18:39has two methods pay and refund. And I've
  432. 18:42taken three child classes UPI, credit
  433. 18:45card and net banking. And you can see
  434. 18:47that all these child classes have their
  435. 18:50own implementations for pay and refund.
  436. 18:53You can see that every child class have
  437. 18:55their own implementation.
  438. 18:57All right. Now let's say I want to add
  439. 19:00some validation inside this interface.
  440. 19:03I want to create a method boolean is
  441. 19:07valid amount. Okay. I'm validating the
  442. 19:09amount
  443. 19:11like this in amount. And my logic for
  444. 19:14validation is that if the amount is
  445. 19:18greater than zero then return true.
  446. 19:21Okay, it's a valid amount. Otherwise,
  447. 19:24simply return false.
  448. 19:27All right, so this is a logic for
  449. 19:29validation that I want to add inside my
  450. 19:31interface and I want this logic to be
  451. 19:35common for all the child classes. Okay.
  452. 19:38So I don't want any child class to
  453. 19:40override this method and give their own
  454. 19:43implementation because it's just a
  455. 19:45simple validation logic. So I want it
  456. 19:47common for all the classes. Okay. So how
  457. 19:51can I do that? Now before Java 8 this
  458. 19:53was not possible because we could only
  459. 19:56declare some abstract methods which the
  460. 19:58child classes can change. But after Java
  461. 20:018 we can do it using the static keyword.
  462. 20:05Okay. So this is how we can declare
  463. 20:07static methods inside our interface and
  464. 20:10these static methods belong only to the
  465. 20:13interface not to the child classes.
  466. 20:16Okay, these methods are not a part of
  467. 20:18child classes and I can show it to you.
  468. 20:21Let's say I create an object of credit
  469. 20:24card
  470. 20:25and using this object I can call the pay
  471. 20:28method. I can call the refund method.
  472. 20:32But if I try to call is valid amount
  473. 20:38this I'm not able to do because it says
  474. 20:41static method may only be called on its
  475. 20:43containing interface. All right let's
  476. 20:46say if I try to call it directly using
  477. 20:49the class name that also I won't be able
  478. 20:53to do. Okay. Again it will give me the
  479. 20:56same error.
  480. 20:58All right. So this proves that the
  481. 21:00static method belongs only to the
  482. 21:03interface. Now let's say I want to
  483. 21:06override this method. Okay, I want to
  484. 21:09override this is valid amount inside net
  485. 21:11banking and give a different
  486. 21:13implementation. All right, so let's try
  487. 21:15that. We have public boolean
  488. 21:20is default.
  489. 21:24Sorry, my bad. Is valid amount.
  490. 21:27Okay. and there is int amount.
  491. 21:32Okay. So let's say I just simply return
  492. 21:35true. All right. So you won't see any
  493. 21:38compilation issue here. But if you look
  494. 21:41at it carefully, this is not an
  495. 21:43implementation of is valid amount. This
  496. 21:45is not overriding because when we
  497. 21:48override a method like we did here, we
  498. 21:50get this sign that says that we have
  499. 21:52overridden a method of this interface
  500. 21:55payment. And here is the implementation.
  501. 21:58Okay. But in this case, what we simply
  502. 22:00did was we just created a separate
  503. 22:03method here. Okay. So this is a totally
  504. 22:05separate method that belongs to this
  505. 22:07class net banking. But this is not an
  506. 22:09implementation of this method. All
  507. 22:12right. So this is actually called method
  508. 22:14hiding. This is not method overwriting.
  509. 22:17All right. So so far we have seen that a
  510. 22:20static method belongs to the interface
  511. 22:22and a static method cannot be
  512. 22:24overridden. Okay. So I'll just simply
  513. 22:26remove it from the implementation. Okay.
  514. 22:29We have it here in net banking. All
  515. 22:31right. So this is just method hiding.
  516. 22:34Okay. Now there are certain other
  517. 22:37features about this static method. One
  518. 22:39is that we can create more than one
  519. 22:41static method inside an interface. So
  520. 22:44let's say I just simply create some
  521. 22:46method log time. Okay. Let's say I want
  522. 22:49to log the time. Okay. So we simply
  523. 22:52print term.out.print print len new date.
  524. 22:57All right, something like this.
  525. 23:02So that can also be done inside one
  526. 23:04interface. We can have multiple static
  527. 23:06methods
  528. 23:08also. One static method can call another
  529. 23:10static method. Okay. So let's say from
  530. 23:13this method I want to call the log time.
  531. 23:15That is also possible. Okay, there is no
  532. 23:17issue in that. But if I want to call a
  533. 23:21default method simply I just create
  534. 23:24another method a default method. Okay.
  535. 23:27And if I try to call it from a static
  536. 23:29method that actually I cannot do.
  537. 23:33Okay. So that is a characteristic of
  538. 23:36this static method that we it it does
  539. 23:39not allow us to call a default method
  540. 23:41from a static method.
  541. 23:44Okay. And there is one more thing the
  542. 23:46static methods that we create we always
  543. 23:50have to define a body for for those
  544. 23:52methods. We cannot leave them abstract.
  545. 23:54Okay. So if I just simply you know keep
  546. 23:56it like this I remove the implementation
  547. 23:59and I try to create it abstract that I
  548. 24:02cannot do and actually that makes sense
  549. 24:05because since no child class can
  550. 24:07override it and give its own
  551. 24:08implementation. So that is why it is
  552. 24:10necessary for us to provide the
  553. 24:12implementation here itself in the
  554. 24:14interface. Right? That is only logical.
  555. 24:17Okay. So always a static method has to
  556. 24:20be given some implementation.
  557. 24:22All right. Now one more thing if a
  558. 24:25static method cannot be implemented
  559. 24:27inside a child class and it cannot be
  560. 24:30even called by a child class then how
  561. 24:32are we going to invoke this method?
  562. 24:35Okay. Who will call this? So let's see.
  563. 24:37First let me give some implementation
  564. 24:39here. System.out.t print ln new date.
  565. 24:45Okay. In the main class we have already
  566. 24:49seen that have I when I created an
  567. 24:51object of credit card I was not able to
  568. 24:53call my method using that. Okay. So
  569. 24:56actually it's very simple. We just take
  570. 24:58the interface name and using this we can
  571. 25:00directly call our method. Okay. Like
  572. 25:04this. Let me just print it.
  573. 25:07John.out.print ln.
  574. 25:12All right.
  575. 25:16Great. So you will see that the static
  576. 25:18methods are invoked. Okay.
  577. 25:22Now this is a summary of all the
  578. 25:24features of static methods inside an
  579. 25:26interface. I have created it so that you
  580. 25:28can take a screenshot of it. It will be
  581. 25:31very helpful for you when you want to
  582. 25:32revise this topic. All right.
  583. 25:35Hello and welcome to my channel the
  584. 25:37curious coder. In this video we'll be
  585. 25:40learning about one of the most important
  586. 25:42feature that Java 8 introduced
  587. 25:45functional interface. What is a
  588. 25:48functional interface? It is an interface
  589. 25:50that contains exactly one abstract
  590. 25:53method which makes it suitable for
  591. 25:55lambda expressions. All right. So it's
  592. 25:58an interface that contains exactly one
  593. 26:00method which is abstract not more not
  594. 26:02less. Why? Because that makes it
  595. 26:06suitable for lambda expressions. Now,
  596. 26:08what are lambda expressions? That we'll
  597. 26:10understand just in a while. For now,
  598. 26:13let's try to create a functional
  599. 26:14interface. So, what I'm going to do is
  600. 26:18I'll just simply create an interface.
  601. 26:20Let's say my interface.
  602. 26:24Now, is this a function interface? No,
  603. 26:27not yet. Because as of now, it does not
  604. 26:30have any abstract method. But we need
  605. 26:32one abstract method. All right. So let
  606. 26:35me add a method. Let's say void method
  607. 26:38one.
  608. 26:39All right. So now is it a functional
  609. 26:41interface? Yes. Now it is because it has
  610. 26:44only one abstract method. Okay. So I
  611. 26:48hope you guys know what abstract methods
  612. 26:50are. Basically these are the methods
  613. 26:52that we just declare. They don't have
  614. 26:54any body. Okay. They don't have any body
  615. 26:56like this. So these are called abstract
  616. 26:58methods. All right. So this can be
  617. 27:01considered as a functional interface.
  618. 27:03Now let's say I add one more method word
  619. 27:06method two like this.
  620. 27:10So now is is this a functional
  621. 27:12interface? No. Again this is not a
  622. 27:14functional interface because it has two
  623. 27:17abstract methods and we only need one.
  624. 27:19Okay. But let's say I make it a default
  625. 27:23method like this.
  626. 27:26All right. So default methods have body
  627. 27:28inside them. Okay. We can add curly
  628. 27:30braces and give any body if you want. So
  629. 27:32now is this a functional interface? Yes,
  630. 27:35it is still a functional interface
  631. 27:37because the only rule is that there
  632. 27:39should be one abstract method. But we
  633. 27:41can have as many default methods as we
  634. 27:43want. Okay. Similarly, we can have
  635. 27:46static methods inside a functional
  636. 27:48interface. Okay. So let's say we have a
  637. 27:50method three like this. So still this
  638. 27:54can be considered as a functional
  639. 27:56interface. All right. Also there is one
  640. 27:59more thing. It is recommended that we
  641. 28:01should put this annotation at the rate
  642. 28:03functional interface on a functional
  643. 28:05interface. Okay. So what is the role of
  644. 28:08this? It basically restricts the
  645. 28:11interface to behave as a functional
  646. 28:13interface. All right. What do I mean by
  647. 28:15that? Let's say now if I add one more
  648. 28:18method. Okay. Like this void method 4.
  649. 28:25So I have added one more abstract
  650. 28:26method. So you'll see that it is giving
  651. 28:28me some compilation issue. It says
  652. 28:31multiple non-overriding abstract methods
  653. 28:33found in my interface. All right. So
  654. 28:35this means that this annotation has
  655. 28:38added a restriction on this interface
  656. 28:40that it should not break the rules of
  657. 28:42functional interface. And as I told you
  658. 28:44functional interface can only have one
  659. 28:46abstract method. So that's why it's
  660. 28:48giving me a compilation issue because
  661. 28:50here we have created two. Okay. So I'll
  662. 28:52just remove this and now it works fine.
  663. 28:55Similarly, if I'll just comment this one
  664. 28:57again, it will give me any it will give
  665. 28:59me error that says no target method
  666. 29:01found because we still need one method.
  667. 29:04Okay. So, this is what functional
  668. 29:06interface is. All right. These are all
  669. 29:08the rules and this is how we can create
  670. 29:09any functional interface. Okay. Now,
  671. 29:12what's the purpose of it? Why does it
  672. 29:14have this rule that it should have only
  673. 29:16one method and uh what exactly we are
  674. 29:19going to do with this interface? So
  675. 29:21actually the main purpose behind this is
  676. 29:23that we can we can give an
  677. 29:25implementation of this interface using
  678. 29:27lambda expressions.
  679. 29:29All right. So now let's understand what
  680. 29:31lambda expressions are. Lambda
  681. 29:33expressions are a concise way to
  682. 29:36represent an implementation of a
  683. 29:37function interface using a short
  684. 29:39readable syntax. Basically they are
  685. 29:42designed to reduce a lot of boilerplate
  686. 29:44code. All right. So they implement the
  687. 29:47functional interface and in a very short
  688. 29:50manner okay using very less syntax. All
  689. 29:52right. So let's see how how they do
  690. 29:54that. So I have created an interface
  691. 29:57named ve and I have declared it as a
  692. 30:00functional interface and it has this
  693. 30:02abstract method start. Okay. Now let's
  694. 30:06say I want to call this method. Okay. So
  695. 30:09can I just create an object of vehicle
  696. 30:12like this and using this can I call it?
  697. 30:16Okay, we have vehicle v1 is equal to new
  698. 30:19vehicle and using this I want to call
  699. 30:21this method. Okay, so it gives me some
  700. 30:24compilation issue that says vehicle is
  701. 30:27abstract cannot be instantiated. Okay,
  702. 30:30so there is a problem with this and the
  703. 30:32problem is that we are actually not
  704. 30:34allowed to create objects of interfaces.
  705. 30:37Okay. Now why why can't we do that?
  706. 30:40Because see logically this start method
  707. 30:43does not have any implementation. There
  708. 30:46is no body of the start method. So if
  709. 30:48you'll create an object of vehicle and
  710. 30:50if you are going to call this method
  711. 30:52then what exactly should be called
  712. 30:54right? Because we don't have any body we
  713. 30:57don't have any implementation. Okay. So
  714. 30:59the compiler won't know what exactly to
  715. 31:02trigger. Okay. So what's the solution?
  716. 31:06So the solution is that we can create a
  717. 31:08child class to implement this vehicle.
  718. 31:11Okay. So let's say I have a class named
  719. 31:13car that implements vehicle. Okay. And
  720. 31:17here we will have to implement this
  721. 31:19method. Let's so this car class will
  722. 31:22override this method vehicle and give
  723. 31:24some implementation. Okay. So let's say
  724. 31:26I'll say system.out.print
  725. 31:29I will say car engine started.
  726. 31:32All right. So now we have an
  727. 31:34implementation for this method. Okay.
  728. 31:38But still if I'm trying to call it using
  729. 31:40the vehicle object, it still gives me an
  730. 31:42error. Now what's the problem? So the
  731. 31:45problem is that even if we create an
  732. 31:48implementation, but still we cannot call
  733. 31:50it using the vehicles object because the
  734. 31:53thing is that what if I create more
  735. 31:55implementations. Okay, let's say I have
  736. 31:57another class bike which also implements
  737. 31:59vehicle. Okay. And here
  738. 32:03let's say I give a different
  739. 32:05implementation. Okay. I'll just say bike
  740. 32:09engine started.
  741. 32:12All right. So now how would the compiler
  742. 32:15know that when we are calling it using a
  743. 32:17vehicle object, which of these two
  744. 32:19implementations should the compiler
  745. 32:21call? Okay. We have not specified that.
  746. 32:23That is why we cannot create an object
  747. 32:25like this. Okay. So what was the
  748. 32:27solution? solution was that we should
  749. 32:30create an object directly of the child
  750. 32:32class. Okay. So the child class one of
  751. 32:34the child class is car. So I'll create
  752. 32:36an object of car and put it in a
  753. 32:38reference of vehicle and using this I
  754. 32:40can call the start method. Similarly I
  755. 32:43can also do the same for bike. Okay I
  756. 32:47can create an object of bike and using
  757. 32:49this I can also call the start method.
  758. 32:52Okay. So if I run it,
  759. 32:56great. You will see that this printed
  760. 32:58car engine started and this one printed
  761. 33:00bike engine started. All right. Now this
  762. 33:03is how things were done before Java 8.
  763. 33:06Now you might observe that whatever we
  764. 33:08did so far is actually containing a lot
  765. 33:11of redundant code. Okay, there is a lot
  766. 33:13of boilerplate code that we actually
  767. 33:15don't need. Why? Because you see we have
  768. 33:18this start method and at the end of the
  769. 33:21day we only want to implement it but
  770. 33:23just to do that we are creating two
  771. 33:25child classes and then we are calling
  772. 33:28methods using those objects. Okay. So I
  773. 33:31think there is a scope of reducing this
  774. 33:33code. All right. So what can I do? I can
  775. 33:37just I let me just first comment this
  776. 33:39out. Okay. And one second I'll just uh
  777. 33:42copy paste this. I'll copy this paste it
  778. 33:45over here. Okay. Okay. So I'll comment
  779. 33:47this part. This was one way of doing
  780. 33:49things. Now we'll look at the second
  781. 33:51method. Okay. So at the end of the day
  782. 33:54all I need is I need some implementation
  783. 33:56here. Okay. So instead of giving it like
  784. 33:58this like writing it like this in new
  785. 34:01car, I'll just simply write new vehicle.
  786. 34:04Okay. And if somehow I give I provide an
  787. 34:07implementation here itself instead of
  788. 34:10creating this class, then my job is
  789. 34:12done. Okay. So how can I do that? I'll
  790. 34:15just simply click here implement methods
  791. 34:17and this is how I will provide an
  792. 34:19implementation here itself. Okay, this
  793. 34:23is called anonymous inner class. All
  794. 34:25right, so we didn't need a separate car
  795. 34:28class. We can provide an anonymous
  796. 34:30implementation here itself. And here I
  797. 34:33can just simply say system.out.trint
  798. 34:35ln. Let's say I want to give an
  799. 34:38implementation of car only. So I say car
  800. 34:40engine started. Okay. So now I won't be
  801. 34:42needing this class car. Okay. Okay, I
  802. 34:44can delete it. Okay. And using this
  803. 34:46v_sub1 now I can call v1 dot start. All
  804. 34:50right. So we didn't create an object of
  805. 34:52class car. We created an object of
  806. 34:54vehicle only. The only thing which was
  807. 34:56missing is the implementation that we
  808. 34:59provided here itself. All right.
  809. 35:01Similarly, I'll do the same for bike. I
  810. 35:04can just say ve and here I will
  811. 35:07implement. Okay. So system.out.print
  812. 35:10print ln let's say bike engine
  813. 35:15started
  814. 35:16all right so v1 dot start v2 dot start
  815. 35:20let me just call this
  816. 35:23great so again using anonymous inner
  817. 35:25classes we can implement these we can
  818. 35:27implement this method okay and now I'll
  819. 35:29also delete the class bike
  820. 35:32okay so now lambda what lambda
  821. 35:35expressions do is that they they are
  822. 35:37actually one step further okay they
  823. 35:39reduce this code even more. That is what
  824. 35:41lambda expression is. It is nothing
  825. 35:43fancy. It is just a way to give
  826. 35:46implementation of a method. All right?
  827. 35:48Inside an inside a functional interface.
  828. 35:51Okay? So let me just copy this also and
  829. 35:54I'll comment this part. This is so this
  830. 35:57was the first way. This is the second
  831. 35:58way of doing things. Now I'll show you
  832. 36:01the third way. Okay. So see the catch
  833. 36:04here is that this vehicle interface
  834. 36:07contains only one abstract method.
  835. 36:09because it's a functional interface. All
  836. 36:11right, that we know and actually that
  837. 36:13compiler also knows. All right, so that
  838. 36:16is why since it's only one method, so do
  839. 36:18we really need to provide all this name
  840. 36:20over here that uh we are implementing
  841. 36:23this method start. We don't need to tell
  842. 36:25the compiler again. It already knows
  843. 36:27that this interface has method start. So
  844. 36:30whichever implementation I'm giving here
  845. 36:32is of will obviously of the method
  846. 36:34start. Okay, so I don't need this code.
  847. 36:38So I won't be needing this. And actually
  848. 36:41uh I already know that it's a vehicle
  849. 36:42reference. So I don't even need to write
  850. 36:44this. Okay. So I'll just remove this as
  851. 36:46well. All right. That's it. This is the
  852. 36:49implementation. And when I'm giving it
  853. 36:52like this, I have to add a hyphen and
  854. 36:54then an arrow. This is what lambda
  855. 36:56represents. This is called lambda. And
  856. 36:58this is my final syntax. All right. Very
  857. 37:02very simple. It reduces the code to a
  858. 37:05very very large extent. you'll see that
  859. 37:08we have given these parenthesis then a
  860. 37:10lambda and here I'm giving the
  861. 37:12implementation so this actually
  862. 37:14represents this only okay the way we
  863. 37:17gave an implementation here similarly I
  864. 37:20can write it in a short way like this
  865. 37:22and this is what called a lambda
  866. 37:23expression and you will see that in this
  867. 37:26method implementation we are only
  868. 37:28writing one statement a single statement
  869. 37:31so in that case I don't even need these
  870. 37:33inside braces okay so I'll just remove
  871. 37:36this and I'll remove one semicolon and
  872. 37:38I'll remove this. All right. So this is
  873. 37:41how I can implement using lambda.
  874. 37:43Similarly, I'll do it again. I'll show
  875. 37:45it to you. First, we remove this method
  876. 37:47name and override. We remove this
  877. 37:49parenthesis. We don't need to tell the
  878. 37:52compiler that it's a vehicle object
  879. 37:53because we have the vehicle reference.
  880. 37:55Okay. So, we remove this here. I will
  881. 37:57add a hyphen then an arrow. And this is
  882. 37:59the implementation. Now, this
  883. 38:01implementation has only one line. So, I
  884. 38:03don't even need this. and I don't need
  885. 38:06this and I don't need this. Okay,
  886. 38:09simple. This is how I will give my
  887. 38:13implementation. All right, so you will
  888. 38:15see that we have done all this only in
  889. 38:18just a single line. Okay, now again I
  890. 38:21want to tell you what it actually
  891. 38:22represents. This line here represents
  892. 38:25the implementation of the start method.
  893. 38:28Okay. So, whatever the start method,
  894. 38:30whatever implementation, whatever body
  895. 38:32you want to provide to the start method,
  896. 38:33that is written here after the lambda.
  897. 38:36Okay. Similarly, here in V2 and I'll run
  898. 38:39it again.
  899. 38:41Great. So, again, car engine started and
  900. 38:43bike engine started. Okay. So, the first
  901. 38:46way is that you create a child class and
  902. 38:48using the object of that class you call
  903. 38:50the method. The second way is that you
  904. 38:52provide an implementation using an
  905. 38:54anonymous inner class. And the third way
  906. 38:56is that you do it using a lambda
  907. 38:59function. Okay, lambda expression. So
  908. 39:01this is just a different type of syntax,
  909. 39:03nothing else. It's very short and it
  910. 39:06only works because vehicle has one
  911. 39:08abstract method. All right? If it would
  912. 39:11have had multiple abstract methods, then
  913. 39:13we could not do it like this because
  914. 39:15then the compiler won't know that which
  915. 39:17method we are talking about because then
  916. 39:19we will have to name the method like we
  917. 39:21named it here. Okay. But since there is
  918. 39:23only one abstract method. So that's why
  919. 39:24we can give an implementation like this.
  920. 39:27All right. Now I think that this topic
  921. 39:30is a little hard to understand
  922. 39:32especially in the beginning. So to
  923. 39:34understand it there is only one way that
  924. 39:36we should practice it a lot. And that's
  925. 39:38why in my video I'll be taking multiple
  926. 39:41examples. I have created four different
  927. 39:43functional interfaces and one by one
  928. 39:45I'll be taking I'll be implementing all
  929. 39:47these interfaces. But still my
  930. 39:49recommendation is that you should
  931. 39:51practice it on your own as well. Okay,
  932. 39:54create some functional interfaces, do
  933. 39:56some implementations of it, read some
  934. 39:58examples and eventually you'll get a
  935. 40:00hold of it. All right, but in my video
  936. 40:03as well, I'll cover a lot of examples
  937. 40:05and I hope that it will be very helpful.
  938. 40:08Okay, so this is the first interface.
  939. 40:10This is an interface named greeting and
  940. 40:13it has this abstract method. Okay. So
  941. 40:15I'll implement it the same way I've
  942. 40:17implemented the vehicle interface. Okay.
  943. 40:19Now the trick is that always first write
  944. 40:23the interface name. All right. Because
  945. 40:26see every time you won't do it like
  946. 40:27this. You won't be creating a child
  947. 40:29class first. Then you will be creating
  948. 40:31an anonymous class and then breaking
  949. 40:33down that code. Okay? You should
  950. 40:35directly write a lambda function. Okay?
  951. 40:37At some point you should know how to
  952. 40:39write it directly. Okay? So I'll just
  953. 40:42tell you the trick for that. So first
  954. 40:44write the interface name then give your
  955. 40:46object some name let's say I say G1
  956. 40:50and after that just check how many
  957. 40:52arguments does this method have so as of
  958. 40:55now it does not have any argument so
  959. 40:57simply just add a parenthesis add the
  960. 41:00lambda the hyphen and an arrow and
  961. 41:02always keep make it an habit always give
  962. 41:06these curly braces in the beginning okay
  963. 41:08always first give this curly braces and
  964. 41:10a semicolon write it like this now This
  965. 41:13is the area where you're going to give
  966. 41:15your implementation. Okay. This is where
  967. 41:18this method greet will be implemented.
  968. 41:21All right. So let's say I simply write a
  969. 41:24statement system.out.print
  970. 41:27hello everyone. Okay. A simple print
  971. 41:31statement and that's it. You have given
  972. 41:33an implementation for the greet method.
  973. 41:36Now after that check that whether this
  974. 41:39implementation is a single line
  975. 41:40implementation. Yes. In this case it is
  976. 41:43a single line. We are only printing
  977. 41:44something. So in that case you can
  978. 41:46remove this. You can remove this and you
  979. 41:49can remove this semicolon. Okay. So this
  980. 41:52is the whole implementation.
  981. 41:55All right. Now using this G1 you can
  982. 41:57call the grid method. All right. Now
  983. 42:01similarly let's take one more example.
  984. 42:03We have an interface named task and the
  985. 42:06method is execute. Okay. So this time
  986. 42:10again I'll take task let's say I name
  987. 42:14the object t1 is equal to in this task
  988. 42:18again we don't have any argument. So
  989. 42:20I'll simply take a parenthesis then a
  990. 42:23lambda and again initially I will add
  991. 42:25these curly braces and a semicolon. Now
  992. 42:28let's say I want to write two statements
  993. 42:30here. Okay. first is that task started
  994. 42:35then
  995. 42:36something like let's say task ended okay
  996. 42:39simple
  997. 42:41so in this case I cannot remove these
  998. 42:43braces okay if I want both these
  999. 42:46statements to be a part of the
  1000. 42:48implementation of this execute then I
  1001. 42:50will have to write both these statements
  1002. 42:52inside the curly braces all right that's
  1003. 42:55the rule so this is how you implement a
  1004. 42:58multi-line implementation you give a
  1005. 43:00multi-line implementation But in case of
  1006. 43:01a single line you can remove these
  1007. 43:03braces as well as this semicolon. Okay.
  1008. 43:07So again using this T1 I can call
  1009. 43:09T1.execute.
  1010. 43:11All right. Cool. Now coming to the third
  1011. 43:14example. This is an interface square.
  1012. 43:16And let me just also add this
  1013. 43:18annotation. Okay. Even though if I
  1014. 43:21didn't add it, it would have worked
  1015. 43:22because there is only a single method.
  1016. 43:24But I still still it's recommended. So I
  1017. 43:26like to add it. All right. So we have
  1018. 43:28this interface. It has this method
  1019. 43:30operate. Now this time the return type
  1020. 43:32is int and we are also taking an
  1021. 43:35argument. All right. Now that is an
  1022. 43:37interesting example. So first always
  1023. 43:40write the interface name. Let's say
  1024. 43:42square s1 is equal to this time we have
  1025. 43:47an argument. Okay we have an argument.
  1026. 43:50So here I will take an argument. All
  1027. 43:52right, like this number and I'll write
  1028. 43:56the lambda and then the curly braces.
  1029. 43:59Okay, so we will have to take an
  1030. 44:01argument because here in the method uh
  1031. 44:04in the method square we have this
  1032. 44:05argument. Okay, now this number using
  1033. 44:08this number I'm saying return
  1034. 44:12number
  1035. 44:13x number. Okay, I'm returning the square
  1036. 44:16and put a semicolon here as well. All
  1037. 44:18right. So now this is the
  1038. 44:20implementation. Okay. So is there any
  1039. 44:24scope of reducing this further? Actually
  1040. 44:26yes there is. First let me just clarify
  1041. 44:28one more point. I'll reduce it as well.
  1042. 44:30But let me just clarify one more point.
  1043. 44:32So the theme the thing is when you see
  1044. 44:35the code like this many people get
  1045. 44:37confused. But the thing is that since we
  1046. 44:39are returning something here let's say
  1047. 44:41the number is 10. Okay. So we are
  1048. 44:43returning something here. So people
  1049. 44:45think that uh the value 100 that will be
  1050. 44:47returned and will be saved in this
  1051. 44:49object. But that is not true. The return
  1052. 44:53type the returning thing is of this
  1053. 44:55method which is this square which is
  1054. 44:57this operate. Okay. So when you are
  1055. 45:00actually calling the operate method at
  1056. 45:02that point of time this code will be
  1057. 45:04triggered. Okay. So whatever you are
  1058. 45:06returning here that is not getting
  1059. 45:07returned here in S1 that will be
  1060. 45:09returned here. I'll just print it.
  1061. 45:12System.out.print print ln.
  1062. 45:14Okay. So when you actually call the
  1063. 45:16method at that point of time, this
  1064. 45:19implementation will be called. All
  1065. 45:21right. This is how it is working.
  1066. 45:23Imagine it like this. At this point, you
  1067. 45:25have only given the implementation. You
  1068. 45:27have not actually triggered any code
  1069. 45:29because the method is actually getting
  1070. 45:31hit at this point of time when you are
  1071. 45:33actually calling the operate method. All
  1072. 45:35right? So just please think of it like
  1073. 45:37this. I wanted to make it clear. Okay.
  1074. 45:40Anyways, so in the operate method here I
  1075. 45:43will pass the value which is 10. This
  1076. 45:46value this 10 will be passed on here and
  1077. 45:49it will actually return 10 into 10. So
  1078. 45:52100. Okay. So this will be returned here
  1079. 45:54in the print statement. All right. Now
  1080. 45:57okay can I further optimize it? Yes.
  1081. 46:00How? First this has this also has only
  1082. 46:02one line. Correct. So I can remove this.
  1083. 46:06I can remove this and I can remove this.
  1084. 46:09All right. Now there is one rule. If
  1085. 46:11inside this single line there is a
  1086. 46:13return statement and you remove the
  1087. 46:15curly braces then you also have to
  1088. 46:17remove this return. Okay, compiler
  1089. 46:20already knows that you are returning
  1090. 46:21something. So the implementation will be
  1091. 46:23like this. There is a number and you are
  1092. 46:25returning a square of number. Now this
  1093. 46:27is just an implementation. This will
  1094. 46:29actually be triggered when you'll call
  1095. 46:30the operate method which is here. Okay.
  1096. 46:34And there is one more optimization. If
  1097. 46:36there is a single parameter in that case
  1098. 46:38you're also allowed to remove these
  1099. 46:40parenthesis. Okay. So please remember
  1100. 46:43that if there is a single parameter you
  1101. 46:45can also remove this parenthesis. So we
  1102. 46:47will just write number and return number
  1103. 46:49into number. All right great. Let me
  1104. 46:53just run it so far so that you can see.
  1105. 46:57Okay. So when G1.Greet was called this
  1106. 47:00statement was triggered and hello
  1107. 47:02everyone was printed. When T1.execute
  1108. 47:04execute was called these two statements
  1109. 47:07were triggered and task started task
  1110. 47:08ended was printed. Similarly when
  1111. 47:11s1operate was called then only this
  1112. 47:14square was returned in this from this
  1113. 47:16operate method and whatever value was
  1114. 47:18returned we are printing it. That's why
  1115. 47:20100 was printed. Okay.
  1116. 47:23Now let me take one more example. This
  1117. 47:26time we have two arguments. Okay. And we
  1118. 47:28are returning some int value. All right.
  1119. 47:31So I'll take this example. I'll write
  1120. 47:35the interface name calculator. Give
  1121. 47:37object some name. Let's say C1 is equal
  1122. 47:39to since there are two arguments. So I
  1123. 47:42will have to take it like this. Let's
  1124. 47:43say a comma b. Okay. You can name
  1125. 47:46anything. You can name the argument
  1126. 47:47anything. Like here it's num one and num
  1127. 47:50two. But here you can say a comma b as
  1128. 47:53well. There is no issue in that. All
  1129. 47:55right. Now an hyphen and an arrow
  1130. 47:59representing lambda. then this
  1131. 48:01parenthesis and this semicolon. Okay, so
  1132. 48:04I could just directly return a + b but
  1133. 48:07let's say I'm taking it like this int
  1134. 48:10result is equal to a + b and I'm also
  1135. 48:14printing it system.out.tprint ln let's
  1136. 48:17say result
  1137. 48:20okay which is this result
  1138. 48:25and then I'm also returning this result
  1139. 48:29like this. So this implementation is not
  1140. 48:32a single line implementation. That's why
  1141. 48:35I should not remove these curly braces.
  1142. 48:37And has this code been called yet? No.
  1143. 48:41Because as of now we have only provided
  1144. 48:43the implementation which is there in the
  1145. 48:44C1. But when I'll call it using C1 C1.
  1146. 48:49Let's say 10, 20. At this point of time,
  1147. 48:52this code will be triggered. Okay. And
  1148. 48:54since I have this print statement here
  1149. 48:56only, so I will not print it again. Let
  1150. 48:58me just show you.
  1151. 49:00Great. So this statement got triggered.
  1152. 49:03Result is 30. Okay. The sum of A and B.
  1153. 49:06So you passed 10 and 20 in this add
  1154. 49:09method. So these were 10 was equal to A.
  1155. 49:1220 was equal to B. These were saved here
  1156. 49:14in the result and then it was printed
  1157. 49:16here and also returned back. So where is
  1158. 49:19it returned? It will be returned here.
  1159. 49:21Okay. I can add it here as well. Int
  1160. 49:23final result something like this is
  1161. 49:26equal to C1. If I'll print this, this
  1162. 49:29will also be equal to 30. All right.
  1163. 49:32Hello and welcome to my channel, The
  1164. 49:34Curious Coder. This video is part of my
  1165. 49:37Java 8 series where we are exploring the
  1166. 49:39most important Java 8 concepts that
  1167. 49:42every developer should know, especially
  1168. 49:44for interviews and real world
  1169. 49:46development. In today's video, we are
  1170. 49:48going to understand four very important
  1171. 49:50functional interfaces. Predicate,
  1172. 49:53consumer, function, and supplier. These
  1173. 49:56interfaces are heavily used with lambda
  1174. 49:59expressions and streams API and once you
  1175. 50:02understand them properly a lot of modern
  1176. 50:04Java code will start making much more
  1177. 50:06sense. Let's start with predicate.
  1178. 50:09Predicate is a predefined functional
  1179. 50:11interface that takes one argument and
  1180. 50:14returns a boolean value. It is mainly
  1181. 50:16used for conditional testing. All right.
  1182. 50:20So this is the interface predicate. As
  1183. 50:22you can see it is a functional
  1184. 50:23interface. It is predefined inside this
  1185. 50:26package and it contains this abstract
  1186. 50:29method named test. All right. So this
  1187. 50:33test takes only one argument and it
  1188. 50:36returns a boolean value. Okay. And this
  1189. 50:39argument can be of any data type.
  1190. 50:41Whatever is passed here in the generics.
  1191. 50:44All right. So we have this flexibility
  1192. 50:46that we can pass any data type inside
  1193. 50:48this generics and the argument will be
  1194. 50:50of the same data type. Okay. So let's
  1195. 50:53see how we can implement it. The rules
  1196. 50:55are same. First we'll write the
  1197. 50:56interface name like this. And let's say
  1198. 50:59in the generics I want my argument to be
  1199. 51:02of the type integer. Okay. I'll give it
  1200. 51:05some name. Let's say P1 is equal to. Now
  1201. 51:08since this method takes one argument. So
  1202. 51:12here also I will have to take one
  1203. 51:14argument. Okay. So I can name it
  1204. 51:15anything. Let's say I name it num
  1205. 51:17representing some number. All right.
  1206. 51:20After that I'll put an arrow and you'll
  1207. 51:23see that IntelligJ has already shown
  1208. 51:25that it is of the type integer because
  1209. 51:27here in the generics we passed it as
  1210. 51:28integer and whatever we pass here will
  1211. 51:31be the data type of the argument. Okay.
  1212. 51:34And here in the parenthesis this is the
  1213. 51:35argument that we are taking. All right.
  1214. 51:38So this is the number and let's say I
  1215. 51:42want to give some first let me add these
  1216. 51:44braces and semicolon like this and here
  1217. 51:47I will give the implementation of this
  1218. 51:49method test. Okay, I'll give some
  1219. 51:51implementation. So let's say I want to
  1220. 51:54check whether this number whatever we
  1221. 51:55are passing is even or not. Okay. So I
  1222. 51:58can simply give an give a condition if
  1223. 52:01num
  1224. 52:02mod 2 okay like this is equal to zero
  1225. 52:06then we should
  1226. 52:09then we should return true
  1227. 52:12all right
  1228. 52:14else we can
  1229. 52:17return false.
  1230. 52:20Okay so this is the implementation of
  1231. 52:23this method test. Okay, it is taking one
  1232. 52:27argument number and we are saying if the
  1233. 52:30number is even then return true
  1234. 52:31otherwise return false. Okay, so we are
  1235. 52:33checking some condition that whether a
  1236. 52:35number is even or not. Okay, now this is
  1237. 52:38just an implementation. This method will
  1238. 52:40actually be called when I'll call it
  1239. 52:42like this P1.est and here I'll actually
  1240. 52:46pass some value inside the argument. So
  1241. 52:48let's say I'm testing 10 and I'm testing
  1242. 52:52seven. Okay, P1.est 7.
  1243. 52:56All right. So, let me also print the
  1244. 52:58result. System.out.print.
  1245. 53:04Similarly, here as well.
  1246. 53:12All right. Okay. Yeah. So, if I run my
  1247. 53:15code,
  1248. 53:18great. You'll see that this returns true
  1249. 53:20because 10 was an even number and se
  1250. 53:22this returns false. Okay, so this is how
  1251. 53:25we can implement predicate interface. We
  1252. 53:28can give an implementation for this
  1253. 53:29method test and uh it will just return
  1254. 53:32it will take some argument and it will
  1255. 53:34return some boolean value. Okay. Now
  1256. 53:37I'll take one more example. This time
  1257. 53:39I'll take some string argument predicate
  1258. 53:42string and let me call it P2 is equal to
  1259. 53:47this time also I have to take one
  1260. 53:48argument. Let's say I'm calling it some
  1261. 53:50str
  1262. 53:52then this arrow and these parentheses
  1263. 53:55these curly braces here I will provide
  1264. 53:57the implementation. So I'm trying to say
  1265. 53:59if string is empty if the string is
  1266. 54:03empty then return true. Okay. Else
  1267. 54:09return false.
  1268. 54:12Okay. This is the implementation. So
  1269. 54:15here in this condition I'm checking
  1270. 54:17whether or not the string is empty.
  1271. 54:20Okay. Now using this P2 as well I will
  1272. 54:22call the method P2 dot test and inside
  1273. 54:26this test I can pass an empty string and
  1274. 54:29also I can pass some not empty string.
  1275. 54:32Okay let's say my name. All right
  1276. 54:36I'll simply print this system.out
  1277. 54:39dot print
  1278. 54:42test.
  1279. 54:43Similarly, system.out.print
  1280. 54:46ln
  1281. 54:48this one.
  1282. 54:50Okay. So if I run my application,
  1283. 54:54great. We got true false then true
  1284. 54:56false. So true for this, false for this.
  1285. 54:59Now since it's an empty string, so it
  1286. 55:01returns true and then it returns false.
  1287. 55:04All right. So this is how predicate
  1288. 55:06works. Now I also want to tell you that
  1289. 55:08we can optimize this a little bit more.
  1290. 55:11Like we have written a lot of lines
  1291. 55:12here. We could have simply written it
  1292. 55:14like this.
  1293. 55:17Return str is empty. Okay, we could have
  1294. 55:21directly written this. Similarly, here
  1295. 55:23as well, I could have return
  1296. 55:27num
  1297. 55:30mode 2 is equal to zero. Okay, I can
  1298. 55:32directly write it like this. Now there
  1299. 55:34is one thing in this uh functional
  1300. 55:37interface and lambda expression that
  1301. 55:39when the implementation you are giving
  1302. 55:41is of a single line like here you can
  1303. 55:43see it's just a single line in that case
  1304. 55:46you can remove this you can remove this
  1305. 55:48and you can remove this semicolon as
  1306. 55:50well okay it will be simply written like
  1307. 55:52this and you can also remove this return
  1308. 55:55keyword okay so actually you'll write it
  1309. 55:58simply like this so you're saying num
  1310. 56:00and then arrow and then num mode 2 equal
  1311. 56:02to equal to0 we'll directly give the
  1312. 56:04condition. Similarly, here as well, I
  1313. 56:06will remove this and remove this. You
  1314. 56:09can return statement as well. So, I'm
  1315. 56:11able to do this because the statement is
  1316. 56:13of single line. Okay? We are not doing
  1317. 56:15anything else. All right? So, this is
  1318. 56:18all about predicate.
  1319. 56:20Now, let's understand function. Function
  1320. 56:23is a predefined function interface that
  1321. 56:25takes one input and returns some output.
  1322. 56:29It is basically used for transforming
  1323. 56:31the data. All right. So this is the
  1324. 56:33interface function. As you can see it is
  1325. 56:36also a function interface and it has
  1326. 56:39this abstract method named apply. Okay.
  1327. 56:42Now here as well we can take one
  1328. 56:44argument but unlike predicate the return
  1329. 56:47type is not fixed as boolean. Okay.
  1330. 56:50Instead we can take any return type and
  1331. 56:52we can pass it here in the generics. All
  1332. 56:54right. So it has the flexibility of
  1333. 56:56passing the data type for the argument
  1334. 56:58as well as for the return type. Okay. So
  1335. 57:02let's see some examples.
  1336. 57:05I'll just write the interface name. Now
  1337. 57:08let's say I have some string value and I
  1338. 57:11want to return the length of that
  1339. 57:12string. Okay. So I'll take the argument
  1340. 57:15type as string and since I want to
  1341. 57:18return the length, so I'll take the
  1342. 57:20return type as integer. Okay. Then I can
  1343. 57:22give some name to the object. Now here
  1344. 57:25as well we have only one argument. So
  1345. 57:27I'll take the I'll take a single
  1346. 57:29argument like this. str then arrow and
  1347. 57:33curly braces. Okay. Now, whatever I
  1348. 57:36provide here will be the implementation
  1349. 57:38of this method apply. Okay. So, I want
  1350. 57:42to return strlength.
  1351. 57:46Okay. Simple. Now using this f_sub_1 I
  1352. 57:51can call apply. Let's say the string is
  1353. 57:54shimum. Okay. So I'm passing a string
  1354. 57:56value and I want to return its length.
  1355. 57:59Okay. So whatever is returned will be
  1356. 58:01printed here. Okay. I'll just print
  1357. 58:03this.
  1358. 58:06All right. Okay. So let me run the
  1359. 58:08project first. Let me just comment these
  1360. 58:11out. Okay. We have already seen
  1361. 58:13predicate.
  1362. 58:15Cool. So if I run the application.
  1363. 58:18Great. So the length of this string is
  1364. 58:20printed which is six. Okay. Now again
  1365. 58:23here we have a scope of optimization.
  1366. 58:25Since it's a single line, I could just
  1367. 58:27remove this. Remove this and this
  1368. 58:30semicolon as well. And we also don't
  1369. 58:32need this return keyword. Okay. So,
  1370. 58:34simply we can write it like this. All
  1371. 58:37right.
  1372. 58:39Now, I'll take one more example. Let's
  1373. 58:41say I have some number, I have some
  1374. 58:43value and I want to return the square of
  1375. 58:46that value. Okay. So, I'll take another
  1376. 58:48function and I have some integer value
  1377. 58:52and I want to return some integer value
  1378. 58:54only. Okay. Okay, I want to return a
  1379. 58:55square root. Not a square root, a
  1380. 58:57square. Okay, let me call it f_sub_2.
  1381. 59:00And we have some number.
  1382. 59:03I'll first write it like this only.
  1383. 59:05Okay. And I want to return number into
  1384. 59:09number. Okay. Now using this f_sub_2, I
  1385. 59:12will call f_sub_2 do.apply and I'll pass
  1386. 59:1610. All right. So this is the number
  1387. 59:19that I passed which will be this one.
  1388. 59:21And in the return basically in this
  1389. 59:23implementation I want to return the
  1390. 59:25square. Okay. Now again this is a single
  1391. 59:28line. So I'll remove this. I'll remove
  1392. 59:30this. And I'll also remove the return
  1393. 59:32keyword. Okay. So simply I'll write it
  1394. 59:35like this. All right. Now let me run my
  1395. 59:37application again. Okay. I've not
  1396. 59:39printed this. Let me print it.
  1397. 59:48Great. So you can see that we pass 10
  1398. 59:50and 100 was returned. Okay. So this is
  1399. 59:54why we use function. It is mainly used
  1400. 59:57to transform data and whenever we want
  1401. 59:59to map one data into another, we can use
  1402. 1:00:02function.
  1403. 1:00:03Now what is consumer? Consumer is a
  1404. 1:00:06predefined functional interface that
  1405. 1:00:08takes one input but returns nothing. It
  1406. 1:00:12is mainly used to perform any operation
  1407. 1:00:14where you don't need to return anything.
  1408. 1:00:17All right. So this is the consumer
  1409. 1:00:19interface as you can see it takes one
  1410. 1:00:21argument but it has no return type.
  1411. 1:00:25Okay. So this is the abstract method
  1412. 1:00:26except that takes one argument but there
  1413. 1:00:28is no return type. All right. So let me
  1414. 1:00:32show you some examples.
  1415. 1:00:34Let's say I just simply create consumer
  1416. 1:00:38and I want to have some string argument.
  1417. 1:00:42Okay.
  1418. 1:00:44So I will take some argument. Let's say
  1419. 1:00:46the argument is name. Okay, I'm giving
  1420. 1:00:48it its name is name. And simply I want
  1421. 1:00:52to print this name. Okay, I don't want
  1422. 1:00:54to return anything. I simply want to
  1423. 1:00:56print my name is
  1424. 1:01:01name. Okay, like this. All right, so
  1425. 1:01:06this is what consumer is used for. I
  1426. 1:01:08took some argument but I didn't want to
  1427. 1:01:10return anything. Okay. Then using this I
  1428. 1:01:13will just call C1 dot accept and I will
  1429. 1:01:16print the name. Okay. Like this.
  1430. 1:01:20All right. Now again this is a single
  1431. 1:01:23line implementation. So I'll remove this
  1432. 1:01:26and simply write it like this. Okay.
  1433. 1:01:30Similarly I can also take a different
  1434. 1:01:32argument. I could have taken integer.
  1435. 1:01:36Okay. C2 is equal to and I could have
  1436. 1:01:40taken my age
  1437. 1:01:42like this and I can again simply print
  1438. 1:01:45it. Okay, this time I'll not take the
  1439. 1:01:46curly braces. I'll give a single line
  1440. 1:01:47implementation.
  1441. 1:01:49Let's say I'm saying my age is something
  1442. 1:01:54okay so my age is age okay and here I
  1443. 1:01:57will pass it C2 dot accept 28. Okay,
  1444. 1:02:02like this. All right, I'll just run it.
  1445. 1:02:06Right? So both these statements are
  1446. 1:02:09printed. Okay. When C1 do.ccept is
  1447. 1:02:11called, this statement will be printed.
  1448. 1:02:13Shivam will be passed as argument over
  1449. 1:02:15here and here it will be printed.
  1450. 1:02:17Similarly, when C2 dot accept is called,
  1451. 1:02:20this number is passed as a value here
  1452. 1:02:23and this is printed here. Okay. So
  1453. 1:02:26nothing is returned from uh this. Okay.
  1454. 1:02:28There is no return type when you call
  1455. 1:02:30these two methods.
  1456. 1:02:32All right. Now at last we have supplier.
  1457. 1:02:36Supplier is a predefined functional
  1458. 1:02:37interface that does not take any input
  1459. 1:02:40but returns some value. It is mainly
  1460. 1:02:43used when you want to generate or supply
  1461. 1:02:45data. All right. So this is the supplier
  1462. 1:02:48interface. As you can see it has this
  1463. 1:02:51method get but it does not take any
  1464. 1:02:53argument. However, it returns some
  1465. 1:02:56value. Okay. So it is kind of opposite
  1466. 1:02:59to consumer. consumer takes value but
  1467. 1:03:02does not have a return type. However,
  1468. 1:03:04supplier does not take any value and has
  1469. 1:03:07some return type. Okay, so let me
  1470. 1:03:09implement this first. I'll just comment
  1471. 1:03:12this out. Okay, so supplier let's say I
  1472. 1:03:16want to return some integer value. Okay,
  1473. 1:03:20so like this I'll just call it. Now
  1474. 1:03:22since it does not have any argument so I
  1475. 1:03:24leave the parenthesis like this and
  1476. 1:03:27arrow
  1477. 1:03:30let's say I want to return some random
  1478. 1:03:32number okay I'm just simply calling I
  1479. 1:03:35simply say return math dot random okay
  1480. 1:03:40like this now this returns double so
  1481. 1:03:43I'll just cast it to int okay like this
  1482. 1:03:48all right now using this I will call s1
  1483. 1:03:51dot get. Okay. And we will just print
  1484. 1:03:54the output
  1485. 1:03:57system.out.t
  1486. 1:03:59print.
  1487. 1:04:02All right. So we didn't take any
  1488. 1:04:04argument but this implementation
  1489. 1:04:07returned some random number. Okay. Let
  1490. 1:04:10me call it. Great. So it returned zero.
  1491. 1:04:13If I'll call it again. Okay. So random
  1492. 1:04:15number is again and again printing zero
  1493. 1:04:17only. All right. Now I can take one more
  1494. 1:04:21example. Let's say this time I want to
  1495. 1:04:23return some string value.
  1496. 1:04:25So let's call it s_2.
  1497. 1:04:30Okay. And I have a string s string a is
  1498. 1:04:35equal to let's say sham
  1499. 1:04:37string
  1500. 1:04:40b is equal to gupta.
  1501. 1:04:45And I want to return a concatenated
  1502. 1:04:48result. Okay. A do.at B. All right. So
  1503. 1:04:53this supplier takes no argument inside
  1504. 1:04:56the implementation. There are two string
  1505. 1:04:58values and we are returning a
  1506. 1:04:59concatenated result. Okay. So when I'll
  1507. 1:05:02call the method s2.get
  1508. 1:05:05this value will be returned. Okay.
  1509. 1:05:08Simply just print this
  1510. 1:05:11system.out.t
  1511. 1:05:14print
  1512. 1:05:16H2.get get okay I'll run it again
  1513. 1:05:21so the concatenated string value is
  1514. 1:05:23returned all right also here I could
  1515. 1:05:26have removed these braces and this
  1516. 1:05:29return as well okay we can simply write
  1517. 1:05:31it like this but here I cannot do this
  1518. 1:05:33because this is not a single line
  1519. 1:05:35implementation
  1520. 1:05:37all right so these were four interfaces
  1521. 1:05:40that are used very widely in Java 8 and
  1522. 1:05:43uh actually when we'll study about
  1523. 1:05:45streams as well there we'll will see the
  1524. 1:05:47use cases of these. So, stream has many
  1525. 1:05:50methods where we use these directly.
  1526. 1:05:53All right. Hello and welcome to my
  1527. 1:05:55channel, the curious coder. This video
  1528. 1:05:58is part of my ongoing Java 8 series.
  1529. 1:06:01Today, I'll be talking about a very
  1530. 1:06:03important but a little tricky topic,
  1531. 1:06:06method reference. So, what exactly is a
  1532. 1:06:09method reference? Simply put, it's a
  1533. 1:06:12shorter way to implement a functional
  1534. 1:06:13interface. But wait, doesn't lambda do
  1535. 1:06:16the same thing? Yes, it does. However,
  1536. 1:06:20at times we can make the syntax even
  1537. 1:06:23more optimized using method references.
  1538. 1:06:26I know it sounds a bit confusing in the
  1539. 1:06:28beginning. That's why in this video I'll
  1540. 1:06:30explain it in the simplest way possible
  1541. 1:06:32and the video will be full of examples
  1542. 1:06:35to help you understand it easily. Okay,
  1543. 1:06:38so let's get started. So I have created
  1544. 1:06:41a functional interface named calculator.
  1545. 1:06:44Now what is a functional interface? Any
  1546. 1:06:46interface that contains exactly one
  1547. 1:06:48abstract method is called a functional
  1548. 1:06:51interface. All right. So this interface
  1549. 1:06:54contains only one abstract method named
  1550. 1:06:56calculate. That is why this can be
  1551. 1:06:58considered as a functional interface.
  1552. 1:07:01Okay. Now we can implement this method
  1553. 1:07:04calculate in a very short way using
  1554. 1:07:06lambda expressions and sometimes we can
  1555. 1:07:09make it even shorter using method
  1556. 1:07:11reference. Okay. So let me show you.
  1557. 1:07:14Let's first see how we can do it using
  1558. 1:07:16lambda expressions. Okay. So the syntax
  1559. 1:07:18is very simple. First we need to write
  1560. 1:07:20the interface name like this. Then we'll
  1561. 1:07:24give some name to the object. Okay. Then
  1562. 1:07:28since in this method there are two
  1563. 1:07:30arguments. So here as well I will have
  1564. 1:07:32to take two arguments. Okay. Like this.
  1565. 1:07:35After that we use the lambda operator.
  1566. 1:07:37This arrow operator. Okay. And after
  1567. 1:07:40these we put some curly braces. Okay. we
  1568. 1:07:42put these curly braces and end with a
  1569. 1:07:44semicolon. All right. Now, whatever you
  1570. 1:07:47write between these two curly braces,
  1571. 1:07:49whatever you write here actually
  1572. 1:07:51represents the implementation of this
  1573. 1:07:54method calculate. Okay. So, let's say I
  1574. 1:07:57just want to add these two. Okay. So,
  1575. 1:07:59I'll say return a plus b. Okay. Like
  1576. 1:08:04this. So, this part this part is
  1577. 1:08:08actually the implementation of this
  1578. 1:08:10method calculate. Okay. So if I run
  1579. 1:08:13this, if I run this code, will any does
  1580. 1:08:15anything happen? Let me see.
  1581. 1:08:20All right. So as of now, nothing will
  1582. 1:08:22happen because we have only declared the
  1583. 1:08:24implementation. We have not actually
  1584. 1:08:26called this method calculate. Okay. This
  1585. 1:08:28has not been triggered yet. So for that
  1586. 1:08:31we have this object C1. And using this
  1587. 1:08:33C1, we can call this calculate method.
  1588. 1:08:37And here I can pass some values. Okay.
  1589. 1:08:40So as you can see I passed 10 and 20. So
  1590. 1:08:42this 10 will be passed on over here and
  1591. 1:08:4520 inside b and the result will be a + b
  1592. 1:08:48which we'll get here. Okay. Because here
  1593. 1:08:50is where we are actually calling the
  1594. 1:08:52method. All right. So I'll print the
  1595. 1:08:54result.
  1596. 1:08:58All right.
  1597. 1:09:00Great. So you can see that the sum of 10
  1598. 1:09:02and 20 has been printed. All right. So
  1599. 1:09:05this is how lambda expressions work. Now
  1600. 1:09:08can I make it make this syntax even
  1601. 1:09:10shorter and convert it into a method
  1602. 1:09:12reference? So actually no. I cannot
  1603. 1:09:16convert this lambda expression into a
  1604. 1:09:18method reference. Why? Because method
  1605. 1:09:20reference has two conditions. Number one
  1606. 1:09:23that the implementation should only have
  1607. 1:09:25a single statement. All right, which in
  1608. 1:09:27this case is true. You can see that in
  1609. 1:09:29inside this implementation we only have
  1610. 1:09:31a single statement. But there is a
  1611. 1:09:33second condition that says that there
  1612. 1:09:36should be some method call happening in
  1613. 1:09:38that statement. Okay. So is a method
  1614. 1:09:41call happening here? No. We are actually
  1615. 1:09:43just taking two arguments and we are
  1616. 1:09:45adding those. Okay. So it does not
  1617. 1:09:48qualify for method reference. All right.
  1618. 1:09:51Now let me change this example a little
  1619. 1:09:53bit. What I'm going to do is I have this
  1620. 1:09:56class math operations. Inside this we
  1621. 1:09:59have this method add. It is a static
  1622. 1:10:02method and it takes two argument and it
  1623. 1:10:04returns the sum of those two values.
  1624. 1:10:07Okay. So instead of directly just
  1625. 1:10:10returning a plus b, I'll change the
  1626. 1:10:12example. What I'll do? I'll just call
  1627. 1:10:14math operations dot add. And let's say I
  1628. 1:10:18pass a comm, b.
  1629. 1:10:21Okay. So we have changed the
  1630. 1:10:22implementation a little bit. Now there
  1631. 1:10:25is a single statement and inside this
  1632. 1:10:27statement there is a method call
  1633. 1:10:30happening. Okay, we are calling this
  1634. 1:10:32method add which belongs to this class
  1635. 1:10:34math operations. Okay, so now is it
  1636. 1:10:37eligible for method reference? Yes, now
  1637. 1:10:40it is actually eligible for method
  1638. 1:10:42reference. So let me show you the
  1639. 1:10:44syntax. What I can do is instead of
  1640. 1:10:47writing all this, we just have to write
  1641. 1:10:50class name colon method name. I'll
  1642. 1:10:53repeat the syntax is class name colon
  1643. 1:10:56method name. Okay. So again I'll give
  1644. 1:11:00the interface name. Let's name this
  1645. 1:11:02object as C2. And instead of writing all
  1646. 1:11:05this, okay, instead of writing all this,
  1647. 1:11:07what I need to do is I'll just write
  1648. 1:11:09class name then colon and then this
  1649. 1:11:12method name. Okay, like this and end
  1650. 1:11:16with a semicolon. That's it. This
  1651. 1:11:19actually represents this much. Okay. And
  1652. 1:11:22now using this C2, I will call the
  1653. 1:11:25method calculate. And again I'll pass
  1654. 1:11:27the value 10, 20. Okay. And let's just
  1655. 1:11:31print this as well.
  1656. 1:11:36All right. Like this. Okay. Now if I'll
  1657. 1:11:40run my application.
  1658. 1:11:42Great. So you can see that this was the
  1659. 1:11:45implementation that was done using
  1660. 1:11:46lambda and this is the implementation
  1661. 1:11:49that is done using method reference.
  1662. 1:11:51Okay. So the compiler automatically
  1663. 1:11:54knows that whatever value you are
  1664. 1:11:56passing here like you're passing 10 and
  1665. 1:11:5820 that has to be populated inside this
  1666. 1:12:00method add. Okay. So you don't need to
  1667. 1:12:03specify the arguments here. This is the
  1668. 1:12:05syntax. Okay. You just pass the value
  1669. 1:12:08and compiler automatically knows that
  1670. 1:12:10this add contains two arguments and the
  1671. 1:12:13first one will go inside A and the
  1672. 1:12:15second one will go inside B. Okay, this
  1673. 1:12:17is how you write it. So the condition is
  1674. 1:12:19that there should be a single statement
  1675. 1:12:21and inside that statement there should
  1676. 1:12:24be a method call that is happening.
  1677. 1:12:26Okay. Now there are actually four
  1678. 1:12:29different types of method references.
  1679. 1:12:31Static method reference, instance method
  1680. 1:12:33reference of a particular object,
  1681. 1:12:36instance method reference of an
  1682. 1:12:38arbitrary object and constructor method
  1683. 1:12:40reference. So one by one let's
  1684. 1:12:43understand each of them. First one is
  1685. 1:12:45static method reference. It is used when
  1686. 1:12:48we are calling a static method directly
  1687. 1:12:50by the class not by an object. So the
  1688. 1:12:53syntax is class name colon static method
  1689. 1:12:57name. All right let's look at it. So the
  1690. 1:13:00example that I just covered here
  1691. 1:13:02actually belongs to the static method
  1692. 1:13:03reference because this add was a static
  1693. 1:13:07method inside this class math
  1694. 1:13:09operations. Okay. And you can see that
  1695. 1:13:11we have directly called this method
  1696. 1:13:13using math operations. Okay. So this is
  1697. 1:13:16a static method reference. Okay. Now
  1698. 1:13:19I'll take one more example. Let's say I
  1699. 1:13:22have one more method inside this class.
  1700. 1:13:25public static int
  1701. 1:13:28multiply
  1702. 1:13:31int a comma int b. Okay. And it returns
  1703. 1:13:35a into b. All right. So what I'm going
  1704. 1:13:39to do is again I'll first implement
  1705. 1:13:41using lambda and then we'll see how we
  1706. 1:13:44can do it using method reference. Okay.
  1707. 1:13:46So this is how I'm going to do in the
  1708. 1:13:48complete video so that you get better
  1709. 1:13:50clarity and initially you should do it
  1710. 1:13:52like this only that first try to
  1711. 1:13:54implement using lambda and then convert
  1712. 1:13:56it into a method reference. Okay.
  1713. 1:13:59Eventually if you start doing it you
  1714. 1:14:00might be able to do it directly as well.
  1715. 1:14:03Okay. calculator C3 is equal to I have
  1716. 1:14:07to take two arguments a comma B then
  1717. 1:14:10this operator these curly braces and
  1718. 1:14:13here I'll give the implementation which
  1719. 1:14:15is return math operations dot multiply
  1720. 1:14:19and I'll pass a comma b okay using this
  1721. 1:14:22C3 I can call the calculate method let's
  1722. 1:14:25say I'm doing 10 into 3 okay now if I
  1723. 1:14:30want to convert this into a method
  1724. 1:14:32reference the syntax this first let me
  1725. 1:14:35take calculator C4 then this is the
  1726. 1:14:38class name and this is the method name
  1727. 1:14:40so I'll just write math operations colon
  1728. 1:14:43multiply okay and it ends with a
  1729. 1:14:46semicolon okay so like this this is how
  1730. 1:14:49I can implement the functional interface
  1731. 1:14:52and now using the C4 I can call the
  1732. 1:14:54calculate method 10 3 all right so let
  1733. 1:14:59me just comment this one and I'll run
  1734. 1:15:03this part.
  1735. 1:15:05Okay, I have not printed this
  1736. 1:15:10system.out.print
  1737. 1:15:12ln.
  1738. 1:15:16Similarly,
  1739. 1:15:25great. So you can see that we get the
  1740. 1:15:27result as 10 into 330 for both these
  1741. 1:15:30cases. This is how we do we did using
  1742. 1:15:32lambda and this is how we did using
  1743. 1:15:35method reference.
  1744. 1:15:37Okay. Next is instance method reference
  1745. 1:15:41of a particular object. It is used when
  1746. 1:15:44we are calling an instance method of
  1747. 1:15:45some class using its object. The syntax
  1748. 1:15:49is object name colon instance method
  1749. 1:15:52name. All right let's see. So I'll take
  1750. 1:15:55another example. What I'll do is I'll
  1751. 1:15:58create another interface named
  1752. 1:16:00validator.
  1753. 1:16:02It's also a functional interface
  1754. 1:16:05and it has this method boolean validate.
  1755. 1:16:10Okay. And here we take some number. All
  1756. 1:16:12right. So I want to implement this
  1757. 1:16:14functional interface. Okay. Inside the
  1758. 1:16:17math operations I'll add another method
  1759. 1:16:21public boolean is positive. Okay. It
  1760. 1:16:26takes a number and it checks whether the
  1761. 1:16:29number is positive or not. Okay, return
  1762. 1:16:31num is greater than zero. And you must
  1763. 1:16:34note that this method is not a static
  1764. 1:16:36method. Okay, these two are static
  1765. 1:16:38methods but this one is a simple
  1766. 1:16:40instance method. Okay. Now let's say I
  1767. 1:16:44want to give some implementation of this
  1768. 1:16:46validator. So first let me do it using
  1769. 1:16:49the lambda expression.
  1770. 1:16:52All right. Let me comment this part.
  1771. 1:16:55So what's the syntax? First I'll write
  1772. 1:16:57the interface name validator v_ub1 is
  1773. 1:17:00equal to there is a single argument
  1774. 1:17:02number like this and here I'll give the
  1775. 1:17:05implementation. Okay. So the
  1776. 1:17:08implementation is I want to call this
  1777. 1:17:11method is positive okay on the number.
  1778. 1:17:13Now since it is not a static method so
  1779. 1:17:16if I'll try to call it like this math
  1780. 1:17:18operations dot is positive.
  1781. 1:17:22Okay, I won't be able to do this. All
  1782. 1:17:24right, because this is not a static
  1783. 1:17:26method. That is why I will have to
  1784. 1:17:28create an object of math operations.
  1785. 1:17:31Okay, math operations like this.
  1786. 1:17:34Okay, now using this object, I can call
  1787. 1:17:37this method, right? So, all right, I'll
  1788. 1:17:41do it like this. Math operations dot is
  1789. 1:17:43positive and here I'll pass the number.
  1790. 1:17:46Okay, simple. And I'll just return this
  1791. 1:17:50value. All right. Then using this v1, we
  1792. 1:17:53will call the method validate.
  1793. 1:17:56Okay. And let's say I pass the number
  1794. 1:17:5810. And I pass the number let's say
  1795. 1:18:02minus 2. Okay. And we'll just print the
  1796. 1:18:05result. System.out.t print ln.
  1797. 1:18:11Similarly here as well.
  1798. 1:18:17Okay. So this is how we can implement
  1799. 1:18:19using lambda expression. Okay. If I run
  1800. 1:18:23this, great. You'll see that we get true
  1801. 1:18:25for this but false for this one because
  1802. 1:18:27it is a negative number. Okay. Now can
  1803. 1:18:30this be converted into method reference.
  1804. 1:18:33All right. So this is a single line.
  1805. 1:18:35Yes. And here a method call is
  1806. 1:18:37happening. Okay. So yes, I can convert
  1807. 1:18:39this into a method reference. But this
  1808. 1:18:42is not a static method reference. The
  1809. 1:18:44difference is that we have called this
  1810. 1:18:46method using this object. Okay. So the
  1811. 1:18:49syntax is that I'll write the interface
  1812. 1:18:51name. I'll give some object and first I
  1813. 1:18:54will have to write the object name. So
  1814. 1:18:56instead of the class name I'll have to
  1815. 1:18:58write the object name which I've taken
  1816. 1:19:00math operation. I'll take something else
  1817. 1:19:01so that it's a little bit confusing.
  1818. 1:19:04Let's say like this we call it M1. Okay.
  1819. 1:19:07So here I will have to write M1 colon
  1820. 1:19:11and then is positive. Okay, like this.
  1821. 1:19:14So this is the syntax. Using this I can
  1822. 1:19:16again call v2 do.validate I'll just copy
  1823. 1:19:21paste this.
  1824. 1:19:24Okay. Instead of v_sub1 I'll take
  1825. 1:19:25v_sub_2.
  1826. 1:19:27All right. If I run it again you'll see
  1827. 1:19:30we get the same result. True false then
  1828. 1:19:32true false. Okay. So this is how we did
  1829. 1:19:36the implementation using lambda. And
  1830. 1:19:39using this method reference we can
  1831. 1:19:41perform the same action. Okay. So the
  1832. 1:19:43syntax is object name col the method
  1833. 1:19:46name. Okay. Next we have instance method
  1834. 1:19:50reference of an arbitrary object. It is
  1835. 1:19:53used when we are calling an instance
  1836. 1:19:55method on an object that is passed as an
  1837. 1:19:58argument. The syntax is class name col
  1838. 1:20:02instance method. All right let's see. So
  1839. 1:20:05again I'll take a different example.
  1840. 1:20:08Let's say I create an interface string
  1841. 1:20:11operations
  1842. 1:20:13and mark it as a function interface.
  1843. 1:20:17Now this has a method int operate. Okay.
  1844. 1:20:21And it takes some string parameter.
  1845. 1:20:23Let's say string str. All right. So if I
  1846. 1:20:26want to implement this using lambda,
  1847. 1:20:28what I can do first? Let me just comment
  1848. 1:20:31this out. I'll write the interface name
  1849. 1:20:34string operations. give some name to the
  1850. 1:20:37object S1. Now there is a single
  1851. 1:20:39parameter. So I'll take one parameter
  1852. 1:20:41and
  1853. 1:20:43between these curly braces I will have
  1854. 1:20:45to give my implementation. Okay.
  1855. 1:20:47Implementation for this method operate.
  1856. 1:20:50All right. So let's say I want to return
  1857. 1:20:52the length of the string. Okay.
  1858. 1:20:54String.length.
  1859. 1:20:55All right. So this is an int value that
  1860. 1:20:58actually and you'll see that this
  1861. 1:21:00operate method also returns some int
  1862. 1:21:01value. Okay. So I've returned
  1863. 1:21:03string.length.
  1864. 1:21:04All right. Now using this S1 I can call
  1865. 1:21:07S1.operate and let's say I pass my name.
  1866. 1:21:12All right. And let me just print the
  1867. 1:21:14result whatever the result will be.
  1868. 1:21:19Cool. So this is how I can implement
  1869. 1:21:21using lambda. If I run this great you
  1870. 1:21:24will see that the length of the string
  1871. 1:21:26sham has been printed. Okay. Now can I
  1872. 1:21:30convert this into method reference?
  1873. 1:21:32again yes because it's a single line and
  1874. 1:21:35only a method call is taking place. Okay
  1875. 1:21:38so but in this case the thing that is
  1876. 1:21:40different is that here in this example
  1877. 1:21:43you can see that we had an object of
  1878. 1:21:45math operations and using this object we
  1879. 1:21:47were calling a method. Okay but in this
  1880. 1:21:50case we have this argument and using
  1881. 1:21:53this argument we are calling some
  1882. 1:21:54method. Okay that is the difference
  1883. 1:21:56between these two types. Okay. So syntax
  1884. 1:22:01wise there is not much difference. In
  1885. 1:22:03this case the object was used object
  1886. 1:22:06name col the method name. In this case
  1887. 1:22:09again we have class name colon the
  1888. 1:22:11instance method name. So now what will
  1889. 1:22:13be the class here. So this str is of the
  1890. 1:22:16type string. So the class will be
  1891. 1:22:18string. Okay, let me show you. String
  1892. 1:22:21operations s_2 is equal to I have to
  1893. 1:22:26write string colon length. Okay, like
  1894. 1:22:30this. So here we'll have the method name
  1895. 1:22:33and the class name will be the data type
  1896. 1:22:35of the argument that is being passed
  1897. 1:22:37there. Okay, so this is a string
  1898. 1:22:39argument. That is why we'll take string.
  1899. 1:22:42Okay, now using this s2 we can call
  1900. 1:22:44operate and again I'll pass this.
  1901. 1:22:48All right. And let me just run it. Let
  1902. 1:22:50me just print this.
  1903. 1:22:56Great.
  1904. 1:22:58Okay. So you'll see that both will
  1905. 1:22:59return the same result. Okay. Similarly,
  1906. 1:23:02I'll take one more example. Let's say
  1907. 1:23:04there is string operations.
  1908. 1:23:08Okay. And
  1909. 1:23:11instead of returning the length, I want
  1910. 1:23:13to return hash code. Okay. Okay. So hash
  1911. 1:23:16code is another method inside this class
  1912. 1:23:18string. Okay. Let me name this S3.
  1913. 1:23:22All right. So using this I can just let
  1914. 1:23:26me
  1915. 1:23:28copy paste this. Okay. S3 dot operate
  1916. 1:23:32shivam. Okay. This is the string. And
  1917. 1:23:35similarly I'll do this thing using the
  1918. 1:23:39method reference. Okay. So how can I do
  1919. 1:23:41it? Again the object is str which is of
  1920. 1:23:45the type string. So I will have to take
  1921. 1:23:47string operations s4 is equal to the
  1922. 1:23:50class name is string col hash code. So
  1923. 1:23:54this time the method name is hash code.
  1924. 1:23:56Okay like this using this s4 I can call
  1925. 1:23:59the method operate and I'll pass sham
  1926. 1:24:02over here. Okay then again let me just
  1927. 1:24:05print this.
  1928. 1:24:07I'll change this to s4.
  1929. 1:24:10All right. So if I run this now you can
  1930. 1:24:14see that some hash code has been
  1931. 1:24:15printed. Okay. This is using lambda and
  1932. 1:24:17this is using method reference.
  1933. 1:24:20All right. Now at last we have
  1934. 1:24:23constructor method reference. It is used
  1935. 1:24:26when we are calling a constructor to
  1936. 1:24:28create a new object. The syntax is class
  1937. 1:24:31name col new. All right let's see. So
  1938. 1:24:35I'll take another example. I'll just
  1939. 1:24:37simply create one more interface.
  1940. 1:24:39let's say creator
  1941. 1:24:42and mark it as a functional interface
  1942. 1:24:46and I'll create a method named create
  1943. 1:24:50okay string name all right it takes an
  1944. 1:24:53argument string name and the return type
  1945. 1:24:55I want to pass using the generics okay
  1946. 1:24:58so we can have a generic return type
  1947. 1:25:00there is no fixed return type okay so at
  1948. 1:25:02the time of implementing this interface
  1949. 1:25:05I'll decide what return type I want to
  1950. 1:25:07give okay Cool. Now I'll take two
  1951. 1:25:11classes. One is a class named user. It
  1952. 1:25:14has this field string name.
  1953. 1:25:19And I'll simply just generate a
  1954. 1:25:22constructor.
  1955. 1:25:24Okay. Similarly, I'll create one more
  1956. 1:25:26class named product
  1957. 1:25:29and it also has a field string. Let's
  1958. 1:25:32say product name. Okay.
  1959. 1:25:36Don't worry, it will all be clear once
  1960. 1:25:37the example is completed. Okay. And I'll
  1961. 1:25:40just generate a constructor for this as
  1962. 1:25:42well. Okay. So, we have two different
  1963. 1:25:44classes product and user. And we have a
  1964. 1:25:47functional interface named creator.
  1965. 1:25:49Okay. That has this abstract method
  1966. 1:25:51create. It takes a string parameter and
  1967. 1:25:53we can have any return time. Okay. So,
  1968. 1:25:56let's say I want to implement this
  1969. 1:25:58first. Let me just comment this part.
  1970. 1:26:02All right. So again the syntax is first
  1971. 1:26:05let me do it using lambda. So I'll take
  1972. 1:26:07a creator. I'll first write the
  1973. 1:26:09interface name and here in the generics
  1974. 1:26:11I will have to pass the return type. So
  1975. 1:26:14let's say I want to create a user. Okay.
  1976. 1:26:16So the return type I take as user. Okay.
  1977. 1:26:18Let me call it C1 is equal to now this
  1978. 1:26:22method has one parameter. So I'll take
  1979. 1:26:25this parameter
  1980. 1:26:27like this. And let's say in this
  1981. 1:26:29implementation in this implementation of
  1982. 1:26:32this method create I want to return a
  1983. 1:26:34user. Okay, I want to create a user. So
  1984. 1:26:37what I'll do I'll just create return new
  1985. 1:26:40user and I will pass the name. Okay, so
  1986. 1:26:45actually I'm calling this constructor.
  1987. 1:26:47Okay, this constructor that I created.
  1988. 1:26:50All right, now using this C1 I will just
  1989. 1:26:53call the create method and let's say I
  1990. 1:26:55create a user. Okay, like this. So can I
  1991. 1:27:00convert this into a method reference?
  1992. 1:27:03Yes, this is also a type of method
  1993. 1:27:05reference because here we are actually
  1994. 1:27:07calling a constructor. Okay, and
  1995. 1:27:09constructor is also a special type of
  1996. 1:27:10method. Okay, so it's a single line and
  1997. 1:27:13we are calling the constructor. So this
  1998. 1:27:15can be converted into a method
  1999. 1:27:16reference. Okay. So the syntax is the
  2000. 1:27:20class name colon new. All right. So I'll
  2001. 1:27:23take the class name creator.
  2002. 1:27:26Okay, first I have to give take the
  2003. 1:27:29interface name and then I'll take the
  2004. 1:27:31class name. Class name is user. Okay, in
  2005. 1:27:33this case user colon new. All right. So
  2006. 1:27:38this represents this. Okay, I know it is
  2007. 1:27:43new to you and these are very different
  2008. 1:27:45examples but these are four types of
  2009. 1:27:47syntaxes that you must learn and if you
  2010. 1:27:50are going to practice using some
  2011. 1:27:52examples, you'll definitely get a hold
  2012. 1:27:54of these. Okay. So using this C2 I will
  2013. 1:27:57call the create and I'll create the user
  2014. 1:28:00shaman.
  2015. 1:28:03Okay like this. So actually this method
  2016. 1:28:07returns T. So T in this case is user. So
  2017. 1:28:10actually this will be user. Okay. I'll
  2018. 1:28:13extract the variable user u1
  2019. 1:28:17and this one user U2.
  2020. 1:28:21Okay. So we can just simply print the
  2021. 1:28:24name again just to verify. Okay. So I
  2022. 1:28:27will print U1
  2023. 1:28:30dot name
  2024. 1:28:32and similarly
  2025. 1:28:35I can print U2.name. All right. Let me
  2026. 1:28:38run this. Great. So you see that we pass
  2027. 1:28:42sham. We pass sham. A new user was
  2028. 1:28:45created and that user was returned here.
  2029. 1:28:48this user and here we have printed the
  2030. 1:28:50name which is coming out to be shown and
  2031. 1:28:52the same thing we have done using method
  2032. 1:28:53reference we wrote the class name col
  2033. 1:28:56new okay so this represent constructor
  2034. 1:28:58reference and then using this object C2
  2035. 1:29:02we did C2Create and a user U2 was
  2036. 1:29:04created okay and then I have printed the
  2037. 1:29:07name all right similarly I can I'll take
  2038. 1:29:11another example using the product as
  2039. 1:29:13well okay so we have taken create a
  2040. 1:29:15product let's say C3 tree is equal to
  2041. 1:29:19name
  2042. 1:29:22and this time we are returning new
  2043. 1:29:24product. Okay. And I'll pass the name.
  2044. 1:29:27All right. So this time I'm calling this
  2045. 1:29:29constructor.
  2046. 1:29:31Okay.
  2047. 1:29:33And using the C3 I will call the create
  2048. 1:29:35method. Let's say the product is Apple.
  2049. 1:29:40Okay. Apple watch.
  2050. 1:29:43And uh similarly how can I convert it
  2051. 1:29:46into a method reference? I'll take the
  2052. 1:29:48interface name product C4 is equal to so
  2053. 1:29:52the class name here is product okay so
  2054. 1:29:55I'll take product colon new okay like
  2055. 1:29:59this then using this C4 I'll again call
  2056. 1:30:02the create method let's say Apple watch
  2057. 1:30:07okay so this will return a product okay
  2058. 1:30:12product let's call it P1
  2059. 1:30:15and this will also return a product
  2060. 1:30:20okay let me write it like this only
  2061. 1:30:22product
  2062. 1:30:24pay2 is equal to C4 create because
  2063. 1:30:27create returns this type and T is
  2064. 1:30:31product in this case okay so using this
  2065. 1:30:34again I can print let me just copy paste
  2066. 1:30:39P1 dot product name okay we have the
  2067. 1:30:42field product name similarly
  2068. 1:30:463
  2069. 1:30:472 dot
  2070. 1:30:50product name. Okay, if I run it, you'll
  2071. 1:30:54see that Apple watch has been printed
  2072. 1:30:55twice. Okay, once using this lambda
  2073. 1:30:58expression and then using method
  2074. 1:31:00reference. Okay, so these are all the
  2075. 1:31:03different method references that were
  2076. 1:31:05covered in Java 8. I know that the
  2077. 1:31:07syntax is a bit tricky in the beginning,
  2078. 1:31:09but trust me, if you practice it a lot,
  2079. 1:31:12you'll definitely get a hold of it.
  2080. 1:31:14Okay. Hello and welcome to my channel
  2081. 1:31:17the curious coder. This video is part of
  2082. 1:31:20my ongoing Java 8 series. Today we are
  2083. 1:31:23going to explore a really powerful
  2084. 1:31:25feature which was introduced in Java 8,
  2085. 1:31:28the optional class. It's a feature whose
  2086. 1:31:31addition was a complete gamecher in
  2087. 1:31:34handling null pointer exceptions. Now if
  2088. 1:31:37you're a working professional, you have
  2089. 1:31:39probably seen code like these a lot. And
  2090. 1:31:41if you're a student, you'll definitely
  2091. 1:31:43come across optional in future. At
  2092. 1:31:46first, it can seem confusing, but don't
  2093. 1:31:48worry. In this video, I'll explain
  2094. 1:31:51optional line by line, word by word, so
  2095. 1:31:54you can understand it in depth. So, what
  2096. 1:31:56exactly is optional? Optional is a
  2097. 1:31:59special class in Java that may or may
  2098. 1:32:02not contain a value. Its main purpose is
  2099. 1:32:05to help us reduce the chances of running
  2100. 1:32:07into null pointer exceptions, thereby
  2101. 1:32:10making our code safer and more readable.
  2102. 1:32:13So how does optional help avoid null
  2103. 1:32:15pointers? Since an optional may or may
  2104. 1:32:18not contain a value, it forces us to
  2105. 1:32:20think about the case when the value is
  2106. 1:32:22missing. As a result, we naturally end
  2107. 1:32:25up handling those cases instead of
  2108. 1:32:27running into null pointers. All right.
  2109. 1:32:30So let's see it in practical. All right.
  2110. 1:32:33So let's consider this example. Here we
  2111. 1:32:36are fetching a user by calling this
  2112. 1:32:38method get user by ID. So we have
  2113. 1:32:41created a class named user. It has these
  2114. 1:32:44two fields ID and name. And we are
  2115. 1:32:46fetching this by calling this method.
  2116. 1:32:49Okay. Now this method belongs to the
  2117. 1:32:51class user service. Here we have taken a
  2118. 1:32:54list of users user 1 2 and three. And we
  2119. 1:32:58take ID as a parameter. So we loop on
  2120. 1:33:01this list, we loop on this list and if
  2121. 1:33:04we find a match for this ID, okay, if we
  2122. 1:33:07find any match, then we return the user.
  2123. 1:33:10Otherwise, we simply return null. Okay,
  2124. 1:33:12so null represents that user not found.
  2125. 1:33:15Okay, user was not there in this list.
  2126. 1:33:18All right, so simple. So if I run this
  2127. 1:33:21code by taking the ID2,
  2128. 1:33:24then you'll see that it runs perfectly.
  2129. 1:33:26Okay, we got a user and here we are
  2130. 1:33:28printing the name and ID of the user.
  2131. 1:33:31Okay, but let's say I change the ID to
  2132. 1:33:34four. Okay, now if I run my application,
  2133. 1:33:37great. We will encounter a null pointer
  2134. 1:33:39exception. Okay, why? Because there was
  2135. 1:33:43no user present against the ID 4 and
  2136. 1:33:46thereby null was returned and here we
  2137. 1:33:49called get name and get ID on a null
  2138. 1:33:51reference. Okay, that's why we got a
  2139. 1:33:53null pointer exception. Okay. So how to
  2140. 1:33:56handle it? One way is that we can add a
  2141. 1:33:58null pointer check here like this. If
  2142. 1:34:01user is not equal to null
  2143. 1:34:05then we should print this statement.
  2144. 1:34:07Okay. Now if I if I'll run it then
  2145. 1:34:11nothing will be printed but there will
  2146. 1:34:13be no null pointer exception. All right.
  2147. 1:34:16But see the problem here is that in an
  2148. 1:34:19actual codebase many developers might be
  2149. 1:34:22working on the same project. Okay. Okay,
  2150. 1:34:24so it's possible that one developer
  2151. 1:34:25wrote this user service and this method
  2152. 1:34:27was written and then another developer
  2153. 1:34:30came and he just started to use this
  2154. 1:34:32method. Okay, and somehow he forgot to
  2155. 1:34:34add the null pointer check. Okay, that
  2156. 1:34:36is a possibility. All right, and even if
  2157. 1:34:39he doesn't forget it here, but let's say
  2158. 1:34:41then there are many lines of code and
  2159. 1:34:43after that someone uses this user again.
  2160. 1:34:46Okay, we have a null check here but
  2161. 1:34:48after some lines then we forgot to add a
  2162. 1:34:51null check there. Okay, that is quite
  2163. 1:34:53possible. So the thing is no one is you
  2164. 1:34:56know reminding me that we have to add a
  2165. 1:34:58null pointer check. We need a reminder
  2166. 1:35:01that tells us that it is necessary for
  2167. 1:35:03us to add a null pointer check to avoid
  2168. 1:35:05any null pointer exception. Okay, so
  2169. 1:35:08that is what optional does. It helps us
  2170. 1:35:10remind that it is necessary for us to
  2171. 1:35:13add a null pointer check. Okay, so I'll
  2172. 1:35:15show you how. What we do is I'll just
  2173. 1:35:18replace it with optional
  2174. 1:35:22like this. Okay. So don't worry about
  2175. 1:35:24the syntax now. I'll explain everything.
  2176. 1:35:27So we do optional dot of
  2177. 1:35:30user and here instead of returning null
  2178. 1:35:33a little optional dom. Okay. So I'm
  2179. 1:35:37making few changes in my code. Okay.
  2180. 1:35:40I'll remove this null check as of now.
  2181. 1:35:43And this get user by ID returns an
  2182. 1:35:46optional user. Okay,
  2183. 1:35:50like this. And I'll call it
  2184. 1:35:52optional user.
  2185. 1:35:56Okay, import. And let me just remove
  2186. 1:35:58this for now. All right. So what we did
  2187. 1:36:01was instead of returning a user
  2188. 1:36:04directly, we wrapped it inside an
  2189. 1:36:06optional and then returned it. Okay. So
  2190. 1:36:09now what exactly is optional? Let's see.
  2191. 1:36:12You'll see optional is a class. Okay, it
  2192. 1:36:15is a class and it contains only one
  2193. 1:36:17field this value. Okay, this is the only
  2194. 1:36:20field inside this class optional. And
  2195. 1:36:23whatever you pass in this generics is
  2196. 1:36:26the return type of this value. Okay. So
  2197. 1:36:28since we have passed this user that is
  2198. 1:36:32why we are able to you know in this
  2199. 1:36:34return type you'll see that I've taken
  2200. 1:36:35the return type as optional of user that
  2201. 1:36:38is why inside this optional the value
  2202. 1:36:40will be of the type user. Okay we can
  2203. 1:36:43and we can wrap a user value inside this
  2204. 1:36:46optional. Okay this is what we did. So
  2205. 1:36:49now what can I do when I see that an
  2206. 1:36:52optional value has been returned from
  2207. 1:36:53here. Now it is my duty that I have to
  2208. 1:36:57put a null pointer check. Okay, why?
  2209. 1:36:59Because optional may or may not contain
  2210. 1:37:01a value. Okay, so I'm forced to think
  2211. 1:37:04that okay, it might be possible that it
  2212. 1:37:06does not contain the value. That is why
  2213. 1:37:08I will have to put a null pointer check.
  2214. 1:37:11Okay, and I don't even have to put it
  2215. 1:37:12directly. Optional has given me many
  2216. 1:37:14methods that let me handle it properly.
  2217. 1:37:17Okay, so we have one method optional
  2218. 1:37:19user dot is present.
  2219. 1:37:22Okay, so this automatically checks
  2220. 1:37:24whether the user was present or not.
  2221. 1:37:27Okay, so it's kind of a null pointer
  2222. 1:37:28check. Okay, so now what I'll do, I'll
  2223. 1:37:32print the name and the ID. Okay, so
  2224. 1:37:37we'll take optional user dot get name.
  2225. 1:37:41But wait, this is not the type of user.
  2226. 1:37:44It is type of optional user. And this
  2227. 1:37:46get name method is inside optional is
  2228. 1:37:49inside user. Okay, so how can I print
  2229. 1:37:51it? So somehow we need to fetch the
  2230. 1:37:54actual user from this optional user.
  2231. 1:37:56Okay, so for that we have a method named
  2232. 1:37:59get.
  2233. 1:38:00Okay, this get will bring me the actual
  2234. 1:38:03user. All right, so I can write it like
  2235. 1:38:06this. User user is equal to optional
  2236. 1:38:09user.get. And now using this user, I
  2237. 1:38:11will print the name as well as
  2238. 1:38:15I will print the ID. Okay, user dot get
  2239. 1:38:19ID.
  2240. 1:38:21All right, let me just add name
  2241. 1:38:27and
  2242. 1:38:29ID.
  2243. 1:38:32Okay,
  2244. 1:38:34so what we did was I'll just repeat we
  2245. 1:38:38changed it to optional of user and then
  2246. 1:38:40we added this if condition that if the
  2247. 1:38:42user is present then only fetch the
  2248. 1:38:44user. Now this user cannot be null
  2249. 1:38:47because we have already added this
  2250. 1:38:48check. can be uh fested using this
  2251. 1:38:51method get and now using this user we
  2252. 1:38:53are printing the name and the id. Okay.
  2253. 1:38:56So if I run it for ID2 you will see that
  2254. 1:39:00the name and ID has been printed. But if
  2255. 1:39:03I run it for ID4
  2256. 1:39:06now nothing has been printed but there
  2257. 1:39:08is no null pointer exception. Okay there
  2258. 1:39:10is no exception. All right. So now one
  2259. 1:39:13by one I'll tell you what these methods
  2260. 1:39:15do. Okay. So first I'll tell you about
  2261. 1:39:18this optional do of okay because this is
  2262. 1:39:21where we actually created an optional
  2263. 1:39:23object okay this is where we wrapped our
  2264. 1:39:25user inside an optional okay so what it
  2265. 1:39:28does is if I go inside this off you will
  2266. 1:39:32see that it returns a new optional okay
  2267. 1:39:34so this is where the object has been
  2268. 1:39:36created okay so if I go here you'll see
  2269. 1:39:39that this is a constructor okay so this
  2270. 1:39:42is actually populating the value this
  2271. 1:39:45value inside the value which is this
  2272. 1:39:47field. Okay, so this is the constructor.
  2273. 1:39:50So by calling the optional off I've
  2274. 1:39:52actually populated the user as a
  2275. 1:39:54optional user. Okay, so this value field
  2276. 1:39:57has been populated. This value field has
  2277. 1:40:00been populated. Okay, now optional empty
  2278. 1:40:03in case of this what happens is that it
  2279. 1:40:06actually returns empty and empty is also
  2280. 1:40:08an optional object but it does not
  2281. 1:40:10contain any value. Okay, it's it's an
  2282. 1:40:12empty field. So in this case optional
  2283. 1:40:15does not contain anything. Okay. Now
  2284. 1:40:18this method is present. If I go inside
  2285. 1:40:20this it's a very simple method. It says
  2286. 1:40:23if value is not equal to null then
  2287. 1:40:25return true otherwise return false.
  2288. 1:40:26Okay. So it's simply checking whether
  2289. 1:40:28this value field has been populated or
  2290. 1:40:30not. Okay. So in case of empty it is not
  2291. 1:40:34populated. But in case when we actually
  2292. 1:40:37populated it by calling optional.off
  2293. 1:40:40then we get something. Okay. Then it
  2294. 1:40:42returns true.
  2295. 1:40:43All right. Now this dot get method it
  2296. 1:40:46does nothing. If the value is null it
  2297. 1:40:48will return no value present. But if it
  2298. 1:40:51is not null then it will return a value.
  2299. 1:40:53Okay. So somehow if you call this get
  2300. 1:40:55method without this is present check
  2301. 1:40:57then it will return this error. Okay.
  2302. 1:41:00But here since we added the check that's
  2303. 1:41:02why it returned the actual value. Okay.
  2304. 1:41:05Which in this case is user because this
  2305. 1:41:07t is of type user. All right. So these
  2306. 1:41:10were all the methods that were here in
  2307. 1:41:12the picture. Now so far we have seen
  2308. 1:41:15optionals methods like is present of
  2309. 1:41:18empty and get. Now these are few more
  2310. 1:41:21methods that optional class contains.
  2311. 1:41:24One by one we'll understand each of them
  2312. 1:41:26with examples. If present is an optional
  2313. 1:41:29classes method which executes a given
  2314. 1:41:32action only if the value is present. All
  2315. 1:41:35right let me show you. So now instead of
  2316. 1:41:38writing it like this, what I'll do is
  2317. 1:41:41I'll simply take optional user and I'll
  2318. 1:41:43call this method if present. Okay, here
  2319. 1:41:46I can take a user and then we can just
  2320. 1:41:50add this print statement inside this
  2321. 1:41:53lambda expression. Okay, like this. All
  2322. 1:41:56right, so this code has been converted
  2323. 1:41:58into a single line using this if present
  2324. 1:42:01method. Okay, so I'll just simply
  2325. 1:42:02comment this out. And let's say if I run
  2326. 1:42:05for ID 2,
  2327. 1:42:08great. If I run for ID2, then the user
  2328. 1:42:11is present, which means this statement
  2329. 1:42:12will be executed. But if I run for ID4,
  2330. 1:42:17then nothing will be executed. Okay, so
  2331. 1:42:20this is how if present works. It only
  2332. 1:42:22executes this statement when the user is
  2333. 1:42:24present. Otherwise, it does not. Okay.
  2334. 1:42:28Now what we are going to do is I'll go
  2335. 1:42:30inside this method and we'll try to
  2336. 1:42:32understand what exactly is happening
  2337. 1:42:34behind the scene. Okay. So I'll you know
  2338. 1:42:37I'll teach you line by line what this
  2339. 1:42:39code represents and we'll look at what
  2340. 1:42:41exactly is happening behind the scene.
  2341. 1:42:43Okay. So this if present method takes
  2342. 1:42:46something called consumer. Okay. It has
  2343. 1:42:48this argument consumer. So what is a
  2344. 1:42:50consumer? Consumer is a functional
  2345. 1:42:52interface. Okay. And it has this method
  2346. 1:42:55named accept. Okay, it has this abstract
  2347. 1:42:57method named accept that takes one
  2348. 1:42:59argument but it returns nothing. All
  2349. 1:43:02right, so what are functional
  2350. 1:43:05interfaces? These are interfaces that
  2351. 1:43:07have a single abstract method and we can
  2352. 1:43:10implement them by giving a lambda
  2353. 1:43:11expression. Okay, and that is exactly
  2354. 1:43:13what we have done over here. Okay, so
  2355. 1:43:16I'll just break it down in a simpler
  2356. 1:43:17way. I'll first show you how to
  2357. 1:43:19implement the interface. Okay, so we
  2358. 1:43:22have a consumer and we want the argument
  2359. 1:43:24to be user. So I'll just simply show it
  2360. 1:43:27show it to you how we can actually
  2361. 1:43:28implement it using lambda expression.
  2362. 1:43:31Okay. So we'll take a user. We'll take
  2363. 1:43:34one parameter.
  2364. 1:43:36All right. Why? Because this except has
  2365. 1:43:39a single parameter. Okay. So I'll take a
  2366. 1:43:41parameter like this. Then I'll add the
  2367. 1:43:44arrow and here we can provide the
  2368. 1:43:46implementation. Okay. So this
  2369. 1:43:48implementation is of this accept method.
  2370. 1:43:51Okay. So here I can actually add this
  2371. 1:43:53logic. So my logic is that I want to
  2372. 1:43:55print the user's name and ID. Okay, so
  2373. 1:43:58here I can print the user's name and ID.
  2374. 1:44:02Okay, like this. And let me just comment
  2375. 1:44:04this for now. Okay, so this is how I can
  2376. 1:44:06implement a consumer interface. Okay, so
  2377. 1:44:09now if I'll run this, will anything
  2378. 1:44:11happen?
  2379. 1:44:12No, actually nothing will happen. Why?
  2380. 1:44:15Because as of now we have not called
  2381. 1:44:17this accept method. Okay, we have to
  2382. 1:44:19call this method. So if I'll do C1 do
  2383. 1:44:22accept
  2384. 1:44:24and here I will pass the user. Okay,
  2385. 1:44:28I'll pass my user. So where will I get
  2386. 1:44:30the user? Optional user dot get. Okay,
  2387. 1:44:33let me let the ID be two. Okay, now if I
  2388. 1:44:36run it, great. You will see that when we
  2389. 1:44:40call this method accept at that point
  2390. 1:44:43this statement will get executed. Okay,
  2391. 1:44:45so this is how functional interfaces
  2392. 1:44:46work. Here we provide the implementation
  2393. 1:44:48and then when the method is actually
  2394. 1:44:50called this implementation is executed.
  2395. 1:44:53Okay. So that is kind of what is
  2396. 1:44:55happening here in if present. Okay. I'll
  2397. 1:44:57show you. So this is the implementation.
  2398. 1:45:00I can shorten it a bit. I can remove
  2399. 1:45:01this. I can remove this and I can since
  2400. 1:45:05it's a single parameter so I can remove
  2401. 1:45:07this as well. Okay. So this
  2402. 1:45:09implementation looks like this. And it
  2403. 1:45:11is exactly what we have added here.
  2404. 1:45:13Okay. This is exactly what we have added
  2405. 1:45:15here. Now the only thing is whenever
  2406. 1:45:17this accept method will be called then
  2407. 1:45:20this statement will be executed. Okay.
  2408. 1:45:22So I'll just remove this. I just wanted
  2409. 1:45:24to show you how consumer works. Okay. So
  2410. 1:45:26here we have added the implementation.
  2411. 1:45:28Now if the accept method will be called
  2412. 1:45:31okay then it means that it will be
  2413. 1:45:33executed otherwise it will not be
  2414. 1:45:35executed. Okay simple. So what this if
  2415. 1:45:38present is now doing here we have added
  2416. 1:45:40a consumer implementation and it has
  2417. 1:45:42added a check here that if the value is
  2418. 1:45:44not null then only call this accept
  2419. 1:45:47method. Okay you understand? So if the
  2420. 1:45:50value is not null then only this accept
  2421. 1:45:52method of this consumer interface has
  2422. 1:45:54been called and that means that this
  2423. 1:45:57statement will be only executed when the
  2424. 1:46:00value is not null. Okay. So when the
  2425. 1:46:02value was null it was not executed and
  2426. 1:46:04uh when the value is not null then this
  2427. 1:46:07was printed. Okay. So this is how if
  2428. 1:46:09present works. Okay. So if you look at
  2429. 1:46:11it inside it's actually quite simple.
  2430. 1:46:13You just need to understand how this
  2431. 1:46:15functional interfaces work and what
  2432. 1:46:17exactly is happening behind the scene.
  2433. 1:46:20Okay. Now or else is an option classes
  2434. 1:46:23method that returns the value if it is
  2435. 1:46:26present otherwise returns a default
  2436. 1:46:28value. Okay. Let me show you. So what
  2437. 1:46:31I'm going to do is I'll take the
  2438. 1:46:33optional user and let's say I call this
  2439. 1:46:36method or else okay and here I provide a
  2440. 1:46:40new user that has some ID let's say
  2441. 1:46:43minus one and I name it a default user
  2442. 1:46:47okay something like this all right so
  2443. 1:46:51this will actually return me a user
  2444. 1:46:54okay simple let me just comment this all
  2445. 1:46:58right so what exactly is happening
  2446. 1:47:00We had this optional user and we wanted
  2447. 1:47:03an actual user out of it. Okay. But the
  2448. 1:47:06only problem is that there is a
  2449. 1:47:08possibility that this optional does not
  2450. 1:47:10contain anything. Okay. So what I've
  2451. 1:47:12done here is that if it does not contain
  2452. 1:47:14anything then in that case I have
  2453. 1:47:17defined a default user for myself. Okay.
  2454. 1:47:20So this or else works when it does not
  2455. 1:47:23have anything. Okay. So I'll just simply
  2456. 1:47:26print this and we'll understand.
  2457. 1:47:32Okay, we have fetched a user using this
  2458. 1:47:34user. I'm printing the name and the ID.
  2459. 1:47:37Okay, so if the ID was two,
  2460. 1:47:41great. Then the actual user again this
  2461. 1:47:43ID was printed. But let's say if the ID
  2462. 1:47:46is four
  2463. 1:47:48then in that case
  2464. 1:47:50this will this or else will be triggered
  2465. 1:47:52and it will return the default user.
  2466. 1:47:54Okay. So this default user will be
  2467. 1:47:56returned. Okay. Simple. Now if I look
  2468. 1:47:59inside this method you will see that
  2469. 1:48:01it's a very simple implementation. It
  2470. 1:48:03simply says that if the value is not
  2471. 1:48:05null then return the exact value. If it
  2472. 1:48:08is return if it is null then return
  2473. 1:48:10whatever we have passed in the argument.
  2474. 1:48:12Okay. So we pass this default user in
  2475. 1:48:14the argument and that is why it was
  2476. 1:48:16returned. Okay. Now or else get is an
  2477. 1:48:20optional classes method that returns the
  2478. 1:48:22value if it is present. Otherwise it
  2479. 1:48:25calls a supplier to generate the default
  2480. 1:48:28value. All right let me show you. So now
  2481. 1:48:31what we can do is we can actually
  2482. 1:48:33replicate this entire thing using a
  2483. 1:48:36different method. Okay let me show you
  2484. 1:48:38how. Instead of or else I will simply
  2485. 1:48:41write or else get. Okay. And here
  2486. 1:48:45instead of passing the user directly I
  2487. 1:48:47will just simply change it to a lambda
  2488. 1:48:49expression. Okay. Like this. That's it.
  2489. 1:48:53All right. So if I'll just run it for
  2490. 1:48:56two.
  2491. 1:48:58Great. You will see that it returns the
  2492. 1:49:00user AIT. But if I run it for four
  2493. 1:49:06then it returns the default user. Okay.
  2494. 1:49:08So this is what is happening here. Uh it
  2495. 1:49:11is exactly it works the same way as or
  2496. 1:49:13else works. The only difference is that
  2497. 1:49:15here it takes a supplier implementation.
  2498. 1:49:18Okay. And here we can pass on the user
  2499. 1:49:20directly. All right. That's the
  2500. 1:49:21difference. So what is a supplier?
  2501. 1:49:23Supplier is a functional interface which
  2502. 1:49:26has this abstract method named get.
  2503. 1:49:28Okay. And it it has some return type.
  2504. 1:49:30All right. So that is why we have to
  2505. 1:49:32pass and pass a lambda implementation
  2506. 1:49:35here. Okay. How can I implement a
  2507. 1:49:37supplier? Again, it's simple. I want to
  2508. 1:49:40supply a user. So let's say I take a
  2509. 1:49:45parenthesis. Okay, since there is no
  2510. 1:49:47argument in this get, so I'll simply
  2511. 1:49:49take parenthesis without any argument
  2512. 1:49:51and we can just return a user. Okay. So
  2513. 1:49:55like this.
  2514. 1:50:01All right. And that is exactly what we
  2515. 1:50:03have done over here. Okay. So this is an
  2516. 1:50:05implementation of this supplier. All
  2517. 1:50:08right. Now when will this be executed?
  2518. 1:50:10When we'll actually call S1.get.
  2519. 1:50:13Okay. Then only this lambda gets
  2520. 1:50:16executed. Okay. And that is what is
  2521. 1:50:19happening here in this or else get here.
  2522. 1:50:22What they are doing is if the value is
  2523. 1:50:24not equal to null then simply return the
  2524. 1:50:26value. But if it is null then call the
  2525. 1:50:29get method on this implementation. Okay.
  2526. 1:50:32So this means that when the value is not
  2527. 1:50:34null then the exact user will be
  2528. 1:50:36returned. But if it is null then this
  2529. 1:50:38lambda implementation that we have
  2530. 1:50:40provided here it will trigger the get
  2531. 1:50:42method which means that this statement
  2532. 1:50:45will be executed and a default user will
  2533. 1:50:48be returned. All right. This is how this
  2534. 1:50:51or else get works.
  2535. 1:50:53Now or else throw is an optional classes
  2536. 1:50:55method which returns the value if it is
  2537. 1:50:58present otherwise it simply throws an
  2538. 1:51:00exception. All right, let me show you.
  2539. 1:51:03So what I'll do is I'll simply take the
  2540. 1:51:05optional user and I'll call this method
  2541. 1:51:09or else throw. Okay, and here we pass on
  2542. 1:51:12the implementation where we are just
  2543. 1:51:15returning some exception. Let's say I
  2544. 1:51:17want to return a runtime exception
  2545. 1:51:20that says user not found. Okay,
  2546. 1:51:23something like this. All right, so this
  2547. 1:51:26time I'm saying that if I find the user
  2548. 1:51:28then it should just simply return that
  2549. 1:51:30user. Otherwise, we should throw some
  2550. 1:51:32exception. Okay. So, we have a method
  2551. 1:51:34for that as well. All right. So, let me
  2552. 1:51:38just run it for two.
  2553. 1:51:42Okay. All right. I have not added the
  2554. 1:51:44print statement. Just a second.
  2555. 1:51:51Yeah. So, if I run it for two,
  2556. 1:51:55then the value gets retrieved. But if I
  2557. 1:51:59run it for four in that case, we get the
  2558. 1:52:03exception. Okay, that user not found. So
  2559. 1:52:06it provides you a provision that if if
  2560. 1:52:08you want to throw some exception that
  2561. 1:52:11also you can do. Okay. Now inside this
  2562. 1:52:14method also it it simply takes a
  2563. 1:52:16supplier and if the value is not null,
  2564. 1:52:18it returns the value. If the value is
  2565. 1:52:20null, then it calls the get method on
  2566. 1:52:23this supplier. Okay. And if you'll look
  2567. 1:52:25at it carefully, this is a supplier
  2568. 1:52:27implementation. Okay. The same as we
  2569. 1:52:30discussed over here in or else get.
  2570. 1:52:33All right. Now, offnable is an optional
  2571. 1:52:36classes method which is used to create
  2572. 1:52:38an optional that holds the value if it
  2573. 1:52:41is non-null. Otherwise, it returns an
  2574. 1:52:44empty optional. All right. Let's see.
  2575. 1:52:46So, we have already seen that we can use
  2576. 1:52:49optional dot off to create an optional.
  2577. 1:52:53Okay. If I pass a user inside this
  2578. 1:52:56optional doof, then an optional of this
  2579. 1:52:58user will be created. Okay. Similarly,
  2580. 1:53:01we can also use optional dot of null
  2581. 1:53:03level. Okay. Like this. Both perform the
  2582. 1:53:06same action. What they do is they
  2583. 1:53:09actually create an object of optional.
  2584. 1:53:11Okay. Now the difference is that in case
  2585. 1:53:14of this off method, we actually cannot
  2586. 1:53:17take a null value. Okay. So let's say
  2587. 1:53:20let me just comment this out for now.
  2588. 1:53:23Let's say I have a user
  2589. 1:53:27user which is null. Okay, by chance it's
  2590. 1:53:31null and if I pass pass it inside this
  2591. 1:53:34method then it will throw me some
  2592. 1:53:36exception. But in case of offnullable
  2593. 1:53:39there won't be any problem. Okay, that
  2594. 1:53:41is the only difference. So I'll show
  2595. 1:53:42you. Let's say I just run it.
  2596. 1:53:46Great. So you can see that we get a null
  2597. 1:53:47pointer exception. Why? Because if I go
  2598. 1:53:50inside this you will see that here it
  2599. 1:53:52calls a new optional and in this private
  2600. 1:53:55constructor we have this condition that
  2601. 1:53:58says objects do require non-null. Okay.
  2602. 1:54:01So it says that a non-null object is
  2603. 1:54:03required. So if the object will be null
  2604. 1:54:05then it will throw a null pointer
  2605. 1:54:06exception. Okay. And that is what
  2606. 1:54:08happened when we call when we use this
  2607. 1:54:11user along with optional.off. Okay. But
  2608. 1:54:15in case of a of offnull labelable if I
  2609. 1:54:17just run it there will be no issue.
  2610. 1:54:20Okay. Because offnully has that
  2611. 1:54:23handling. So it simply says that if the
  2612. 1:54:25value is is null then simply return
  2613. 1:54:28optional empty. Okay. So it is returning
  2614. 1:54:30optional empty. All right. So that is
  2615. 1:54:34the only difference. Okay. Hello and
  2616. 1:54:37welcome to my channel the curious coder.
  2617. 1:54:40Today we'll be learning about one of the
  2618. 1:54:42most powerful and game-changing features
  2619. 1:54:44introduced in Java 8 stream API. Trust
  2620. 1:54:48me after this video your entire approach
  2621. 1:54:51to coding will change. So what exactly
  2622. 1:54:54is a stream? A stream is a sequence of
  2623. 1:54:57elements that supports various
  2624. 1:54:59operations to process data. Formally
  2625. 1:55:02speaking, stream is an interface
  2626. 1:55:04introduced in Java 8 that provides
  2627. 1:55:07methods to perform data processing in a
  2628. 1:55:09functional style programming. Now, don't
  2629. 1:55:12worry if that sounds a bit heavy. So
  2630. 1:55:14far, what do we know? Stream is an
  2631. 1:55:17interface. It has a lot of methods. Its
  2632. 1:55:20purpose is to process data and it does
  2633. 1:55:23so in a functional programming style.
  2634. 1:55:26All right. Now, what does all of this
  2635. 1:55:28actually mean? Let's understand it
  2636. 1:55:30practically. So as I told you stream is
  2637. 1:55:33actually an interface and as you can see
  2638. 1:55:36it contains a lot of methods. Okay. We
  2639. 1:55:38have this filter map map to int.
  2640. 1:55:41Similarly there are a lot of other
  2641. 1:55:43methods inside this interface. Okay. So
  2642. 1:55:46if we look at this method filter you'll
  2643. 1:55:49see that it takes a parameter of this
  2644. 1:55:52type predicate. All right. So if I go
  2645. 1:55:54inside this you'll see that predicate is
  2646. 1:55:57actually a functional interface. Okay.
  2647. 1:56:00Now what are functional interfaces? Any
  2648. 1:56:02interface that contains a single
  2649. 1:56:04abstract method is called a functional
  2650. 1:56:06interface. Okay. So this interface
  2651. 1:56:09predicate contains only one abstract
  2652. 1:56:11method which is this one boolean test.
  2653. 1:56:14That is why it can be treated as a
  2654. 1:56:16functional interface. All right. Now
  2655. 1:56:18when I say that stream supports
  2656. 1:56:20functional shell programming, this is
  2657. 1:56:22what I actually mean that the methods
  2658. 1:56:24inside stream they work with these
  2659. 1:56:27functional interfaces. Okay. So you can
  2660. 1:56:29see that filter works with predicate.
  2661. 1:56:31Similarly, map works with this interface
  2662. 1:56:33function. Okay, which is another
  2663. 1:56:35functional interface. All right. So this
  2664. 1:56:37is how these this is how stream supports
  2665. 1:56:39functional style programming that all
  2666. 1:56:41that the methods inside this interface
  2667. 1:56:44actually work with functional
  2668. 1:56:45interfaces.
  2669. 1:56:47Okay. Now what's the benefit of having
  2670. 1:56:49these functional interface? The benefit
  2671. 1:56:51is that we can implement these
  2672. 1:56:54interfaces, we can implement these
  2673. 1:56:56methods in a very short and concise
  2674. 1:56:58syntax. Okay, that is what we call
  2675. 1:57:00lambda expressions. All right, let me
  2676. 1:57:03show you. So what I'm going to do is in
  2677. 1:57:06reality we have three to four very
  2678. 1:57:08important functional interfaces that we
  2679. 1:57:10use quite a lot along with streams. So
  2680. 1:57:12one by one I'm going to show you how we
  2681. 1:57:14can implement those using lambda
  2682. 1:57:16expressions. Okay. And that would be
  2683. 1:57:18very helpful in the video when we'll
  2684. 1:57:20move ahead. Okay. So, first I'll start
  2685. 1:57:23with predicate. What we are trying to
  2686. 1:57:25achieve is that we want to implement
  2687. 1:57:26this method test. Okay. It takes a
  2688. 1:57:28single argument and it returns a boolean
  2689. 1:57:31value. Okay. So, how will I do that?
  2690. 1:57:33I'll simply write predicate.
  2691. 1:57:36Let's say it is of the type integer.
  2692. 1:57:38Okay. So, this means that whatever I
  2693. 1:57:40pass here is actually of the type of
  2694. 1:57:42this argument. Okay. So, let's say I
  2695. 1:57:44name my object something P1. Now from
  2696. 1:57:47here I will start the lambda expression.
  2697. 1:57:49All right. So how does it work? Since
  2698. 1:57:52this method contains a single argument
  2699. 1:57:54so I will also take a single argument
  2700. 1:57:56like this and I can name it anything.
  2701. 1:57:59Let's say I name it num. After that I
  2702. 1:58:02will have to add a arrow operator. Okay.
  2703. 1:58:04Then followed by the curly braces and a
  2704. 1:58:07semicolon. All right. This is the
  2705. 1:58:09syntax. Okay. So whatever I write
  2706. 1:58:12between these two curly braces actually
  2707. 1:58:14represents the implementation of this
  2708. 1:58:16method test. Okay. So whatever I am
  2709. 1:58:19writing here is representing the
  2710. 1:58:20implementation. Okay. Now what do I need
  2711. 1:58:23to implement? Let's say I simply want to
  2712. 1:58:26check whether this number is even or
  2713. 1:58:28not. Okay. So what I can do? I can
  2714. 1:58:30simply write if num mod 2 is zero then
  2715. 1:58:34return true.
  2716. 1:58:36Okay. Otherwise return false.
  2717. 1:58:40All right. So this is how I have
  2718. 1:58:42implemented this predicate interface.
  2719. 1:58:44Okay. It is very simple, very short and
  2720. 1:58:46very concise. And this is what we call
  2721. 1:58:49lambda expressions. Okay. Now I could
  2722. 1:58:52have simplified it a bit more. Instead
  2723. 1:58:54of writing all this, I could have simply
  2724. 1:58:56returned return num mod 2 equal to zero.
  2725. 1:59:01Okay. We could simplify the if else like
  2726. 1:59:03this. And now that's the beauty of
  2727. 1:59:06lambda expression that when the
  2728. 1:59:08implementation is of a single line.
  2729. 1:59:10Okay, in this case the implementation is
  2730. 1:59:11of a single line. So I can remove this.
  2731. 1:59:14I can remove this curly brace and I can
  2732. 1:59:16remove the semicolon. And I also don't
  2733. 1:59:19need the return keyword. Okay. So I can
  2734. 1:59:22simply write it like this. All right.
  2735. 1:59:24That's the whole implementation. We just
  2736. 1:59:26have to write num which is the argument.
  2737. 1:59:29Okay. Which is this argument here. And
  2738. 1:59:32then I'm saying num mode 2 equal to
  2739. 1:59:34equal to0. So this means that I want to
  2740. 1:59:37return true if the number is even
  2741. 1:59:39otherwise I want to return false. Okay.
  2742. 1:59:42Similarly let's see some other
  2743. 1:59:44interfaces. We have this interface
  2744. 1:59:46function. Okay. Here it it takes an
  2745. 1:59:48argument but it does not return boolean.
  2746. 1:59:51Okay. It can return anything. All right.
  2747. 1:59:53Whatever you pass here it can return
  2748. 1:59:55anything. All right. Let's see. I'll
  2749. 1:59:58simply let's say I take integer and
  2750. 2:00:01integer only. Okay,
  2751. 2:00:06let me call it f_sub_1. So this time
  2752. 2:00:08again here we have a single argument.
  2753. 2:00:11Okay, so I will just take a single
  2754. 2:00:13argument. Let's say I take it as number
  2755. 2:00:14only. And I want to return I want to
  2756. 2:00:18return the square of this number. Okay,
  2757. 2:00:21like this num into num. All right. So
  2758. 2:00:26this is how I can provide an
  2759. 2:00:27implementation. Now again it is of a
  2760. 2:00:29single line. So I will remove this. I'll
  2761. 2:00:31remove this and I will remove the
  2762. 2:00:32semicolon and also the return keyword.
  2763. 2:00:35Okay. So this is how I can implement
  2764. 2:00:37this interface function. All right. Now
  2765. 2:00:39we have two more interfaces. There is
  2766. 2:00:42this interface consumer. Okay. It is
  2767. 2:00:44also very important. So it is a
  2768. 2:00:45functional interface and you will see
  2769. 2:00:47that it takes a single argument but it
  2770. 2:00:49has no return type. All right. So I'll
  2771. 2:00:52give one example.
  2772. 2:00:54Let's say I'll take integer only again
  2773. 2:00:58and what I need to do is it takes a
  2774. 2:01:00single argument. Okay. So again I'm
  2775. 2:01:02taking a number and let's say I just
  2776. 2:01:06simply want to print this number. Okay.
  2777. 2:01:08We don't have to return anything from
  2778. 2:01:10here. So I'll simply print it.
  2779. 2:01:12System.out.print
  2780. 2:01:14number. All right. Like this. Again it's
  2781. 2:01:17a single statement. I'll remove this and
  2782. 2:01:19I'll remove these two. And this I can
  2783. 2:01:22write over here like this. Similarly,
  2784. 2:01:25there is one more. There is an interface
  2785. 2:01:27supplier. Okay? So, it's kind of
  2786. 2:01:29opposite to consumer. What it does is it
  2787. 2:01:32does not take any argument, but it has a
  2788. 2:01:34return type. All right? So, this can be
  2789. 2:01:37used. Let's say the return type I want
  2790. 2:01:40to give as integer. Okay. So, I'll
  2791. 2:01:42simply say S1. Now, now this time I
  2792. 2:01:45don't have any argument. So, I'll simply
  2793. 2:01:46write it like this followed by this
  2794. 2:01:48arrow operator. And let's say I want to
  2795. 2:01:52return some value. Okay, simply I want
  2796. 2:01:54to return math dot random. I'll return a
  2797. 2:01:58random number. Okay, and let me cast it
  2798. 2:02:01to int.
  2799. 2:02:02All right, that's it. Simple. Now again
  2800. 2:02:06uh now why did I do that? Because this
  2801. 2:02:07does not take any parameter but it has
  2802. 2:02:09some return type. Okay, that's why I
  2803. 2:02:11returned some value. And in case of a
  2804. 2:02:13consumer, it took a parameter but it
  2805. 2:02:15didn't have any return type. Okay, so
  2806. 2:02:17that's how we implemented it. All right.
  2807. 2:02:20So again in this case it's a single
  2808. 2:02:22line. I'll remove this. I'll remove this
  2809. 2:02:25and I'll also remove the return
  2810. 2:02:27statement. Okay. So we can simply
  2811. 2:02:29implement it like this. All right. So
  2812. 2:02:31these are four interfaces that I
  2813. 2:02:33implemented. You will see that it's a
  2814. 2:02:35very simple implementation. We can do
  2815. 2:02:37all this in a single line. Now so far we
  2816. 2:02:40have seen that stream is an interface.
  2817. 2:02:42It has a lot of methods and it uses
  2818. 2:02:45functional programming. Now let's
  2819. 2:02:47understand how it actually processes the
  2820. 2:02:49data. So this is the data that I have
  2821. 2:02:52taken. It's a list of integers and
  2822. 2:02:55somehow I want to convert this list into
  2823. 2:02:57a stream. Okay. So how can I do that?
  2824. 2:03:00It's very simple. We can just call this
  2825. 2:03:02method named stream. All right. Now many
  2826. 2:03:06developers get confused here. The
  2827. 2:03:08confusion is that whether stream is an
  2828. 2:03:10interface or whether stream is a method.
  2829. 2:03:13Okay. So stream is actually an interface
  2830. 2:03:15only. But we also have a method named
  2831. 2:03:18stream which we use when we want to
  2832. 2:03:20convert any collection into a stream.
  2833. 2:03:23Okay. So I'll repeat stream is also a
  2834. 2:03:26method which we use when we want to
  2835. 2:03:28convert any collection into a stream.
  2836. 2:03:30Okay. So you'll see that the return type
  2837. 2:03:32of this method is actually stream only
  2838. 2:03:35which is the same interface that we just
  2839. 2:03:36saw. Okay. Now this stream is inside
  2840. 2:03:40this interface named collection. Now
  2841. 2:03:42what is collection? collection is
  2842. 2:03:44actually a parent interface of many data
  2843. 2:03:46structures. Okay, so list is one of
  2844. 2:03:49those data structures, right? I'll show
  2845. 2:03:51you. You can see that one of the
  2846. 2:03:53implementation is list. Similarly, we
  2847. 2:03:56have set as well and we have other data
  2848. 2:03:59structures as well. Okay, we have dq and
  2849. 2:04:01many more. All right, so any any
  2850. 2:04:04collection can be converted into a
  2851. 2:04:06stream just by calling this method
  2852. 2:04:08stream. All right, so that is what we
  2853. 2:04:11have done. We have converted this list
  2854. 2:04:13into a string by calling the method
  2855. 2:04:14stream. Okay, so I'll just extract a
  2856. 2:04:17variable.
  2857. 2:04:20Great. Okay, so we got a string. Now
  2858. 2:04:23what can we do? We can perform various
  2859. 2:04:25operations. Okay, so let's say I'll
  2860. 2:04:27start with a very simple operation. We
  2861. 2:04:29just want to print all the elements.
  2862. 2:04:31Okay, I want to print all the elements.
  2863. 2:04:33So for that I'll call a method named for
  2864. 2:04:37each. Okay. And here I'll say that for
  2865. 2:04:40every element we just have to print the
  2866. 2:04:44element. All right. Like this. Cool. So
  2867. 2:04:48you'll see that this is actually a
  2868. 2:04:50lambda expression. And if you look at it
  2869. 2:04:53carefully, this for each method takes a
  2870. 2:04:55consumer and this lambda expression is
  2871. 2:04:58actually an implementation of consumer
  2872. 2:05:00only. Okay, you can see here that this
  2873. 2:05:02is what we implemented a little while
  2874. 2:05:04ago. All right. So that is how we can
  2875. 2:05:06write it. Now if I'll just run it.
  2876. 2:05:10Great. You can see that all the elements
  2877. 2:05:12are printed. Okay. So this is one of the
  2878. 2:05:15method which loops on every element and
  2879. 2:05:17we can just provide a consumer
  2880. 2:05:19implementation here and that will be
  2881. 2:05:21executed. Okay. So we have written a
  2882. 2:05:23print statement that is why all the
  2883. 2:05:25elements are printed one by one. Okay.
  2884. 2:05:28Now I'll take another example. Let's say
  2885. 2:05:30this time I want to print only the even
  2886. 2:05:32elements. Okay. Only the elements that
  2887. 2:05:35are even that I want to print. So what I
  2888. 2:05:37can do is okay first one thing that I
  2889. 2:05:39want to do here I'll not write it like
  2890. 2:05:41this okay we don't need this stream
  2891. 2:05:43variable we can do it directly like this
  2892. 2:05:45one list dotstream
  2893. 2:05:47dot for each okay generally we do it
  2894. 2:05:50like this okay and I'll tell you why all
  2895. 2:05:52right in a little while we'll get to
  2896. 2:05:54know why that we why don't we extract it
  2897. 2:05:56in a variable okay for now we'll do it
  2898. 2:05:59like this now let's say I want to print
  2899. 2:06:01only the even elements okay so what can
  2900. 2:06:04I do list dotstream dot filter. So we
  2901. 2:06:08have another method filter. So it will
  2902. 2:06:10take every element and we can add some
  2903. 2:06:13condition. Let's say the condition is
  2904. 2:06:15element mode 2 equal to equal to zero.
  2905. 2:06:18Okay. And that's it. So what I'm saying
  2906. 2:06:20here is that this filter takes a
  2907. 2:06:22predicate and this is how we give a
  2908. 2:06:24predicate implementation. So here we can
  2909. 2:06:26pass on a condition and what filter will
  2910. 2:06:29do is that it will filter out all the
  2911. 2:06:31elements who which does not satisfy this
  2912. 2:06:34condition. Okay. So only for the
  2913. 2:06:36elements that will return true for this
  2914. 2:06:38those will be kept in the stream and
  2915. 2:06:40rest all will be removed. Okay. And one
  2916. 2:06:43more thing this filter actually returns
  2917. 2:06:45a stream only. Okay. So we still have a
  2918. 2:06:48stream. So we can still again call
  2919. 2:06:49another method. So now what I'll call is
  2920. 2:06:51I'll call for each because I also want
  2921. 2:06:54to print the elements. Okay. So again
  2922. 2:06:58I'll say for every element I just simply
  2923. 2:07:01want to print the elements. All right.
  2924. 2:07:04Like this. So what filter did it will
  2925. 2:07:07filter out all the elements that does
  2926. 2:07:09not satisfy this condition. Okay. So
  2927. 2:07:11only the elements that satisfy this
  2928. 2:07:13condition will remain. So after that
  2929. 2:07:15only the even elements will be present
  2930. 2:07:17and here we are printing those elements.
  2931. 2:07:20Okay. Okay. So I'll show you. Let me
  2932. 2:07:21just comment this out.
  2933. 2:07:25Great. So you will see that 2 4 and six
  2934. 2:07:28have been printed. Okay. And as I told
  2935. 2:07:31you this filter takes a predicate. Okay.
  2936. 2:07:33This you should know for for interview
  2937. 2:07:35point of view. Okay. Cool. Now I'll
  2938. 2:07:38perform another operation. What I want
  2939. 2:07:40to do is I'll just simply take
  2940. 2:07:44a stream and I want to print the square
  2941. 2:07:47of all these elements. Okay. So how can
  2942. 2:07:50I do that? Again I'll call a method
  2943. 2:07:53named map. Okay. And it takes every
  2944. 2:07:57element and it maps it into its square.
  2945. 2:08:01Okay. Like this.
  2946. 2:08:03All right. And then I want to print it
  2947. 2:08:06because map also returns a string. So I
  2948. 2:08:08can still call another method. So I call
  2949. 2:08:10this method for each. And here I'm just
  2950. 2:08:14printing every element. Out. Element.
  2951. 2:08:19Okay. Now here also we have given a
  2952. 2:08:22lambda expression and this one is
  2953. 2:08:25actually of the argument is function and
  2954. 2:08:27this expression is of the type is is a
  2955. 2:08:30lambda expression of function. Okay
  2956. 2:08:33good. So now this map what this map has
  2957. 2:08:35done is that it will take every element
  2958. 2:08:37and it will convert it into its square.
  2959. 2:08:39Okay. And then here we are printing
  2960. 2:08:41those. All right. So I'll run it again.
  2961. 2:08:44Okay. Let me just comment this.
  2962. 2:08:48Great. So you'll see that all the
  2963. 2:08:50elements are printed. One, all the
  2964. 2:08:52squares are printed. 1 4 9 16 25 36.
  2965. 2:08:56Okay. Now I could have also added a
  2966. 2:08:58filter here. What I can do is I'll just
  2967. 2:09:01simply change the example. First I I
  2968. 2:09:03want to filter all the elements and I
  2969. 2:09:05only want to keep the even elements.
  2970. 2:09:07Okay, like this.
  2971. 2:09:10And after keeping all the even elements,
  2972. 2:09:12I want to map it to their square and
  2973. 2:09:15then I want to print them. Okay. So that
  2974. 2:09:17also I can do. Great. 4 16 and 36. All
  2975. 2:09:21right. So first I called a filter that
  2976. 2:09:24filtered the elements. Now since it
  2977. 2:09:26still returns a stream. So I can call
  2978. 2:09:28another method. That's why I called map.
  2979. 2:09:30Now map has mapped these elements to
  2980. 2:09:32their square. Now it also returns a
  2981. 2:09:34stream only. This map also returns a
  2982. 2:09:37stream. So I moved ahead and I called
  2983. 2:09:39for each. And now using for each I am
  2984. 2:09:42printing all the elements. Okay. of
  2985. 2:09:44filter is taking a predicate
  2986. 2:09:45implementation. Map is taking a function
  2987. 2:09:48implementation and for each is taking a
  2988. 2:09:50consumer implementation.
  2989. 2:09:52All right, I'll take another example.
  2990. 2:09:56Let's say we have a list of employees.
  2991. 2:09:58Employee is a class which has these
  2992. 2:10:00fields ID, name and department. Now in
  2993. 2:10:03an interview, someone asks you to print
  2994. 2:10:06the name of all the employees whose
  2995. 2:10:08department is tech and do this using
  2996. 2:10:11functional programming. So how will you
  2997. 2:10:13do this? Let's find out. So we have this
  2998. 2:10:16list of employees inside this class
  2999. 2:10:18employee. We have ID, name and
  3000. 2:10:21department. And we have created getter
  3001. 2:10:23methods for all these fields. Okay. I
  3002. 2:10:25have a get ID, get name, and get
  3003. 2:10:27department. Okay. Now what I want is I
  3004. 2:10:31want all the employees whose department
  3005. 2:10:33is tech and I only want to print the
  3006. 2:10:36name of those employees. Okay. Only want
  3007. 2:10:38to print the name. All right. So how can
  3008. 2:10:41I do that? First I'll take the list and
  3009. 2:10:45convert it into a string. Okay, then
  3010. 2:10:48I'll just simply filter out all the
  3011. 2:10:51employees whose department is tech.
  3012. 2:10:53Okay, so I'll take an argument employee
  3013. 2:10:56and I'll simply add a condition that
  3014. 2:10:59employee dot get department dot equals
  3015. 2:11:03tech.
  3016. 2:11:05Okay, this is how I can add a predicate
  3017. 2:11:07condition. Okay, filter takes a
  3018. 2:11:09predicate and the condition says that
  3019. 2:11:12all the employees whose department
  3020. 2:11:13equals to tech those should be kept and
  3021. 2:11:16rest all should be filtered out. Okay.
  3022. 2:11:19Now what I want is that as of now in
  3023. 2:11:22this list we only have employees. Okay.
  3024. 2:11:24You can see that here also we are
  3025. 2:11:25looping on employees and using this
  3026. 2:11:27employee reference we have called this
  3027. 2:11:29get department method. Okay. But in the
  3028. 2:11:31end what I want is I want the name of
  3029. 2:11:34those employees. Okay. I don't want the
  3030. 2:11:36employees. I want to print the name. So
  3031. 2:11:38what I can do is I can just take a map.
  3032. 2:11:42I can call this method map. And we have
  3033. 2:11:45employee but I will map it to employee
  3034. 2:11:48dot get name. Okay. And now I will just
  3035. 2:11:53print all these names like this.
  3036. 2:11:57All right. System.out.t
  3037. 2:12:00print ln name. Okay. So what we did was
  3038. 2:12:05first we converted the list into a
  3039. 2:12:06stream. Then we filtered out all the
  3040. 2:12:10employees whose department is equal to
  3041. 2:12:12tech. Then here we mapped the employee
  3042. 2:12:15into its name. Okay. So now after that
  3043. 2:12:18the list this list the stream has become
  3044. 2:12:20a stream of names. Okay. So here when
  3045. 2:12:23I'll loop on it using for each here I
  3046. 2:12:25will get a name. Okay. You can see that
  3047. 2:12:27this is string and this is of the type
  3048. 2:12:28employee. And then here I'm just
  3049. 2:12:30printing all those names. All right. So
  3050. 2:12:33this is how filter map and for each
  3051. 2:12:35works. Okay. So I'll show you this as
  3052. 2:12:37well.
  3053. 2:12:40Great. So you will see that Amit, Rahul,
  3054. 2:12:43Vikas and Arjun. So Amit, Rahul, Vikas
  3055. 2:12:46and Arjun all these have their
  3056. 2:12:48department as tech. And you'll see that
  3057. 2:12:50not all the employees are printed. Only
  3058. 2:12:52their names are printed. Okay.
  3059. 2:12:56Now let's look at another important
  3060. 2:12:58method. Collect. Collect is a method
  3061. 2:13:01inside stream interface which takes all
  3062. 2:13:04the elements from the stream and
  3063. 2:13:06transform them into some final result
  3064. 2:13:09like a list set or a map. All right. So
  3065. 2:13:12if you want to convert your stream back
  3066. 2:13:14into a data structure, you can use
  3067. 2:13:16collect. Okay. So let's see how. So I'll
  3068. 2:13:20take the same example. Initially I had a
  3069. 2:13:23list of employees. I converted it into a
  3070. 2:13:26stream. Then I did some filtering, some
  3071. 2:13:29mapping and in the end I'm looping on
  3072. 2:13:31this stream and I'm printing all the
  3073. 2:13:34names. Okay, but let's say I don't want
  3074. 2:13:37to print all the names. Instead, I want
  3075. 2:13:39this stream to be converted back into
  3076. 2:13:42some data structure. Okay, so after the
  3077. 2:13:45filtering and the mapping, I don't want
  3078. 2:13:46to print right away. I just want to
  3079. 2:13:48convert it back into some data
  3080. 2:13:50structure. Okay, so for that I can use
  3081. 2:13:53the method collect. Okay. Now let's say
  3082. 2:13:56I want it in the form of a list. So for
  3083. 2:13:58that I will have to write it like this
  3084. 2:14:01collectors dot to list. Okay. And here I
  3085. 2:14:05can add it in some variable.
  3086. 2:14:08All right.
  3087. 2:14:10Let's say I say names.
  3088. 2:14:14All right. So eventually I will get a
  3089. 2:14:16list of string. Okay. So I had a list of
  3090. 2:14:18employee. I filtered all the employees
  3091. 2:14:20whose department were tech. Then I
  3092. 2:14:23mapped it into employee.get. get name.
  3093. 2:14:25So at this point of time when when this
  3094. 2:14:27map is called now we have a stream of
  3095. 2:14:30names. Okay, a stream of strings and
  3096. 2:14:32then we convert it back into a list.
  3097. 2:14:34That is why we get a list of string.
  3098. 2:14:37Okay, we get only the names because we
  3099. 2:14:39have mapped the employee to employee.get
  3100. 2:14:41name. Okay, so this is how we can use
  3101. 2:14:44collect. Now if I wanted it to be in the
  3102. 2:14:47form of set, so for that I could have
  3103. 2:14:50called to set and in this case it would
  3104. 2:14:53be a set of string.
  3105. 2:14:56Okay, cool. Now I will take one more
  3106. 2:14:59example. Let's say I want to convert
  3107. 2:15:01this into a map. Okay, now map is a bit
  3108. 2:15:05tricky because for map you will have to
  3109. 2:15:07define the key as well as the value.
  3110. 2:15:10Okay, so you will have to define what
  3111. 2:15:12key you want to take and what value you
  3112. 2:15:14want to take. So let's say we have this
  3113. 2:15:16list of employees and I want to convert
  3114. 2:15:18it into a map whose key is the employee
  3115. 2:15:20ID and value is the employee name. Okay,
  3116. 2:15:24let's say I want to convert it into
  3117. 2:15:25something like that. Okay, so how can I
  3118. 2:15:28do it? I'll take the employee. I'll
  3119. 2:15:30convert it into a stream. I'll call the
  3120. 2:15:33method collect and here I can just write
  3121. 2:15:36collectors
  3122. 2:15:39dot to map. Okay, but it does not end
  3123. 2:15:43here like this because now you will have
  3124. 2:15:45to define what is the key and what is
  3125. 2:15:47the value. Okay, that is why this two
  3126. 2:15:49map takes two arguments. The first one
  3127. 2:15:52defines the key and the second one
  3128. 2:15:54defines the value. Okay, and you can see
  3129. 2:15:57that both these arguments are actually
  3130. 2:15:59of the type function. Okay, so in both
  3131. 2:16:01these arguments we have to provide
  3132. 2:16:03lambda expressions and those lambda
  3133. 2:16:05expressions the first one will represent
  3134. 2:16:07the key and second one will represent
  3135. 2:16:09the function. Okay, lambda expressions
  3136. 2:16:11will be of the interface function. All
  3137. 2:16:13right, so we have already seen how we
  3138. 2:16:15implement function. Let's say I want the
  3139. 2:16:18key to be integer. Okay, and I have my
  3140. 2:16:23argument as employee. So I'll take it
  3141. 2:16:25like this. Function employee, integer.
  3142. 2:16:29Let's call it f_sub_1. Okay. So I have
  3143. 2:16:32employee that I know and
  3144. 2:16:36I want to convert it into I want to
  3145. 2:16:39fetch the key out of it. Okay. So I want
  3146. 2:16:41to map it into the ID. Sorry, I want to
  3147. 2:16:43fetch the ID out of it. Okay. So this
  3148. 2:16:45will be my first lambda expression.
  3149. 2:16:48Similarly, I'll create one more. I have
  3150. 2:16:51a employee and from this employee, I
  3151. 2:16:53want the name. Okay, that will be my
  3152. 2:16:56value. So I have this employee
  3153. 2:17:00and I'll simply do employee dot get
  3154. 2:17:03name. Okay, like this. So this is the
  3155. 2:17:07first lambda expression which will
  3156. 2:17:08represent the key and this is the second
  3157. 2:17:10lambda expression which will represent
  3158. 2:17:12the value. All right. So here I will
  3159. 2:17:14just add f_sub_1, f_sub_2 like this. Got
  3160. 2:17:19it? So in this method to map we pass on
  3161. 2:17:22two function arguments and first one
  3162. 2:17:25represent the key and second one
  3163. 2:17:26represents the value. Okay. So the key
  3164. 2:17:28will be ID and the value will be name.
  3165. 2:17:31All right. Let's see. I'll just extract
  3166. 2:17:33a variable.
  3167. 2:17:37Great. So let me call it employee map.
  3168. 2:17:43Okay. So you'll see that the key is of
  3169. 2:17:45the type integer and value is of the
  3170. 2:17:47type string.
  3171. 2:17:49So now I'll just print this map to show
  3172. 2:17:52you what exactly is the final result.
  3173. 2:17:55Okay. So we have this employees map. If
  3174. 2:17:57I simply just run it.
  3175. 2:18:01Great. So you will see that we have a
  3176. 2:18:03map where key is the id. Okay. We have
  3177. 2:18:06all the ids and value is their
  3178. 2:18:09corresponding names. Okay. So against
  3179. 2:18:11one we have a similarly against two we
  3180. 2:18:13have nha and so on. Okay. So this is how
  3181. 2:18:17we can use collect along with these
  3182. 2:18:19various functionalities. Collectors dot2
  3183. 2:18:22list to map and to set. Now let's
  3184. 2:18:25understand another very important
  3185. 2:18:26concept. Intermediate operations and
  3186. 2:18:29terminal operations in streams. First
  3187. 2:18:32let's talk about intermediate
  3188. 2:18:33operations. These are the operations
  3189. 2:18:36that transform or process the data in a
  3190. 2:18:38stream but they do not actually produce
  3191. 2:18:41a result of their own. Some common
  3192. 2:18:43examples of intermediate operations are
  3193. 2:18:46filter, map, sorted, etc. Terminal
  3194. 2:18:50operations are the operations that
  3195. 2:18:52trigger the execution of the stream and
  3196. 2:18:54produce a final result. Examples of
  3197. 2:18:57terminal operations include collect, for
  3198. 2:18:59each, count, etc. All right, let's see
  3199. 2:19:02it in practical. So I have taken the
  3200. 2:19:05same example. We have a list of
  3201. 2:19:07integers. We convert them into a string.
  3202. 2:19:10Then we apply a filter and we keep all
  3203. 2:19:13the even elements. Then we provide a
  3204. 2:19:15map. We apply a map and we convert these
  3205. 2:19:18elements into its square. Okay. Then we
  3206. 2:19:22call for each and we print all the
  3207. 2:19:24elements. All right. So here filter and
  3208. 2:19:27map are intermediate operations and for
  3209. 2:19:30each is a terminal operation. Okay. Why?
  3210. 2:19:33Because filter returns a stream. And
  3211. 2:19:35that is why even after calling this
  3212. 2:19:37method, I can still do further
  3213. 2:19:40processing. Okay, because I still have a
  3214. 2:19:42string. Similarly, map also returns a
  3215. 2:19:44stream. So that is why I was able to
  3216. 2:19:46call this method for each because after
  3217. 2:19:50calling this method, I still had a
  3218. 2:19:51string. Right? So that is why these are
  3219. 2:19:54called intermediate operations. But for
  3220. 2:19:56each actually does not return anything.
  3221. 2:19:59That is why it is a terminal operation.
  3222. 2:20:01Okay? because it will end the stream and
  3223. 2:20:04now we cannot do any further processing.
  3224. 2:20:07Okay. Similarly, we have other examples.
  3225. 2:20:10I have a method named count. Okay. So,
  3226. 2:20:13this count returns a long value. But
  3227. 2:20:16again, it does not return a stream.
  3228. 2:20:18Okay. So, when we call this method, the
  3229. 2:20:20stream has been ended. All right. Now,
  3230. 2:20:23what's this method used for? It actually
  3231. 2:20:25returns the count of all the elements
  3232. 2:20:27that are remaining in the stream. Okay.
  3233. 2:20:30So I'll just print this long count.
  3234. 2:20:36All right. So it will actually return
  3235. 2:20:38the count if we just print this.
  3236. 2:20:44Great. So the count is three because
  3237. 2:20:47after we applied the filter only three
  3238. 2:20:49elements were remaining. Okay. Count is
  3239. 2:20:52another terminal operation. Similarly we
  3240. 2:20:54have collect. Collect is also another
  3241. 2:20:57terminal operation because this also
  3242. 2:21:01ends the stream and it converts the
  3243. 2:21:03stream back into some data structure.
  3244. 2:21:05Okay, like in this case we have
  3245. 2:21:07converted it back into some list.
  3246. 2:21:10All right, list of integer. Let's call
  3247. 2:21:13it a final list.
  3248. 2:21:15Okay, now there is a very important
  3249. 2:21:17interview concept that comes from this
  3250. 2:21:19topic. The thing is that once a stream
  3251. 2:21:22is used, it cannot be reused.
  3252. 2:21:25Basically once you call a terminal
  3253. 2:21:28operation on a stream then you cannot
  3254. 2:21:30reuse a stream. Okay, let me show you
  3255. 2:21:33what I what I'm trying to say. If you
  3256. 2:21:36remember initially we just took this
  3257. 2:21:38list stream and we extracted it in some
  3258. 2:21:42variable okay like this and then let me
  3259. 2:21:46call it just s okay and then we applied
  3260. 2:21:49filter or map or anything on it. Okay,
  3261. 2:21:51we can do it like this also. I know we
  3262. 2:21:53can just uh take call this method
  3263. 2:21:55stream. We can add it in some variable
  3264. 2:21:58and then we can call these methods.
  3265. 2:22:00Okay, so let me run it like this. Great.
  3266. 2:22:03So you will see that there is no issue
  3267. 2:22:04in this. Okay, but let's say let's say I
  3268. 2:22:09took this s again and I called
  3269. 2:22:13s dot
  3270. 2:22:15let's say anything I called s do. Okay.
  3271. 2:22:17Again and I'm trying these elements and
  3272. 2:22:22I write anything element
  3273. 2:22:24mod 3 equal to equal to0. Let's say this
  3274. 2:22:27is the condition that I want to give.
  3275. 2:22:29Okay. So now if I run my project again.
  3276. 2:22:33Yes. So we will have some exception and
  3277. 2:22:36the exception says that stream has
  3278. 2:22:38already been operated upon or closed.
  3279. 2:22:41All right. So what I mean by that is
  3280. 2:22:43that when we had a stream we can perform
  3281. 2:22:46as many intermediate operations as we
  3282. 2:22:48want but after we call a terminal
  3283. 2:22:50operation then the stream has been
  3284. 2:22:52closed. So you cannot use this variable
  3285. 2:22:55again to call another intermediate
  3286. 2:22:57operation. Okay that will give you an
  3287. 2:22:59error. That is why it is not recommended
  3288. 2:23:02that you extract this in some variable
  3289. 2:23:04because it this kind of code is possible
  3290. 2:23:07and it will throw you some exception.
  3291. 2:23:09Okay. because at this point at line
  3292. 2:23:11number 20 the stream was actually
  3293. 2:23:13closed. Now if you will try to call
  3294. 2:23:15another intermediate method it will give
  3295. 2:23:17you some exception. Okay. So this is a
  3296. 2:23:20very important interview question that
  3297. 2:23:22is asked a lot. Okay. So you should know
  3298. 2:23:24about this. All right. So I'll just
  3299. 2:23:26change it back to list.stream.
  3300. 2:23:29list stream like this. Okay. Let me just
  3301. 2:23:32remove this. Great.
  3302. 2:23:35All right. So at this point the stream
  3303. 2:23:37has been ended. Okay. Now there is one
  3304. 2:23:39more interview question that comes from
  3305. 2:23:41this topic. It says that all the
  3306. 2:23:43intermediate operations are lazy loaded
  3307. 2:23:46and the terminal operations are eagerly
  3308. 2:23:48loaded. Okay. So what exactly do I mean
  3309. 2:23:51by that by lazy loading? I mean that
  3310. 2:23:54these methods are not called until this
  3311. 2:23:57ter until we add a terminal operation.
  3312. 2:24:00Okay, I'll show you this. Let's say if I
  3313. 2:24:02just remove this operation and I end it
  3314. 2:24:04here. Okay, I just write list.tra.filter
  3315. 2:24:08filter.m map. Okay. And uh let me just
  3316. 2:24:11change the implementation a little bit.
  3317. 2:24:14Okay. I'll take a completely different
  3318. 2:24:16example altogether. All right. Let me
  3319. 2:24:19remove this. So I have a list. I take a
  3320. 2:24:22stream. Let's say I call a method
  3321. 2:24:24filter. Okay. So I'm taking every
  3322. 2:24:27element
  3323. 2:24:29and I provide some implementation here.
  3324. 2:24:31Okay. So here I am writing a print
  3325. 2:24:33statement as well just to show it to
  3326. 2:24:35you.
  3327. 2:24:36I'm saying filter method called. Okay.
  3328. 2:24:42And I'm saying if element mod 2 equal
  3329. 2:24:46equal to0 return true. Okay. If you
  3330. 2:24:50remember it, we did it like this
  3331. 2:24:52initially. Okay. Now it's not a single
  3332. 2:24:55line implementation. So that's why I
  3333. 2:24:57cannot remove these curly braces. Okay.
  3334. 2:25:00Similarly, I'll call do map. And here as
  3335. 2:25:03well, I'll do kind of a same thing. I'll
  3336. 2:25:05take an element
  3337. 2:25:08and let's say I write another print
  3338. 2:25:11statement that says
  3339. 2:25:14map method called
  3340. 2:25:17okay and my implementation is that I am
  3341. 2:25:20returning element
  3342. 2:25:22into element
  3343. 2:25:24okay and I just ended over here all
  3344. 2:25:28right so I've called a filter method
  3345. 2:25:31I've called a map method I've added
  3346. 2:25:33print statements in their implementation
  3347. 2:25:34ations and I've not called any terminal
  3348. 2:25:37operation. Okay. So if I'll just run my
  3349. 2:25:40project.
  3350. 2:25:42Great. So you will see that these
  3351. 2:25:45statements have not been printed. Okay.
  3352. 2:25:48So this means that filter and map has
  3353. 2:25:50not been called at all. Okay. It has not
  3354. 2:25:53been executed. Now why? Because they are
  3355. 2:25:56lazy loaded which means that they will
  3356. 2:25:58only be executed when we'll add a
  3357. 2:26:00terminal operation on them. Okay. So
  3358. 2:26:02let's say now I just add a simple
  3359. 2:26:05operation dot collect collectors dot to
  3360. 2:26:08list. Okay. So I've done nothing else.
  3361. 2:26:10I've just added another operation which
  3362. 2:26:12is a terminal operation. I'm not even
  3363. 2:26:14using this list. Okay. I've just added
  3364. 2:26:17this method. And now if I'll run it
  3365. 2:26:20great you will see that these these
  3366. 2:26:23statements have been printed. So this
  3367. 2:26:25filter and map has been called again and
  3368. 2:26:27again like again and again because they
  3369. 2:26:29loop on this list and filter out
  3370. 2:26:30elements one by one. Similarly map all
  3371. 2:26:32the elements one by one. That's why you
  3372. 2:26:34see so many print statements. But the
  3373. 2:26:36crux is my point is that both these
  3374. 2:26:38methods were executed only when we add a
  3375. 2:26:41terminal operation. Okay. So this is
  3376. 2:26:43what lazy loading and eager loading is.
  3377. 2:26:46Again it's a very important interview
  3378. 2:26:48topic and you should definitely know
  3379. 2:26:49this. All right.
  3380. 2:26:52Now these are a few other commonly used
  3381. 2:26:54stream methods. Let's go through them
  3382. 2:26:57one by one and understand how each of
  3383. 2:26:59them works.
  3384. 2:27:01Find first is a terminal method that
  3385. 2:27:03returns the first element of the string.
  3386. 2:27:06All right, let's see. So I have taken
  3387. 2:27:09this list of different names. Let's say
  3388. 2:27:12I want to convert it into a stream and
  3389. 2:27:15call this method find first. Okay, so
  3390. 2:27:18this will return the first element from
  3391. 2:27:20this list. Okay, so let me just extract
  3392. 2:27:24it in some variable.
  3393. 2:27:26Okay, so this actually returns an
  3394. 2:27:28optional. So if I actually want to print
  3395. 2:27:31it, I will have to call this method get.
  3396. 2:27:34Okay, first dot get.
  3397. 2:27:37All right, like this. Okay, if I run it,
  3398. 2:27:41great. So you'll see that ammon has been
  3399. 2:27:43printed because ammon is the first
  3400. 2:27:45element. Okay, now generally we use this
  3401. 2:27:48find first along with some condition.
  3402. 2:27:50Okay. So, generally we add a filter and
  3403. 2:27:53let's say I apply a filter that says
  3404. 2:27:57that the string starts with
  3405. 2:28:01let's say a okay or let me call it some
  3406. 2:28:05other example let me take it as r all
  3407. 2:28:08right now if I run it
  3408. 2:28:11okay so it will return raj all right all
  3409. 2:28:14right why because this was the first
  3410. 2:28:16string that starts with r okay so first
  3411. 2:28:18I applied a filter and Then as soon as
  3412. 2:28:21it gets the first element, it will
  3413. 2:28:23simply return it. Okay, find any is a
  3414. 2:28:27terminal method that returns any element
  3415. 2:28:29from the string. It is not guaranteed
  3416. 2:28:32that it will be the first one. All
  3417. 2:28:34right, let's see. So in this example
  3418. 2:28:36only instead of find first, I can just
  3419. 2:28:39simply call find any. Okay, so it is not
  3420. 2:28:41guaranteed that it will return the first
  3421. 2:28:43element. It can return any of the
  3422. 2:28:45element. But generally when the list is
  3423. 2:28:47this small and uh we are calling this
  3424. 2:28:50find any it mostly returns the first one
  3425. 2:28:52only. Okay. So you'll see that it
  3426. 2:28:53returns Raj only. But there is a concept
  3427. 2:28:56of parallel processing. Okay. Sometimes
  3428. 2:28:58we work parallelly on the streams and at
  3429. 2:29:01that point of time this method is used
  3430. 2:29:03quite a lot. Okay. Because at that time
  3431. 2:29:05it can return any result. Okay. Now any
  3432. 2:29:09match is a terminal method which returns
  3433. 2:29:11true if any of the element matches a
  3434. 2:29:14given condition. So we have these names.
  3435. 2:29:17Let's say I convert it into a stream and
  3436. 2:29:20call the method any match. Okay. Now
  3437. 2:29:23inside this method we can actually pass
  3438. 2:29:25a predicate implementation. Okay. So I
  3439. 2:29:28can pass a lambda expression with some
  3440. 2:29:30condition. So let's say I say that if
  3441. 2:29:32any of the name starts
  3442. 2:29:37starts with R. Okay, this is a predicate
  3443. 2:29:42implementation that I can add here. So
  3444. 2:29:44what any match will do is that if any of
  3445. 2:29:47these element satisfy this condition
  3446. 2:29:50then it will return true. Okay. So this
  3447. 2:29:52method actually returns a boolean. Okay.
  3448. 2:29:55Otherwise it will return false. So I'll
  3449. 2:29:57just extract it in some variable. Let me
  3450. 2:29:59call it boolean value. Okay. And we'll
  3451. 2:30:02just simply print it.
  3452. 2:30:05All right. If I run it.
  3453. 2:30:09Great. So it returns true because we
  3454. 2:30:11have elements that start with R. Okay.
  3455. 2:30:14But let's say I just write starts with
  3456. 2:30:16Z. Okay. Now if I run it. So now it will
  3457. 2:30:20return false because none of the element
  3458. 2:30:22starts with zed. Right? Now all match is
  3459. 2:30:26a terminal method which returns true if
  3460. 2:30:29all the elements match a given
  3461. 2:30:31condition. Okay. Let's see. So if I'll
  3462. 2:30:34just replace it with all match. Okay.
  3463. 2:30:37And let's say I just test it for R.
  3464. 2:30:41Okay. So if I run it, it will return
  3465. 2:30:44false because all math says that if all
  3466. 2:30:47the elements would have been started
  3467. 2:30:49from R, then only it will return true.
  3468. 2:30:52Okay. So let's say I provide a different
  3469. 2:30:54condition I can just simply say
  3470. 2:30:58str.length
  3471. 2:31:00is greater than one. Okay. So we have
  3472. 2:31:02all the strings whose length is greater
  3473. 2:31:03than one. Now if I run it, so now it
  3474. 2:31:06returns true. Okay. So this is how all
  3475. 2:31:09match works. Now none match is a
  3476. 2:31:12terminal method which returns true if no
  3477. 2:31:15elements match a given condition. All
  3478. 2:31:17right. Let's see. So if I replace it
  3479. 2:31:19with none match. So none match returns
  3480. 2:31:22true if none of the element matches the
  3481. 2:31:25given condition. Okay. So if I'll run it
  3482. 2:31:27for this, it will return false because
  3483. 2:31:30all the elements matches this condition.
  3484. 2:31:32Let's say I change it back to strarts
  3485. 2:31:35with R. So again you'll see that it will
  3486. 2:31:39return false because few because most of
  3487. 2:31:42these names start with R. Okay. In fact
  3488. 2:31:46if I'll do it for A then also it will
  3489. 2:31:49return false because Ammon starts with
  3490. 2:31:51A. Okay. So it it requires that none of
  3491. 2:31:54the elements should match that
  3492. 2:31:55condition. Okay. So let's say I say
  3493. 2:31:58starts with zed. Now if I'll run it. So
  3494. 2:32:01now it returns true because none of the
  3495. 2:32:04elements start with zed. Okay. So these
  3496. 2:32:06were few of the methods that we we can
  3497. 2:32:08use quite a lot. Okay. Whenever we want
  3498. 2:32:10to fetch some boolean value in the end.
  3499. 2:32:13Okay. Now sorted is an intermediate
  3500. 2:32:16method which returns a string with all
  3501. 2:32:19the elements sorted. All right. Let's
  3502. 2:32:21see. So I have taken an unsorted list
  3503. 2:32:25and if I simply just call numbers
  3504. 2:32:28dotstream dot sorted. So this will
  3505. 2:32:31return me a sorted list. Okay. But it is
  3506. 2:32:33an intermediate method. So I will have
  3507. 2:32:35to call a terminal operation. Let's say
  3508. 2:32:38I call this. And now if I print all the
  3509. 2:32:41elements.
  3510. 2:32:43Okay. If I print all the number, you
  3511. 2:32:46will see that these are in sorted
  3512. 2:32:49format. Okay. So sorted is quite simple.
  3513. 2:32:52We can just directly call it and we can
  3514. 2:32:54sort any list. All right. Now let's talk
  3515. 2:32:58about another interesting concept.
  3516. 2:33:00Primitive streams in Java. Normally
  3517. 2:33:03stream works with collection but Java
  3518. 2:33:05also provides special streams for
  3519. 2:33:07primitive types like int, long and
  3520. 2:33:10double. These are called int stream,
  3521. 2:33:13long stream and double stream. All
  3522. 2:33:15right, let's look at them in practical.
  3523. 2:33:18So we have this interface named int
  3524. 2:33:20stream. If I go inside this, you'll see
  3525. 2:33:24that it is another interface which is
  3526. 2:33:26quite similar to stream. As you can see,
  3527. 2:33:28it has all these methods. We have
  3528. 2:33:30filter, we have map and we have various
  3529. 2:33:32other methods. Okay, but this is not
  3530. 2:33:35actually a stream. It is a different
  3531. 2:33:37interface and it is specific for
  3532. 2:33:40primitive data type. Okay, so int stream
  3533. 2:33:42is specific for int only. Okay, so you
  3534. 2:33:45we use it with some int values. Let's
  3535. 2:33:48how can I create it? It's very simple. I
  3536. 2:33:51can just call instream dot range. Okay,
  3537. 2:33:54so it has this static method inside it
  3538. 2:33:56which we can call and in the range I can
  3539. 2:33:58just pass the range. Okay, so 1 to 5.
  3540. 2:34:01Okay, so this means that it's a stream
  3541. 2:34:03of 1 2 3 4 and 5. All right. Now using
  3542. 2:34:07this end stream we can perform various
  3543. 2:34:09operations. Let's say I want to
  3544. 2:34:11calculate the average. Okay. So I can
  3545. 2:34:14just call this method average. I'll just
  3546. 2:34:17extract this variable and I'll simply
  3547. 2:34:20print it. Okay, average dot get as
  3548. 2:34:24double. Okay, so in the end we have to
  3549. 2:34:26convert it into a double value. Okay, so
  3550. 2:34:29if I run it,
  3551. 2:34:31great. It will return the average of all
  3552. 2:34:33the values. Okay, similarly we have a
  3553. 2:34:36method named sum. Okay, if I want the
  3554. 2:34:38sum of all the values, so it will return
  3555. 2:34:41me an int value. Okay, int sum. And I
  3556. 2:34:45can just simply print this as well. All
  3557. 2:34:48right.
  3558. 2:34:51Great. So you can see it gives me the
  3559. 2:34:53sum of all the values. Okay. Similarly,
  3560. 2:34:56we have other interfaces. We have this
  3561. 2:34:58interface double stream. This one is
  3562. 2:35:00meant for double values. Okay. So how
  3563. 2:35:03can I create it? Can create it like
  3564. 2:35:05this. Double stream dot of off. And here
  3565. 2:35:09I can pass any value that I want. Okay.
  3566. 2:35:12Let's say I pass random double values.
  3567. 2:35:14So a stream of double has been created.
  3568. 2:35:17Okay. Similarly, this also has this
  3569. 2:35:20method average. And we have other
  3570. 2:35:23methods as well. Okay. So I'm just
  3571. 2:35:25showing you a few of them.
  3572. 2:35:29Alen
  3573. 2:35:31double stream dot average dot get as
  3574. 2:35:34double. Okay. And we can run it and it
  3575. 2:35:38will just give me the average of these
  3576. 2:35:40three numbers. Okay. Similarly, we have
  3577. 2:35:43another interface named long stream.
  3578. 2:35:46Okay. So, this also I'll show you. We
  3579. 2:35:47have this interface long stream. Okay.
  3580. 2:35:50This also we can create in the same
  3581. 2:35:52fashion. We can just say long stream dot
  3582. 2:35:55off or we could have used a range as
  3583. 2:35:57well. And let's say 1 2.3 4.5. Okay. We
  3584. 2:36:02can give different values here. Okay. We
  3585. 2:36:04have to give these long values.
  3586. 2:36:08Okay. Sorry, my bad. It's long. So we
  3587. 2:36:10can take values like this.
  3588. 2:36:13Okay. So similarly I can perform
  3589. 2:36:14operations on long stream as well. All
  3590. 2:36:17right. So the crux is that for primitive
  3591. 2:36:20data types as well we have a stream
  3592. 2:36:22interface. We have different interfaces
  3593. 2:36:24for all of them. But you should know
  3594. 2:36:26that this is different from the actual
  3595. 2:36:28stream interface. Okay you should know.
  3596. 2:36:31All right. Now let's talk about parallel
  3597. 2:36:34streams. Normally streams process the
  3598. 2:36:37elements sequentially one after another.
  3599. 2:36:40But sometimes we want to process large
  3600. 2:36:42collections faster by using multiple
  3601. 2:36:45threads. That's where parallel stream
  3602. 2:36:47comes in. It allows the stream to
  3603. 2:36:50process elements in parallel. Using this
  3604. 2:36:52parallel stream method, we can make
  3605. 2:36:54operations faster especially for larger
  3606. 2:36:57data sets. But for small collections, it
  3607. 2:37:00may not make much of a difference. But
  3608. 2:37:02still for large collections, we can use
  3609. 2:37:03this method. All right, let's see. So,
  3610. 2:37:06we have this list of names. Now, instead
  3611. 2:37:09of calling the method stream, I will
  3612. 2:37:12call dot parallel stream. Okay. And then
  3613. 2:37:15I can perform the same operations that I
  3614. 2:37:18used to do earlier. All right. So, I can
  3615. 2:37:20call a filter method. I can just simply
  3616. 2:37:23filter out some values.
  3617. 2:37:26Let's say I want to add a condition that
  3618. 2:37:28string starts with R. Okay. And then I
  3619. 2:37:31want to call for each and I'll just take
  3620. 2:37:34every name and we'll just print these
  3621. 2:37:37names.
  3622. 2:37:39Okay. So the only difference is that
  3623. 2:37:42instead of running sequentially instead
  3624. 2:37:45of looping on the list sequentially now
  3625. 2:37:47the stream will work in a parallel way.
  3626. 2:37:49Okay. Using multiple threads. All right.
  3627. 2:37:52So if I run it
  3628. 2:37:56great. So you will see that these names
  3629. 2:37:59are not printed in a sequential order.
  3630. 2:38:01You see that Rahul is printed first then
  3631. 2:38:04Raj then Ria Reena. Okay. So they are
  3632. 2:38:07not in their actual order. This is
  3633. 2:38:10because we were processing on the stream
  3634. 2:38:12in a parallel way. Okay. So it would
  3635. 2:38:14have actually been processed faster
  3636. 2:38:17because multiple threads are being used.
  3637. 2:38:19However, when it's a very small list, so
  3638. 2:38:22it won't make much of a difference. But
  3639. 2:38:23when we are working with larger data
  3640. 2:38:25sets at that point of time if we want to
  3641. 2:38:28process faster then we can use parallel
  3642. 2:38:30string okay because it will open
  3643. 2:38:32multiple threads.
  3644. 2:38:34All right. Hello and welcome to my
  3645. 2:38:36channel the curious coder. This video is
  3646. 2:38:39part of my ongoing Java 8 series and
  3647. 2:38:42today we are going to talk about another
  3648. 2:38:44important feature of Java 8 the new
  3649. 2:38:46datetime API. In Java 8 a new package
  3650. 2:38:50called Java time was introduced. This
  3651. 2:38:53was added to fix the shortcomings of the
  3652. 2:38:55older classes like date and calendar.
  3653. 2:38:58Now what exactly was improved? Earlier
  3654. 2:39:01the old classes were mutable which made
  3655. 2:39:03them errorprone. But in the new package
  3656. 2:39:06all classes are immutable and thread
  3657. 2:39:08safe making them much safer to use. The
  3658. 2:39:12old classes were also confusing and
  3659. 2:39:14poorly designed. But the new package
  3660. 2:39:16provides clear and separate classes like
  3661. 2:39:19local date, local time and local date
  3662. 2:39:21time. each serving a specific purpose.
  3663. 2:39:24Also, handling time zones was quite
  3664. 2:39:26complicated before, but now it is much
  3665. 2:39:28simpler with dedicated classes like zone
  3666. 2:39:31date, time, and zone ID. All right, now
  3667. 2:39:34that we understand why this new API was
  3668. 2:39:36introduced, let's start exploring it
  3669. 2:39:39step by step. So, this was the package
  3670. 2:39:41which was introduced in Java 8. As you
  3671. 2:39:44can see, I have already imported it.
  3672. 2:39:46I've imported all the classes from this
  3673. 2:39:48package. Okay, so let's start by looking
  3674. 2:39:51at some of the important classes that
  3675. 2:39:53this package contains. First, we have a
  3676. 2:39:56class named local date. This class
  3677. 2:39:59represents the date. Okay, but it does
  3678. 2:40:02not have the time component. All right,
  3679. 2:40:04so for time we have another class local
  3680. 2:40:06time. It only has the time component. It
  3681. 2:40:09does not contain any date. Okay, then we
  3682. 2:40:12have another class local date time.
  3683. 2:40:14Okay, so this contains both date as well
  3684. 2:40:17as time. Right? So let me show you how
  3685. 2:40:19we can create them. It's very simple. We
  3686. 2:40:22can just call a static method dot now.
  3687. 2:40:26Okay. So this will create the current
  3688. 2:40:28date. Similarly,
  3689. 2:40:30we can call this method on local time
  3690. 2:40:33and it will give me the current time and
  3691. 2:40:37this
  3692. 2:40:38local date time
  3693. 2:40:40also as now and it will give me the
  3694. 2:40:42current date time. Okay. So we have
  3695. 2:40:44these three classes and we can create
  3696. 2:40:47the objects of these using this method
  3697. 2:40:49named now. Okay. So let me just print
  3698. 2:40:52this
  3699. 2:40:55LD.
  3700. 2:40:56Okay. Let's just copy this.
  3701. 2:41:05All right. So if I run it.
  3702. 2:41:08Great. So you will see that the first
  3703. 2:41:10one contains the date contains only the
  3704. 2:41:12date. Then the local time has the time
  3705. 2:41:15component and in this local date time we
  3706. 2:41:18have both date as well as the time.
  3707. 2:41:22Okay, cool. Now there are other ways of
  3708. 2:41:26creating these dates. Okay, so here we
  3709. 2:41:28have only created the current date.
  3710. 2:41:29Similarly, we can create a date for
  3711. 2:41:32create a specific date as well. Okay,
  3712. 2:41:34let's say we want a specific date or a
  3713. 2:41:35specific time. So for that we have
  3714. 2:41:38another method which is local date
  3715. 2:41:43dot of off. Okay. So here we can specify
  3716. 2:41:46the year month and the day of the month.
  3717. 2:41:49Okay. So let's say the year is 2025. I
  3718. 2:41:52want the year as 2025, the month as 3
  3719. 2:41:55and the date as 12. All right. So I can
  3720. 2:41:59save it inside this variable. Okay. So
  3721. 2:42:02this is how I can create a date of I can
  3722. 2:42:04create some specific date. Okay.
  3723. 2:42:06Similarly, I can also create a specific
  3724. 2:42:09time. We have local time dot of off.
  3725. 2:42:13Now, here you can pass r, minute, second
  3726. 2:42:15and also nan of second. So, I'll use
  3727. 2:42:17this one r, minute and second. So, let's
  3728. 2:42:20say the r is 18, minute is 25 and second
  3729. 2:42:25is 50.
  3730. 2:42:27Okay, like this. Similarly for LDT we
  3731. 2:42:31have local time local date time
  3732. 2:42:36dot of off. Okay. So here here we can
  3733. 2:42:39specify all these things year month day
  3734. 2:42:42and then hour and minute and then second
  3735. 2:42:44as well in this method. Okay. So that's
  3736. 2:42:47what I'll do. Let's say 2023
  3737. 2:42:50and month is April. Then we have some
  3738. 2:42:53date let's say 13 and time is 12.
  3739. 2:42:5830 and 21 second. Okay. So this is how
  3740. 2:43:04using off method we can create a
  3741. 2:43:06specific date, a specific time and a
  3742. 2:43:08specific date time. Okay. Now if I print
  3743. 2:43:10these
  3744. 2:43:12great. So you'll see that the date
  3745. 2:43:14updated dates have been printed. Okay.
  3746. 2:43:16So this is 2025. Then we have this time
  3747. 2:43:191825. And here we have this date time
  3748. 2:43:222023 and 123021.
  3749. 2:43:26Okay. Now what else? I can also change
  3750. 2:43:29these dates. Okay, so we have some
  3751. 2:43:31methods like I'll show you LD. Plus okay
  3752. 2:43:37so you can see that I can add some days.
  3753. 2:43:40I could add months. I could add weeks or
  3754. 2:43:42I could add years. Okay, so let's say I
  3755. 2:43:44just add a few days. Okay, so in this
  3756. 2:43:47date, let's say I just want to in this
  3757. 2:43:49date I want to add a few days. Okay, so
  3758. 2:43:52I add three days. Similarly in LT we
  3759. 2:43:57have plus hours plus minute plus nanos
  3760. 2:44:00plus second. Okay. So we could add r
  3761. 2:44:02minute or nancond or second. Let's say I
  3762. 2:44:04add some minutes. Okay. So plus minutes
  3763. 2:44:07I add 12. Okay. And
  3764. 2:44:14similarly we also have LDT. And here we
  3765. 2:44:17have both the options. We could add
  3766. 2:44:19days, minutes, hours or we could just
  3767. 2:44:22add seconds, weeks, years. Okay. can
  3768. 2:44:24also we can minus as well. We can
  3769. 2:44:26subtract these things. Okay. So what
  3770. 2:44:28I'll do I'll just minus our year. Okay.
  3771. 2:44:30So minus years let's say three. I reduce
  3772. 2:44:343 years from it. Okay. So now if I run
  3773. 2:44:37it what exactly will happen? Let's see.
  3774. 2:44:41All right. So you will see that the
  3775. 2:44:44dates are actually same. Okay. This is
  3776. 2:44:462025 312 and all these are exactly same.
  3777. 2:44:51Okay. If you'll check it carefully. So
  3778. 2:44:53why didn't it work? Now see as I told
  3779. 2:44:56you in the beginning if you remember
  3780. 2:44:58that these classes are immutable okay so
  3781. 2:45:02what does immutable mean that they don't
  3782. 2:45:04change the same object. Okay like we
  3783. 2:45:07created this object here like local date
  3784. 2:45:09dot now. So the same object does not get
  3785. 2:45:12changed when we are calling this method
  3786. 2:45:13plus days another local date has been
  3787. 2:45:16returned. Okay. So if I'll just do it
  3788. 2:45:18like this that LD do plus date it will
  3789. 2:45:20not change the actual object. Okay,
  3790. 2:45:22instead a new object has been created
  3791. 2:45:24when I call this plus days. So the
  3792. 2:45:26change will be reflected only if I'll
  3793. 2:45:29change my reference variable to this new
  3794. 2:45:31object. Okay, so LD do plus days
  3795. 2:45:34actually returned a new object and this
  3796. 2:45:36reference object this this reference
  3797. 2:45:38variable that I created earlier only if
  3798. 2:45:41I explicitly change it to this object
  3799. 2:45:43then only the change will be reflected.
  3800. 2:45:45Okay, this is what immutability means
  3801. 2:45:48that the the existing object is there.
  3802. 2:45:51It's still in the memory but when we are
  3803. 2:45:53calling these methods a new object has
  3804. 2:45:55been returned. Okay. So now when I'll do
  3805. 2:45:58it like this. Okay. So all these methods
  3806. 2:46:00are returning a new object and we have
  3807. 2:46:03we have explicitly said that these
  3808. 2:46:04reference variables should now point
  3809. 2:46:06towards this new object. Okay. Now the
  3810. 2:46:09change will be reflected. All right. Let
  3811. 2:46:11me show you.
  3812. 2:46:13Great. So you can see that uh the day
  3813. 2:46:16was 12 but now the day is 15 because
  3814. 2:46:18three days have been added. Similarly,
  3815. 2:46:20here 12 minutes have been added. So now
  3816. 2:46:22the minutes are 37 instead of 25. And
  3817. 2:46:26here three years have been reduced. So
  3818. 2:46:28that's why the year is 2020.
  3819. 2:46:31All right. So this is how these methods
  3820. 2:46:34work. Now what else can be done? We can
  3821. 2:46:37also perform comparisons between these
  3822. 2:46:39dates. Okay. Let me show you. Let's say
  3823. 2:46:42I take this date LD and I can simply
  3824. 2:46:45call this method is after. Okay. And
  3825. 2:46:49here I can pass another date. Let's say
  3826. 2:46:51I pass local date dot now. Okay. So what
  3827. 2:46:56it is doing is that it is actually
  3828. 2:46:58comparing this date with this one. Okay.
  3829. 2:47:01So it is checking that whether this date
  3830. 2:47:03is after this date. Okay. So let me just
  3831. 2:47:06print the result. It will actually
  3832. 2:47:07return a boolean result. Okay.
  3833. 2:47:12Similarly we can do it for others.
  3834. 2:47:17Let's say I take LT. So in case of LT
  3835. 2:47:20you can see I cannot pass a local date
  3836. 2:47:22here now. Okay. We can compare local
  3837. 2:47:23time with local time only. That is why
  3838. 2:47:26let's say I take local time dot now.
  3839. 2:47:29Okay. And for LD let's say for LDT sorry
  3840. 2:47:34uh in this case also I will have to pass
  3841. 2:47:36a local date time. Okay. So I'll pass
  3842. 2:47:39local date time dot now. Also like is
  3843. 2:47:41after we have one more method we have
  3844. 2:47:43this method is before. Okay. So it
  3845. 2:47:46checks that whether this local date time
  3846. 2:47:48is before this local date time. Okay.
  3847. 2:47:52And uh what I'll do it let's just first
  3848. 2:47:55print these dates and then let's print
  3849. 2:47:57the comparisons and also I'll print the
  3850. 2:48:00local date as well. Okay. So that you
  3851. 2:48:02know what exactly is the local date and
  3852. 2:48:03time
  3853. 2:48:06date dot now.
  3854. 2:48:11Okay. So this is local time and local
  3855. 2:48:14date time. All right. So let me run
  3856. 2:48:18this.
  3857. 2:48:20Great. So you will see the output is
  3858. 2:48:23false then true and then true. Okay.
  3859. 2:48:25Why? Because in this case we have LD dot
  3860. 2:48:31is after local date dot now. Okay. So
  3861. 2:48:33what is LD? The local date is 2025 315.
  3862. 2:48:37Okay. and local date dot now is 2026.
  3863. 2:48:41Okay. So we are checking that whether
  3864. 2:48:43this date is after this local date which
  3865. 2:48:45it is not. Okay. Because 2025 is before
  3866. 2:48:482026. All right. Similarly for LT is
  3867. 2:48:51after here we are checking the LT is
  3868. 2:48:54183750
  3869. 2:48:56but the current time is actually 108.
  3870. 2:48:59Okay. So this is actually this is yes
  3871. 2:49:02this date is this time is after local
  3872. 2:49:04time dot now. Okay. That's why it
  3873. 2:49:06returned true. And in the last case for
  3874. 2:49:09LDT the current uh the the LDT is
  3875. 2:49:12actually 2020 and you will see that the
  3876. 2:49:15current date is 2026. So that is why
  3877. 2:49:18here we have checked that whether it is
  3878. 2:49:20before. So yes this local date time is
  3879. 2:49:22before this current date current date
  3880. 2:49:24time. Okay. So that's why this also
  3881. 2:49:26return true. Okay. So this is how we can
  3882. 2:49:28also make comparisons between dates.
  3883. 2:49:31Now there are two more important classes
  3884. 2:49:33in this package. Duration and period.
  3885. 2:49:37Duration represents a time based amount.
  3886. 2:49:40Whereas period represents a date based
  3887. 2:49:42amount. Duration is compatible with
  3888. 2:49:44local time as well as local date time.
  3889. 2:49:47Whereas period is only compatible with
  3890. 2:49:49local date. All right. Let's see it in
  3891. 2:49:52practical. So I have already taken this
  3892. 2:49:55example. I have created a start date,
  3893. 2:49:57end date, a start time, end time and
  3894. 2:50:00similarly a start date time and an end
  3895. 2:50:02date time. Okay. So now how can I create
  3896. 2:50:05duration? I'll simply write it like
  3897. 2:50:08this. Duration duration is equal to
  3898. 2:50:11duration dot between. Okay. So we have
  3899. 2:50:14this method between that we can call.
  3900. 2:50:16Here I will pass the start time and in
  3901. 2:50:19the second argument I'll pass the end
  3902. 2:50:20time. Okay. So now what exactly is
  3903. 2:50:23duration? Basically we are we we want to
  3904. 2:50:26calculate the time between this end time
  3905. 2:50:29and the start time. Okay. That is what
  3906. 2:50:31duration represents. Okay. So I'll just
  3907. 2:50:34simply print this system.print ln. What
  3908. 2:50:37I can do is I can just simply print
  3909. 2:50:40duration dot2.
  3910. 2:50:42Okay, I'll just show you duration.2.
  3911. 2:50:45We have these different methods. Okay,
  3912. 2:50:48so I want to know the time difference
  3913. 2:50:50between these two times. And this is
  3914. 2:50:52where I specify in which format I
  3915. 2:50:54actually want them to know. I wanted to
  3916. 2:50:56know. Okay, so let's say I I do 2 hours.
  3917. 2:51:00Okay. Now if I'll print it.
  3918. 2:51:03Okay. So it comes out to be 9. This
  3919. 2:51:06means that these two time have a
  3920. 2:51:08difference of 9 hours between them. And
  3921. 2:51:10as you can see that is the case. This is
  3922. 2:51:1218 and this is 9. So the difference is
  3923. 2:51:149. Okay. But I could have taken this
  3924. 2:51:17difference in the format of minutes as
  3925. 2:51:19well. Okay. And it says it gives me the
  3926. 2:51:22exact minutes which is 569. Okay. So the
  3927. 2:51:25difference between these two time is 569
  3928. 2:51:27minutes. Okay. Similarly, I could do it
  3929. 2:51:30for seconds as well. Okay, I'll just
  3930. 2:51:31show you that as well. Okay, we don't
  3931. 2:51:34have seconds. I guess we have nanos and
  3932. 2:51:36millies. All right, good. Okay, so this
  3933. 2:51:40is how we can create a duration. Now,
  3934. 2:51:43instead of start time, I can also take a
  3935. 2:51:45start date time and an end date time.
  3936. 2:51:48Okay, it is compatible with these as
  3937. 2:51:50well. Okay, so if I run it,
  3938. 2:51:54great. So, you see that it works fine.
  3939. 2:51:56Okay, so these are the dates. These are
  3940. 2:51:58the date times and uh this much is the
  3941. 2:52:01difference but in minutes. Okay. So I'm
  3942. 2:52:03not going to calculate it but yeah this
  3943. 2:52:05is the difference. I could have also
  3944. 2:52:07taken it in hours and yes in this case I
  3945. 2:52:10could take it in some other format as
  3946. 2:52:12well. I could have also taken it in
  3947. 2:52:14days. Okay. So now if I run it.
  3948. 2:52:17Okay. So you'll see that the difference
  3949. 2:52:18is 193 days. Okay. Between these two
  3950. 2:52:21start date time and end date time. All
  3951. 2:52:24right. Similarly we can create a period
  3952. 2:52:26object as well. Let's call it period.
  3953. 2:52:28Period is equal to period dot between.
  3954. 2:52:32Okay. Now here I can pass a start date
  3955. 2:52:35and then end date.
  3956. 2:52:37Okay. And similarly I can just print
  3957. 2:52:42system dot out dot print ln period dot
  3958. 2:52:48get days or get months or get years. So
  3959. 2:52:52this represents the difference in terms
  3960. 2:52:54of date. Okay. So here we don't have the
  3961. 2:52:56time component. So we are saying the
  3962. 2:52:57difference in term of days or months or
  3963. 2:53:00years. Okay. So that is the difference
  3964. 2:53:01between period and duration. Duration is
  3965. 2:53:04basically representing time and period
  3966. 2:53:07is representing days or dates. Okay. So
  3967. 2:53:10let's try to get days. And if I run it
  3968. 2:53:15so you'll see that the difference is 16
  3969. 2:53:16days. Okay. So between these two dates
  3970. 2:53:19the difference is 16 dates. If I would
  3971. 2:53:22have done get years,
  3972. 2:53:25okay, then there is no difference
  3973. 2:53:27because they are in the same year. Okay,
  3974. 2:53:29but let's say I change it to 2026.
  3975. 2:53:32Okay, now the year is one. All right, so
  3976. 2:53:35this is how period and duration works.
  3977. 2:53:38Now we have another very important class
  3978. 2:53:41inside this package zone date time. Zone
  3979. 2:53:44date time represents a date and a time
  3980. 2:53:47along with a time zone. It is used when
  3981. 2:53:50you want to handle date and time across
  3982. 2:53:52different time zones like scheduling
  3983. 2:53:55events globally or working with users in
  3984. 2:53:57different regions. Okay. So let's see.
  3985. 2:54:01So I can just simply use this class zone
  3986. 2:54:03date time. Let's say I want a default
  3987. 2:54:07zone and I can just simply call it like
  3988. 2:54:10this. Zone date time dot now. Okay. Let
  3989. 2:54:14me just print this. System.out.print
  3990. 2:54:17print. Okay, let me call it default.
  3991. 2:54:20Okay, default zone.
  3992. 2:54:24So you will see that it has the date
  3993. 2:54:27component, the time component as well as
  3994. 2:54:30the time zone. Okay, which in this case
  3995. 2:54:32is Asia/colkata
  3996. 2:54:34because the default zone for me is
  3997. 2:54:36Asia/colkata.
  3998. 2:54:38Okay, similarly, I can create a zone
  3999. 2:54:40date time of some particular zone. So I
  4000. 2:54:43can just call it like this. We have a
  4001. 2:54:45class named zone ID and here I can pass
  4002. 2:54:48the zone ID of some particular zone.
  4003. 2:54:51Okay, so let's say I want it for Europe.
  4004. 2:54:54Okay, I want the London time. So let me
  4005. 2:54:57just create this inside
  4006. 2:55:00some variable
  4007. 2:55:06London time. Okay,
  4008. 2:55:09great. So now this will represent the
  4009. 2:55:11time in Europe/ London. Okay. Similarly,
  4010. 2:55:16I can uh what I can do is I can just uh
  4011. 2:55:19uh change a time to a different time
  4012. 2:55:21zone. So, we have this method with
  4013. 2:55:25zone same instant. Okay. Here I can pass
  4014. 2:55:29zone ID dot of
  4015. 2:55:31let's say America/ new york. Okay. So, I
  4016. 2:55:34want to convert this London time into
  4017. 2:55:36this America time. Okay. So now I can
  4018. 2:55:39just take it inside some variable. Let's
  4019. 2:55:41say US time.
  4020. 2:55:46All right, let me just print this as
  4021. 2:55:49well.
  4022. 2:55:52print ln US time. Okay,
  4023. 2:55:57great. So you will see that this was the
  4024. 2:56:00India time then this is the London time
  4025. 2:56:02and now this London time was converted
  4026. 2:56:04into this US time. Okay, so this also we
  4027. 2:56:08can do using this new datetime API.
  4028. 2:56:12All right, so these were all the
  4029. 2:56:13important features which were introduced
  4030. 2:56:15in this new datetime API. I hope this
  4031. 2:56:18video was helpful. For more such
  4032. 2:56:20content, please follow my channel, The
  4033. 2:56:22Curious Coder.

About this transcript

This page contains the full transcript of Java 8 Crash Course in One Shot πŸ”₯ | From Basics to Advanced by The Curious Coder, generated from the public captions YouTube serves with the video. The transcript has 27,821 words across 4,033 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator β€” no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.