Java 8 Crash Course in One Shot π₯ | From Basics to Advanced β Transcript
Full transcript
- 0:00Hello and welcome to my channel, The
- 0:02Curious Coder. If you're preparing for
- 0:04Java interviews or want to finally
- 0:06understand Java 8 the right way, this
- 0:09course is for you. In the next 3 hours,
- 0:12we are not just going to learn concepts.
- 0:15We are going to code everything live
- 0:17with real world examples that actually
- 0:20make sense. We'll start with powerful
- 0:22upgrades to interfaces like default
- 0:25methods and static methods. Then move on
- 0:27to the core of Java 8 functional
- 0:29interfaces and lambda expressions. From
- 0:32there we'll break down the most
- 0:34important built-in functional interfaces
- 0:36predicate consumer function and
- 0:38supplier. You'll also learn method
- 0:41references then optional and then master
- 0:45the most asked topic in interviews
- 0:47streams API. And finally, we'll cover
- 0:50the modern date and time API so you can
- 0:52stop struggling with legacy classes once
- 0:54and for all. All right, so let's get
- 0:57started. So what is a default method?
- 1:00Default method is a method inside an
- 1:03interface that has some implementation.
- 1:06Before Java 8, we could only declare
- 1:09abstract methods inside an interface. We
- 1:11could not give them any body. But Java 8
- 1:14allowed us to have fully implemented
- 1:16methods inside an interface. Now these
- 1:19methods can be defined using the default
- 1:21keyword. Hence they are called default
- 1:24methods. All right. So let me show you
- 1:27some examples. So we have an interface
- 1:29here named my interface. And let's say I
- 1:32just simply declare one method. Okay.
- 1:36Like this. Now this is an abstract
- 1:38method because it does not have any
- 1:40implementation. All right. So this is
- 1:43how we used to declare abstract methods
- 1:45in an interface. But now if I try to
- 1:48give it some implementation, you will
- 1:50see that it gives me an error that says
- 1:53interface abstract methods cannot have a
- 1:55body. All right. So inside an interface
- 1:58we are not allowed to give body to a
- 2:00method. Okay. Now that was before Java
- 2:038. Now using Java 8, I can just simply
- 2:06add a keyword default. And now there is
- 2:09no compilation issue. All right. So
- 2:12using this default keyword I can declare
- 2:14the methods and I can also give them
- 2:16some body. Let's so I'll just add a
- 2:18statement here. Let's say
- 2:20system.out.print ln this is a default
- 2:26method.
- 2:27Okay something like that. All right and
- 2:31I'll just call it using some child
- 2:33class. So let me create some my child
- 2:37class
- 2:41and this implements the interface
- 2:45and if I create an object of this class
- 2:48my child class obj is equal to new my
- 2:53child class using this I can simply call
- 2:56this method. All right
- 3:00great. So this statement is printed. All
- 3:03right. So we have declared the method
- 3:05inside an interface only. I didn't give
- 3:08any implementation here in the child
- 3:09class and using this I can call the
- 3:12method. All right. Now why was default
- 3:15method introduced? What is the need for
- 3:17it? Default methods were introduced for
- 3:20backward compatibility. Before Java 8,
- 3:24if a new method was added to an
- 3:26interface, all the implementing classes
- 3:28were forced to implement it. Otherwise
- 3:31the code would fail to compile. But now
- 3:34with default methods we can provide an
- 3:36implementation directly inside the
- 3:38interface without breaking the existing
- 3:40code. If a child class wants to override
- 3:43the method and give its own
- 3:45implementation, it can do so. If not,
- 3:48the default method implementation
- 3:50defined in the interface will be used
- 3:52automatically.
- 3:53This means no changes are required in
- 3:55the existing implementing classes. All
- 3:58right. So let me show you. I have an
- 4:01interface named payment and it has this
- 4:04method void pay. All right. So this is
- 4:07an abstract method which has no
- 4:09implementation. Okay. So we have three
- 4:11child classes of this interface. Credit
- 4:13card, net banking and UPI. And in all
- 4:16these classes this method has been
- 4:18implemented. Okay. They have given their
- 4:21each implementation. Okay. In net
- 4:23banking similarly in UPI as well. Okay.
- 4:26So so far everything seems fine but
- 4:29let's say I want to add one more
- 4:32functionality. Let's say now now I want
- 4:34to add a refund functionality. Okay. So
- 4:36I just add another method void refund.
- 4:40Okay. So what happens is that it has
- 4:43created a certain problems. Okay. If
- 4:45I'll go inside this implementation you
- 4:47can see that there is a compilation
- 4:49error that says class credit card must
- 4:52either be declared abstract or implement
- 4:54the abstract method refund. Okay, so
- 4:56it's asking this this class to implement
- 5:00this method. Okay, this one the refund
- 5:03that we just added. Similarly, UPI will
- 5:06also have to implement it and net
- 5:08banking will also have to implement it.
- 5:11Okay, so first of all, why why do they
- 5:14have to implement this this method?
- 5:16Because let's say I just create an
- 5:18object of net banking and I try to call
- 5:20this method refund. So what exactly will
- 5:23be called? there is no implementation
- 5:25right now. Correct? So that is why
- 5:28compiler will not know what exactly to
- 5:30call. So that's why there is a need for
- 5:32some implementation. Okay. So now what
- 5:36what is the solution for this? I can
- 5:38simply just implement this method. Let's
- 5:40say I just implement refund. I can give
- 5:43some print statement. Similarly in net
- 5:46banking as well I can implement this
- 5:48method. All right. And in UPI also we'll
- 5:52just implement the method. Okay. So that
- 5:56is one way. But the problem is let's say
- 5:59there is an interface with hundreds of
- 6:01implementations and you just want to add
- 6:03some functionalities. So all the
- 6:05implementations will have to implement
- 6:07that method. Okay. So before adding that
- 6:10functionality you will have to give all
- 6:12that logic. Okay. So now that is not
- 6:15backward compatible and that is a bit
- 6:17tedious. So to solve this problem what
- 6:20Java did was let me first just remove
- 6:23this. Okay. We will not give
- 6:24implementation here.
- 6:27All right.
- 6:30Okay. So here we have not given in UPI
- 6:33and in net banking also I will remove
- 6:35it. Yeah. So instead of giving the
- 6:37implementations there what we can do
- 6:40simply we can make this method as
- 6:42default
- 6:44and we can provide our own
- 6:46implementation.
- 6:48Okay. So now you can see that there is
- 6:50no issue in any of these classes because
- 6:53now if we create an object of credit
- 6:55card and we try to call this payment uh
- 6:58this refund method. So this default
- 7:00implementation will be called. Okay. So
- 7:02as of now I've not written anything. I
- 7:04can write something as well. Let's say
- 7:06default
- 7:08refund flow executed.
- 7:12All right. Something like this. Okay. So
- 7:15this makes the code backward compatible
- 7:18and now we can just add any of the
- 7:20methods that we want to without breaking
- 7:22the existing code. Okay. If these
- 7:25classes want to give some some of their
- 7:28own implementation for refund, they are
- 7:30allowed to do that. They can just
- 7:31override this method and give their own
- 7:33implementation. If they don't want to,
- 7:35then there is no necessity for them to
- 7:37do it. Okay? The code will not break.
- 7:41All right? And also like I'll give one
- 7:43more example. Let's consider this list
- 7:45do on Java. Okay. So this is a class
- 7:48which is of this package java.util. So
- 7:51this class belongs to java itself. And
- 7:53actually they were also facing the same
- 7:55problem. They wanted to add some
- 7:57functionalities in this interface list.
- 7:59But you can see that list has so many
- 8:01implementations. There are a lot of
- 8:03implementations and Java is used
- 8:05worldwide. So everyone have to implement
- 8:07it. Okay. So that's why they introduce
- 8:11default methods. So I'll show you some
- 8:13examples like
- 8:17default
- 8:18here we have like we have this default
- 8:20method split iterator or we have this
- 8:24sort method okay and we have the replace
- 8:27all. So all these methods were added
- 8:30with a default keyword so that the
- 8:31existing code would not break. Okay. So
- 8:35the answer is that Java this default
- 8:37method was introduced this default
- 8:39keyword was introduced for backward
- 8:41compatibility.
- 8:43Now there are certain rules associated
- 8:45with default methods. So let's look at
- 8:48them one by one. First rule says that we
- 8:51can have multiple default methods inside
- 8:53an interface. Okay. So let's say I want
- 8:55to create one more default method. Let's
- 8:57say something is valid payment. Okay.
- 9:01Something like this. And I just want to
- 9:03give an implementation here as well. So
- 9:06I can also create a default I can use a
- 9:09default keyword and create method like
- 9:11this. Okay. Let's say I'm just simply
- 9:12returning true for now.
- 9:15Okay. So in the same interface we have
- 9:18multiple default methods. Now second
- 9:21rule says that these default methods can
- 9:24be overridden as well. Okay. So as I
- 9:27told you if these child classes want to
- 9:29give their own implementation they can
- 9:31do that. Okay. So I will just implement
- 9:34it. Let's say I want to implement
- 9:36refund. Okay. So
- 9:39here this class has its own
- 9:42implementation. It says CC refund flow
- 9:48executed.
- 9:50All right. Now in the main class if I
- 9:54just create an object of credit card
- 9:58let's say OPJ1 is equal to new credit
- 10:01card and using this object
- 10:04if I call refund okay and I create one
- 10:09more let's say for UPI
- 10:11opJ2 is equal to new UPI
- 10:15and using UPI also I will call okay
- 10:19using this object also
- 10:21I will call the refund method. Okay. So
- 10:24in credit card I have given some
- 10:26implementation. In UPI I have not
- 10:28implemented refund. Okay. I've not
- 10:30overridden it. All right. So if we run
- 10:32this.
- 10:34Great. So for this credit card the CC
- 10:37refund flow executed. But for UPI the
- 10:40default refund flow executed.
- 10:42All right. Now uh there is one more
- 10:45issue. There is actually an issue with
- 10:47this default keyword as well. Let me
- 10:50tell you about that. So it is called
- 10:52multiple inheritance problem or we also
- 10:55call it as a diamond problem. Okay. So
- 10:58I'll take one more example. Let's say I
- 11:00have a calculator
- 11:06interface.
- 11:07Here we have a simple method int
- 11:10operate. Okay. It takes two arguments
- 11:12int a comma int b. All right. So we have
- 11:18this calculator interface and let's say
- 11:20this is a default method. I simply give
- 11:23an implementation where I'm returning a
- 11:26+ b. Okay. So I'm returning the sum of
- 11:30these two values. And let's say there is
- 11:32one more interface
- 11:35calculator one.
- 11:38Let's say it's an interface.
- 11:41And here as well we create this default
- 11:44method operate. It also has the same
- 11:46name but the implementation is
- 11:49different. Okay. Here we are multiplying
- 11:51the two fields. All right. Now there is
- 11:54a child class
- 11:57right. Let's say child calculator or let
- 12:01me call it simple
- 12:03calculator impl calculator
- 12:06implementation.
- 12:07Okay. And this implements
- 12:11calculator as well as calculator one.
- 12:15Okay. So this child class implements
- 12:17both these calculators. So now there is
- 12:20an issue. There is a diamond problem.
- 12:23How? When I'll create an object of this
- 12:26class calculator impl. Now compiler will
- 12:29again won't know which method to be
- 12:31called. If I try to call this operate
- 12:33method, this implementation is
- 12:36implementing both the calculators. So
- 12:38the compiler will not know which operate
- 12:40to be called. Okay. So the rule says
- 12:43that whenever a child class is
- 12:45implementing multiple interfaces and
- 12:47those interfaces have the same method,
- 12:50method with the same name. So in that
- 12:52case it has to implement that default
- 12:54method. Okay. So now it's a necessity
- 12:56for calculator implement for calculator
- 12:59impl.
- 13:01And here we have to tell which one we
- 13:04want to actually call. Okay. So using
- 13:07calculator.sup super.operate we are
- 13:09saying we are actually telling compiler
- 13:12that the operate of this calculator
- 13:14interface has to be called. Okay. So let
- 13:16me show you in the main. Let me just
- 13:18remove all this.
- 13:20We have calculator implementation.
- 13:23C is equal to new calculator
- 13:25implementation. using this if I call the
- 13:28operate method 10, 20 and I simply run
- 13:32it.
- 13:34Okay, we have not printed this. So,
- 13:36system.out.print
- 13:37ln just print the result.
- 13:43Okay, so you will see that these two
- 13:44values have been added because we have
- 13:46written calculator.sup super.operate.
- 13:49So, this one will be called. Now,
- 13:51instead of this, if I write
- 13:54calculator 1.sup.operate operate and
- 13:56then I run it. So now it will be a
- 13:59multiplication because this method was
- 14:02called the pres the one present in
- 14:04interface calculator one. Okay. So this
- 14:07rule was that whenever multiple
- 14:08interface whenever a class is
- 14:10implementing multiple interfaces and
- 14:11those interfaces have a method with the
- 14:13same name then the child class will have
- 14:15to give some implementation of its own.
- 14:18Okay. Now let's consider one more
- 14:20scenario. Let's say this calculator one
- 14:23was not an interface. it was just a
- 14:25class. Okay. And I'll not create a
- 14:28default method here. This will be a
- 14:29normal public int operate. Okay. So this
- 14:32is a class calculator which has this
- 14:34operate method. And calculator is an
- 14:36interface which also has operate method
- 14:38but this one is a default one. Okay. So
- 14:41this calculator impl
- 14:45not an interface. So we will just extend
- 14:48calculator one and implement calculator
- 14:52interface. Okay. And let me remove this.
- 14:56All right. So now the child class is
- 14:59extending one class and it is
- 15:01implementing an interface. And both the
- 15:04interface as well as this class have the
- 15:07same method. Okay. Method with the same
- 15:09name but they have some different
- 15:11implementations.
- 15:12Okay. So now if I try to call this
- 15:15method operate using the implementations
- 15:17object. Okay. So what will be called
- 15:20whether it will be this interfaces
- 15:22method or will it be this classes method
- 15:26okay this one sorry calculator one this
- 15:28classes method or this interfaces method
- 15:31okay so let's see if I run my
- 15:33application
- 15:35great so you will see that
- 15:36multiplication has been performed which
- 15:39means that the method of this class
- 15:41calculator one was called okay so this
- 15:45is rule number four it says that
- 15:47whenever a child class is implementing
- 15:50one class is implementing one interface
- 15:52and extending another class. So the
- 15:55class will get precedence in case there
- 15:57are two methods with the same name.
- 15:59Okay. So this class will get precedence
- 16:02which in this case is calculator one and
- 16:05this interface will not get will be at
- 16:07the priority two. All right. Now there
- 16:11is one more rule a last rule that I want
- 16:13to tell you and mostly people miss this
- 16:15that we have a class object
- 16:19okay which is the parent class of all
- 16:22the classes all right and you can see it
- 16:24has some methods like it has a method
- 16:27equals okay I'll show you
- 16:34okay this is there is this method equals
- 16:36so let's say I just take this method
- 16:40And this is my interface here. Let's say
- 16:45I just try to override this method.
- 16:48Okay. I'll give some other
- 16:50implementation and I'll add a default
- 16:52keyword. Okay. Simply return false.
- 16:57All right. So this I cannot do. All
- 17:01right. In any other class I can override
- 17:04the method of object class because
- 17:06object class is the parent class of all
- 17:08the classes. So in any other class I can
- 17:10override that method and give my own
- 17:12implementation. But I cannot do that in
- 17:15an interface where I'm using the default
- 17:17keyword. Okay. So you can see that it
- 17:19gives me an error that says default
- 17:21method equal overrides a member of
- 17:23object. All right. So this is one more
- 17:25rule which you must keep in mind that
- 17:27you cannot override a method of object
- 17:29class and give the default keyword okay
- 17:32in this interface.
- 17:34All right. Hello and welcome to my
- 17:36channel the curious coder. Today I'll be
- 17:40talking about another very interesting
- 17:42feature that was introduced in Java 8.
- 17:44Static methods inside interfaces.
- 17:48Before Java 8 interfaces were very
- 17:50strict. We were not allowed to add
- 17:52static method inside an interface. But
- 17:54starting from Java 8, Java allowed us to
- 17:57do so. Now the question is why was this
- 18:01needed? Think about it. Sometimes we
- 18:04want a method that logically belongs
- 18:07only to the interface. We do not want
- 18:10child classes to override it and have
- 18:12their own implementation.
- 18:14It could be a helper method or some
- 18:16validation method. But the point is that
- 18:19we want that behavior to stay with the
- 18:21interface itself, not with the
- 18:23implementing class. That is exactly
- 18:26where static methods come into the
- 18:28picture. Let's move to Intelligj and
- 18:31I'll help you understand how this
- 18:33actually works. So I have taken an
- 18:36interface named payment. As of now it
- 18:39has two methods pay and refund. And I've
- 18:42taken three child classes UPI, credit
- 18:45card and net banking. And you can see
- 18:47that all these child classes have their
- 18:50own implementations for pay and refund.
- 18:53You can see that every child class have
- 18:55their own implementation.
- 18:57All right. Now let's say I want to add
- 19:00some validation inside this interface.
- 19:03I want to create a method boolean is
- 19:07valid amount. Okay. I'm validating the
- 19:09amount
- 19:11like this in amount. And my logic for
- 19:14validation is that if the amount is
- 19:18greater than zero then return true.
- 19:21Okay, it's a valid amount. Otherwise,
- 19:24simply return false.
- 19:27All right, so this is a logic for
- 19:29validation that I want to add inside my
- 19:31interface and I want this logic to be
- 19:35common for all the child classes. Okay.
- 19:38So I don't want any child class to
- 19:40override this method and give their own
- 19:43implementation because it's just a
- 19:45simple validation logic. So I want it
- 19:47common for all the classes. Okay. So how
- 19:51can I do that? Now before Java 8 this
- 19:53was not possible because we could only
- 19:56declare some abstract methods which the
- 19:58child classes can change. But after Java
- 20:018 we can do it using the static keyword.
- 20:05Okay. So this is how we can declare
- 20:07static methods inside our interface and
- 20:10these static methods belong only to the
- 20:13interface not to the child classes.
- 20:16Okay, these methods are not a part of
- 20:18child classes and I can show it to you.
- 20:21Let's say I create an object of credit
- 20:24card
- 20:25and using this object I can call the pay
- 20:28method. I can call the refund method.
- 20:32But if I try to call is valid amount
- 20:38this I'm not able to do because it says
- 20:41static method may only be called on its
- 20:43containing interface. All right let's
- 20:46say if I try to call it directly using
- 20:49the class name that also I won't be able
- 20:53to do. Okay. Again it will give me the
- 20:56same error.
- 20:58All right. So this proves that the
- 21:00static method belongs only to the
- 21:03interface. Now let's say I want to
- 21:06override this method. Okay, I want to
- 21:09override this is valid amount inside net
- 21:11banking and give a different
- 21:13implementation. All right, so let's try
- 21:15that. We have public boolean
- 21:20is default.
- 21:24Sorry, my bad. Is valid amount.
- 21:27Okay. and there is int amount.
- 21:32Okay. So let's say I just simply return
- 21:35true. All right. So you won't see any
- 21:38compilation issue here. But if you look
- 21:41at it carefully, this is not an
- 21:43implementation of is valid amount. This
- 21:45is not overriding because when we
- 21:48override a method like we did here, we
- 21:50get this sign that says that we have
- 21:52overridden a method of this interface
- 21:55payment. And here is the implementation.
- 21:58Okay. But in this case, what we simply
- 22:00did was we just created a separate
- 22:03method here. Okay. So this is a totally
- 22:05separate method that belongs to this
- 22:07class net banking. But this is not an
- 22:09implementation of this method. All
- 22:12right. So this is actually called method
- 22:14hiding. This is not method overwriting.
- 22:17All right. So so far we have seen that a
- 22:20static method belongs to the interface
- 22:22and a static method cannot be
- 22:24overridden. Okay. So I'll just simply
- 22:26remove it from the implementation. Okay.
- 22:29We have it here in net banking. All
- 22:31right. So this is just method hiding.
- 22:34Okay. Now there are certain other
- 22:37features about this static method. One
- 22:39is that we can create more than one
- 22:41static method inside an interface. So
- 22:44let's say I just simply create some
- 22:46method log time. Okay. Let's say I want
- 22:49to log the time. Okay. So we simply
- 22:52print term.out.print print len new date.
- 22:57All right, something like this.
- 23:02So that can also be done inside one
- 23:04interface. We can have multiple static
- 23:06methods
- 23:08also. One static method can call another
- 23:10static method. Okay. So let's say from
- 23:13this method I want to call the log time.
- 23:15That is also possible. Okay, there is no
- 23:17issue in that. But if I want to call a
- 23:21default method simply I just create
- 23:24another method a default method. Okay.
- 23:27And if I try to call it from a static
- 23:29method that actually I cannot do.
- 23:33Okay. So that is a characteristic of
- 23:36this static method that we it it does
- 23:39not allow us to call a default method
- 23:41from a static method.
- 23:44Okay. And there is one more thing the
- 23:46static methods that we create we always
- 23:50have to define a body for for those
- 23:52methods. We cannot leave them abstract.
- 23:54Okay. So if I just simply you know keep
- 23:56it like this I remove the implementation
- 23:59and I try to create it abstract that I
- 24:02cannot do and actually that makes sense
- 24:05because since no child class can
- 24:07override it and give its own
- 24:08implementation. So that is why it is
- 24:10necessary for us to provide the
- 24:12implementation here itself in the
- 24:14interface. Right? That is only logical.
- 24:17Okay. So always a static method has to
- 24:20be given some implementation.
- 24:22All right. Now one more thing if a
- 24:25static method cannot be implemented
- 24:27inside a child class and it cannot be
- 24:30even called by a child class then how
- 24:32are we going to invoke this method?
- 24:35Okay. Who will call this? So let's see.
- 24:37First let me give some implementation
- 24:39here. System.out.t print ln new date.
- 24:45Okay. In the main class we have already
- 24:49seen that have I when I created an
- 24:51object of credit card I was not able to
- 24:53call my method using that. Okay. So
- 24:56actually it's very simple. We just take
- 24:58the interface name and using this we can
- 25:00directly call our method. Okay. Like
- 25:04this. Let me just print it.
- 25:07John.out.print ln.
- 25:12All right.
- 25:16Great. So you will see that the static
- 25:18methods are invoked. Okay.
- 25:22Now this is a summary of all the
- 25:24features of static methods inside an
- 25:26interface. I have created it so that you
- 25:28can take a screenshot of it. It will be
- 25:31very helpful for you when you want to
- 25:32revise this topic. All right.
- 25:35Hello and welcome to my channel the
- 25:37curious coder. In this video we'll be
- 25:40learning about one of the most important
- 25:42feature that Java 8 introduced
- 25:45functional interface. What is a
- 25:48functional interface? It is an interface
- 25:50that contains exactly one abstract
- 25:53method which makes it suitable for
- 25:55lambda expressions. All right. So it's
- 25:58an interface that contains exactly one
- 26:00method which is abstract not more not
- 26:02less. Why? Because that makes it
- 26:06suitable for lambda expressions. Now,
- 26:08what are lambda expressions? That we'll
- 26:10understand just in a while. For now,
- 26:13let's try to create a functional
- 26:14interface. So, what I'm going to do is
- 26:18I'll just simply create an interface.
- 26:20Let's say my interface.
- 26:24Now, is this a function interface? No,
- 26:27not yet. Because as of now, it does not
- 26:30have any abstract method. But we need
- 26:32one abstract method. All right. So let
- 26:35me add a method. Let's say void method
- 26:38one.
- 26:39All right. So now is it a functional
- 26:41interface? Yes. Now it is because it has
- 26:44only one abstract method. Okay. So I
- 26:48hope you guys know what abstract methods
- 26:50are. Basically these are the methods
- 26:52that we just declare. They don't have
- 26:54any body. Okay. They don't have any body
- 26:56like this. So these are called abstract
- 26:58methods. All right. So this can be
- 27:01considered as a functional interface.
- 27:03Now let's say I add one more method word
- 27:06method two like this.
- 27:10So now is is this a functional
- 27:12interface? No. Again this is not a
- 27:14functional interface because it has two
- 27:17abstract methods and we only need one.
- 27:19Okay. But let's say I make it a default
- 27:23method like this.
- 27:26All right. So default methods have body
- 27:28inside them. Okay. We can add curly
- 27:30braces and give any body if you want. So
- 27:32now is this a functional interface? Yes,
- 27:35it is still a functional interface
- 27:37because the only rule is that there
- 27:39should be one abstract method. But we
- 27:41can have as many default methods as we
- 27:43want. Okay. Similarly, we can have
- 27:46static methods inside a functional
- 27:48interface. Okay. So let's say we have a
- 27:50method three like this. So still this
- 27:54can be considered as a functional
- 27:56interface. All right. Also there is one
- 27:59more thing. It is recommended that we
- 28:01should put this annotation at the rate
- 28:03functional interface on a functional
- 28:05interface. Okay. So what is the role of
- 28:08this? It basically restricts the
- 28:11interface to behave as a functional
- 28:13interface. All right. What do I mean by
- 28:15that? Let's say now if I add one more
- 28:18method. Okay. Like this void method 4.
- 28:25So I have added one more abstract
- 28:26method. So you'll see that it is giving
- 28:28me some compilation issue. It says
- 28:31multiple non-overriding abstract methods
- 28:33found in my interface. All right. So
- 28:35this means that this annotation has
- 28:38added a restriction on this interface
- 28:40that it should not break the rules of
- 28:42functional interface. And as I told you
- 28:44functional interface can only have one
- 28:46abstract method. So that's why it's
- 28:48giving me a compilation issue because
- 28:50here we have created two. Okay. So I'll
- 28:52just remove this and now it works fine.
- 28:55Similarly, if I'll just comment this one
- 28:57again, it will give me any it will give
- 28:59me error that says no target method
- 29:01found because we still need one method.
- 29:04Okay. So, this is what functional
- 29:06interface is. All right. These are all
- 29:08the rules and this is how we can create
- 29:09any functional interface. Okay. Now,
- 29:12what's the purpose of it? Why does it
- 29:14have this rule that it should have only
- 29:16one method and uh what exactly we are
- 29:19going to do with this interface? So
- 29:21actually the main purpose behind this is
- 29:23that we can we can give an
- 29:25implementation of this interface using
- 29:27lambda expressions.
- 29:29All right. So now let's understand what
- 29:31lambda expressions are. Lambda
- 29:33expressions are a concise way to
- 29:36represent an implementation of a
- 29:37function interface using a short
- 29:39readable syntax. Basically they are
- 29:42designed to reduce a lot of boilerplate
- 29:44code. All right. So they implement the
- 29:47functional interface and in a very short
- 29:50manner okay using very less syntax. All
- 29:52right. So let's see how how they do
- 29:54that. So I have created an interface
- 29:57named ve and I have declared it as a
- 30:00functional interface and it has this
- 30:02abstract method start. Okay. Now let's
- 30:06say I want to call this method. Okay. So
- 30:09can I just create an object of vehicle
- 30:12like this and using this can I call it?
- 30:16Okay, we have vehicle v1 is equal to new
- 30:19vehicle and using this I want to call
- 30:21this method. Okay, so it gives me some
- 30:24compilation issue that says vehicle is
- 30:27abstract cannot be instantiated. Okay,
- 30:30so there is a problem with this and the
- 30:32problem is that we are actually not
- 30:34allowed to create objects of interfaces.
- 30:37Okay. Now why why can't we do that?
- 30:40Because see logically this start method
- 30:43does not have any implementation. There
- 30:46is no body of the start method. So if
- 30:48you'll create an object of vehicle and
- 30:50if you are going to call this method
- 30:52then what exactly should be called
- 30:54right? Because we don't have any body we
- 30:57don't have any implementation. Okay. So
- 30:59the compiler won't know what exactly to
- 31:02trigger. Okay. So what's the solution?
- 31:06So the solution is that we can create a
- 31:08child class to implement this vehicle.
- 31:11Okay. So let's say I have a class named
- 31:13car that implements vehicle. Okay. And
- 31:17here we will have to implement this
- 31:19method. Let's so this car class will
- 31:22override this method vehicle and give
- 31:24some implementation. Okay. So let's say
- 31:26I'll say system.out.print
- 31:29I will say car engine started.
- 31:32All right. So now we have an
- 31:34implementation for this method. Okay.
- 31:38But still if I'm trying to call it using
- 31:40the vehicle object, it still gives me an
- 31:42error. Now what's the problem? So the
- 31:45problem is that even if we create an
- 31:48implementation, but still we cannot call
- 31:50it using the vehicles object because the
- 31:53thing is that what if I create more
- 31:55implementations. Okay, let's say I have
- 31:57another class bike which also implements
- 31:59vehicle. Okay. And here
- 32:03let's say I give a different
- 32:05implementation. Okay. I'll just say bike
- 32:09engine started.
- 32:12All right. So now how would the compiler
- 32:15know that when we are calling it using a
- 32:17vehicle object, which of these two
- 32:19implementations should the compiler
- 32:21call? Okay. We have not specified that.
- 32:23That is why we cannot create an object
- 32:25like this. Okay. So what was the
- 32:27solution? solution was that we should
- 32:30create an object directly of the child
- 32:32class. Okay. So the child class one of
- 32:34the child class is car. So I'll create
- 32:36an object of car and put it in a
- 32:38reference of vehicle and using this I
- 32:40can call the start method. Similarly I
- 32:43can also do the same for bike. Okay I
- 32:47can create an object of bike and using
- 32:49this I can also call the start method.
- 32:52Okay. So if I run it,
- 32:56great. You will see that this printed
- 32:58car engine started and this one printed
- 33:00bike engine started. All right. Now this
- 33:03is how things were done before Java 8.
- 33:06Now you might observe that whatever we
- 33:08did so far is actually containing a lot
- 33:11of redundant code. Okay, there is a lot
- 33:13of boilerplate code that we actually
- 33:15don't need. Why? Because you see we have
- 33:18this start method and at the end of the
- 33:21day we only want to implement it but
- 33:23just to do that we are creating two
- 33:25child classes and then we are calling
- 33:28methods using those objects. Okay. So I
- 33:31think there is a scope of reducing this
- 33:33code. All right. So what can I do? I can
- 33:37just I let me just first comment this
- 33:39out. Okay. And one second I'll just uh
- 33:42copy paste this. I'll copy this paste it
- 33:45over here. Okay. Okay. So I'll comment
- 33:47this part. This was one way of doing
- 33:49things. Now we'll look at the second
- 33:51method. Okay. So at the end of the day
- 33:54all I need is I need some implementation
- 33:56here. Okay. So instead of giving it like
- 33:58this like writing it like this in new
- 34:01car, I'll just simply write new vehicle.
- 34:04Okay. And if somehow I give I provide an
- 34:07implementation here itself instead of
- 34:10creating this class, then my job is
- 34:12done. Okay. So how can I do that? I'll
- 34:15just simply click here implement methods
- 34:17and this is how I will provide an
- 34:19implementation here itself. Okay, this
- 34:23is called anonymous inner class. All
- 34:25right, so we didn't need a separate car
- 34:28class. We can provide an anonymous
- 34:30implementation here itself. And here I
- 34:33can just simply say system.out.trint
- 34:35ln. Let's say I want to give an
- 34:38implementation of car only. So I say car
- 34:40engine started. Okay. So now I won't be
- 34:42needing this class car. Okay. Okay, I
- 34:44can delete it. Okay. And using this
- 34:46v_sub1 now I can call v1 dot start. All
- 34:50right. So we didn't create an object of
- 34:52class car. We created an object of
- 34:54vehicle only. The only thing which was
- 34:56missing is the implementation that we
- 34:59provided here itself. All right.
- 35:01Similarly, I'll do the same for bike. I
- 35:04can just say ve and here I will
- 35:07implement. Okay. So system.out.print
- 35:10print ln let's say bike engine
- 35:15started
- 35:16all right so v1 dot start v2 dot start
- 35:20let me just call this
- 35:23great so again using anonymous inner
- 35:25classes we can implement these we can
- 35:27implement this method okay and now I'll
- 35:29also delete the class bike
- 35:32okay so now lambda what lambda
- 35:35expressions do is that they they are
- 35:37actually one step further okay they
- 35:39reduce this code even more. That is what
- 35:41lambda expression is. It is nothing
- 35:43fancy. It is just a way to give
- 35:46implementation of a method. All right?
- 35:48Inside an inside a functional interface.
- 35:51Okay? So let me just copy this also and
- 35:54I'll comment this part. This is so this
- 35:57was the first way. This is the second
- 35:58way of doing things. Now I'll show you
- 36:01the third way. Okay. So see the catch
- 36:04here is that this vehicle interface
- 36:07contains only one abstract method.
- 36:09because it's a functional interface. All
- 36:11right, that we know and actually that
- 36:13compiler also knows. All right, so that
- 36:16is why since it's only one method, so do
- 36:18we really need to provide all this name
- 36:20over here that uh we are implementing
- 36:23this method start. We don't need to tell
- 36:25the compiler again. It already knows
- 36:27that this interface has method start. So
- 36:30whichever implementation I'm giving here
- 36:32is of will obviously of the method
- 36:34start. Okay, so I don't need this code.
- 36:38So I won't be needing this. And actually
- 36:41uh I already know that it's a vehicle
- 36:42reference. So I don't even need to write
- 36:44this. Okay. So I'll just remove this as
- 36:46well. All right. That's it. This is the
- 36:49implementation. And when I'm giving it
- 36:52like this, I have to add a hyphen and
- 36:54then an arrow. This is what lambda
- 36:56represents. This is called lambda. And
- 36:58this is my final syntax. All right. Very
- 37:02very simple. It reduces the code to a
- 37:05very very large extent. you'll see that
- 37:08we have given these parenthesis then a
- 37:10lambda and here I'm giving the
- 37:12implementation so this actually
- 37:14represents this only okay the way we
- 37:17gave an implementation here similarly I
- 37:20can write it in a short way like this
- 37:22and this is what called a lambda
- 37:23expression and you will see that in this
- 37:26method implementation we are only
- 37:28writing one statement a single statement
- 37:31so in that case I don't even need these
- 37:33inside braces okay so I'll just remove
- 37:36this and I'll remove one semicolon and
- 37:38I'll remove this. All right. So this is
- 37:41how I can implement using lambda.
- 37:43Similarly, I'll do it again. I'll show
- 37:45it to you. First, we remove this method
- 37:47name and override. We remove this
- 37:49parenthesis. We don't need to tell the
- 37:52compiler that it's a vehicle object
- 37:53because we have the vehicle reference.
- 37:55Okay. So, we remove this here. I will
- 37:57add a hyphen then an arrow. And this is
- 37:59the implementation. Now, this
- 38:01implementation has only one line. So, I
- 38:03don't even need this. and I don't need
- 38:06this and I don't need this. Okay,
- 38:09simple. This is how I will give my
- 38:13implementation. All right, so you will
- 38:15see that we have done all this only in
- 38:18just a single line. Okay, now again I
- 38:21want to tell you what it actually
- 38:22represents. This line here represents
- 38:25the implementation of the start method.
- 38:28Okay. So, whatever the start method,
- 38:30whatever implementation, whatever body
- 38:32you want to provide to the start method,
- 38:33that is written here after the lambda.
- 38:36Okay. Similarly, here in V2 and I'll run
- 38:39it again.
- 38:41Great. So, again, car engine started and
- 38:43bike engine started. Okay. So, the first
- 38:46way is that you create a child class and
- 38:48using the object of that class you call
- 38:50the method. The second way is that you
- 38:52provide an implementation using an
- 38:54anonymous inner class. And the third way
- 38:56is that you do it using a lambda
- 38:59function. Okay, lambda expression. So
- 39:01this is just a different type of syntax,
- 39:03nothing else. It's very short and it
- 39:06only works because vehicle has one
- 39:08abstract method. All right? If it would
- 39:11have had multiple abstract methods, then
- 39:13we could not do it like this because
- 39:15then the compiler won't know that which
- 39:17method we are talking about because then
- 39:19we will have to name the method like we
- 39:21named it here. Okay. But since there is
- 39:23only one abstract method. So that's why
- 39:24we can give an implementation like this.
- 39:27All right. Now I think that this topic
- 39:30is a little hard to understand
- 39:32especially in the beginning. So to
- 39:34understand it there is only one way that
- 39:36we should practice it a lot. And that's
- 39:38why in my video I'll be taking multiple
- 39:41examples. I have created four different
- 39:43functional interfaces and one by one
- 39:45I'll be taking I'll be implementing all
- 39:47these interfaces. But still my
- 39:49recommendation is that you should
- 39:51practice it on your own as well. Okay,
- 39:54create some functional interfaces, do
- 39:56some implementations of it, read some
- 39:58examples and eventually you'll get a
- 40:00hold of it. All right, but in my video
- 40:03as well, I'll cover a lot of examples
- 40:05and I hope that it will be very helpful.
- 40:08Okay, so this is the first interface.
- 40:10This is an interface named greeting and
- 40:13it has this abstract method. Okay. So
- 40:15I'll implement it the same way I've
- 40:17implemented the vehicle interface. Okay.
- 40:19Now the trick is that always first write
- 40:23the interface name. All right. Because
- 40:26see every time you won't do it like
- 40:27this. You won't be creating a child
- 40:29class first. Then you will be creating
- 40:31an anonymous class and then breaking
- 40:33down that code. Okay? You should
- 40:35directly write a lambda function. Okay?
- 40:37At some point you should know how to
- 40:39write it directly. Okay? So I'll just
- 40:42tell you the trick for that. So first
- 40:44write the interface name then give your
- 40:46object some name let's say I say G1
- 40:50and after that just check how many
- 40:52arguments does this method have so as of
- 40:55now it does not have any argument so
- 40:57simply just add a parenthesis add the
- 41:00lambda the hyphen and an arrow and
- 41:02always keep make it an habit always give
- 41:06these curly braces in the beginning okay
- 41:08always first give this curly braces and
- 41:10a semicolon write it like this now This
- 41:13is the area where you're going to give
- 41:15your implementation. Okay. This is where
- 41:18this method greet will be implemented.
- 41:21All right. So let's say I simply write a
- 41:24statement system.out.print
- 41:27hello everyone. Okay. A simple print
- 41:31statement and that's it. You have given
- 41:33an implementation for the greet method.
- 41:36Now after that check that whether this
- 41:39implementation is a single line
- 41:40implementation. Yes. In this case it is
- 41:43a single line. We are only printing
- 41:44something. So in that case you can
- 41:46remove this. You can remove this and you
- 41:49can remove this semicolon. Okay. So this
- 41:52is the whole implementation.
- 41:55All right. Now using this G1 you can
- 41:57call the grid method. All right. Now
- 42:01similarly let's take one more example.
- 42:03We have an interface named task and the
- 42:06method is execute. Okay. So this time
- 42:10again I'll take task let's say I name
- 42:14the object t1 is equal to in this task
- 42:18again we don't have any argument. So
- 42:20I'll simply take a parenthesis then a
- 42:23lambda and again initially I will add
- 42:25these curly braces and a semicolon. Now
- 42:28let's say I want to write two statements
- 42:30here. Okay. first is that task started
- 42:35then
- 42:36something like let's say task ended okay
- 42:39simple
- 42:41so in this case I cannot remove these
- 42:43braces okay if I want both these
- 42:46statements to be a part of the
- 42:48implementation of this execute then I
- 42:50will have to write both these statements
- 42:52inside the curly braces all right that's
- 42:55the rule so this is how you implement a
- 42:58multi-line implementation you give a
- 43:00multi-line implementation But in case of
- 43:01a single line you can remove these
- 43:03braces as well as this semicolon. Okay.
- 43:07So again using this T1 I can call
- 43:09T1.execute.
- 43:11All right. Cool. Now coming to the third
- 43:14example. This is an interface square.
- 43:16And let me just also add this
- 43:18annotation. Okay. Even though if I
- 43:21didn't add it, it would have worked
- 43:22because there is only a single method.
- 43:24But I still still it's recommended. So I
- 43:26like to add it. All right. So we have
- 43:28this interface. It has this method
- 43:30operate. Now this time the return type
- 43:32is int and we are also taking an
- 43:35argument. All right. Now that is an
- 43:37interesting example. So first always
- 43:40write the interface name. Let's say
- 43:42square s1 is equal to this time we have
- 43:47an argument. Okay we have an argument.
- 43:50So here I will take an argument. All
- 43:52right, like this number and I'll write
- 43:56the lambda and then the curly braces.
- 43:59Okay, so we will have to take an
- 44:01argument because here in the method uh
- 44:04in the method square we have this
- 44:05argument. Okay, now this number using
- 44:08this number I'm saying return
- 44:12number
- 44:13x number. Okay, I'm returning the square
- 44:16and put a semicolon here as well. All
- 44:18right. So now this is the
- 44:20implementation. Okay. So is there any
- 44:24scope of reducing this further? Actually
- 44:26yes there is. First let me just clarify
- 44:28one more point. I'll reduce it as well.
- 44:30But let me just clarify one more point.
- 44:32So the theme the thing is when you see
- 44:35the code like this many people get
- 44:37confused. But the thing is that since we
- 44:39are returning something here let's say
- 44:41the number is 10. Okay. So we are
- 44:43returning something here. So people
- 44:45think that uh the value 100 that will be
- 44:47returned and will be saved in this
- 44:49object. But that is not true. The return
- 44:53type the returning thing is of this
- 44:55method which is this square which is
- 44:57this operate. Okay. So when you are
- 45:00actually calling the operate method at
- 45:02that point of time this code will be
- 45:04triggered. Okay. So whatever you are
- 45:06returning here that is not getting
- 45:07returned here in S1 that will be
- 45:09returned here. I'll just print it.
- 45:12System.out.print print ln.
- 45:14Okay. So when you actually call the
- 45:16method at that point of time, this
- 45:19implementation will be called. All
- 45:21right. This is how it is working.
- 45:23Imagine it like this. At this point, you
- 45:25have only given the implementation. You
- 45:27have not actually triggered any code
- 45:29because the method is actually getting
- 45:31hit at this point of time when you are
- 45:33actually calling the operate method. All
- 45:35right? So just please think of it like
- 45:37this. I wanted to make it clear. Okay.
- 45:40Anyways, so in the operate method here I
- 45:43will pass the value which is 10. This
- 45:46value this 10 will be passed on here and
- 45:49it will actually return 10 into 10. So
- 45:52100. Okay. So this will be returned here
- 45:54in the print statement. All right. Now
- 45:57okay can I further optimize it? Yes.
- 46:00How? First this has this also has only
- 46:02one line. Correct. So I can remove this.
- 46:06I can remove this and I can remove this.
- 46:09All right. Now there is one rule. If
- 46:11inside this single line there is a
- 46:13return statement and you remove the
- 46:15curly braces then you also have to
- 46:17remove this return. Okay, compiler
- 46:20already knows that you are returning
- 46:21something. So the implementation will be
- 46:23like this. There is a number and you are
- 46:25returning a square of number. Now this
- 46:27is just an implementation. This will
- 46:29actually be triggered when you'll call
- 46:30the operate method which is here. Okay.
- 46:34And there is one more optimization. If
- 46:36there is a single parameter in that case
- 46:38you're also allowed to remove these
- 46:40parenthesis. Okay. So please remember
- 46:43that if there is a single parameter you
- 46:45can also remove this parenthesis. So we
- 46:47will just write number and return number
- 46:49into number. All right great. Let me
- 46:53just run it so far so that you can see.
- 46:57Okay. So when G1.Greet was called this
- 47:00statement was triggered and hello
- 47:02everyone was printed. When T1.execute
- 47:04execute was called these two statements
- 47:07were triggered and task started task
- 47:08ended was printed. Similarly when
- 47:11s1operate was called then only this
- 47:14square was returned in this from this
- 47:16operate method and whatever value was
- 47:18returned we are printing it. That's why
- 47:20100 was printed. Okay.
- 47:23Now let me take one more example. This
- 47:26time we have two arguments. Okay. And we
- 47:28are returning some int value. All right.
- 47:31So I'll take this example. I'll write
- 47:35the interface name calculator. Give
- 47:37object some name. Let's say C1 is equal
- 47:39to since there are two arguments. So I
- 47:42will have to take it like this. Let's
- 47:43say a comma b. Okay. You can name
- 47:46anything. You can name the argument
- 47:47anything. Like here it's num one and num
- 47:50two. But here you can say a comma b as
- 47:53well. There is no issue in that. All
- 47:55right. Now an hyphen and an arrow
- 47:59representing lambda. then this
- 48:01parenthesis and this semicolon. Okay, so
- 48:04I could just directly return a + b but
- 48:07let's say I'm taking it like this int
- 48:10result is equal to a + b and I'm also
- 48:14printing it system.out.tprint ln let's
- 48:17say result
- 48:20okay which is this result
- 48:25and then I'm also returning this result
- 48:29like this. So this implementation is not
- 48:32a single line implementation. That's why
- 48:35I should not remove these curly braces.
- 48:37And has this code been called yet? No.
- 48:41Because as of now we have only provided
- 48:43the implementation which is there in the
- 48:44C1. But when I'll call it using C1 C1.
- 48:49Let's say 10, 20. At this point of time,
- 48:52this code will be triggered. Okay. And
- 48:54since I have this print statement here
- 48:56only, so I will not print it again. Let
- 48:58me just show you.
- 49:00Great. So this statement got triggered.
- 49:03Result is 30. Okay. The sum of A and B.
- 49:06So you passed 10 and 20 in this add
- 49:09method. So these were 10 was equal to A.
- 49:1220 was equal to B. These were saved here
- 49:14in the result and then it was printed
- 49:16here and also returned back. So where is
- 49:19it returned? It will be returned here.
- 49:21Okay. I can add it here as well. Int
- 49:23final result something like this is
- 49:26equal to C1. If I'll print this, this
- 49:29will also be equal to 30. All right.
- 49:32Hello and welcome to my channel, The
- 49:34Curious Coder. This video is part of my
- 49:37Java 8 series where we are exploring the
- 49:39most important Java 8 concepts that
- 49:42every developer should know, especially
- 49:44for interviews and real world
- 49:46development. In today's video, we are
- 49:48going to understand four very important
- 49:50functional interfaces. Predicate,
- 49:53consumer, function, and supplier. These
- 49:56interfaces are heavily used with lambda
- 49:59expressions and streams API and once you
- 50:02understand them properly a lot of modern
- 50:04Java code will start making much more
- 50:06sense. Let's start with predicate.
- 50:09Predicate is a predefined functional
- 50:11interface that takes one argument and
- 50:14returns a boolean value. It is mainly
- 50:16used for conditional testing. All right.
- 50:20So this is the interface predicate. As
- 50:22you can see it is a functional
- 50:23interface. It is predefined inside this
- 50:26package and it contains this abstract
- 50:29method named test. All right. So this
- 50:33test takes only one argument and it
- 50:36returns a boolean value. Okay. And this
- 50:39argument can be of any data type.
- 50:41Whatever is passed here in the generics.
- 50:44All right. So we have this flexibility
- 50:46that we can pass any data type inside
- 50:48this generics and the argument will be
- 50:50of the same data type. Okay. So let's
- 50:53see how we can implement it. The rules
- 50:55are same. First we'll write the
- 50:56interface name like this. And let's say
- 50:59in the generics I want my argument to be
- 51:02of the type integer. Okay. I'll give it
- 51:05some name. Let's say P1 is equal to. Now
- 51:08since this method takes one argument. So
- 51:12here also I will have to take one
- 51:14argument. Okay. So I can name it
- 51:15anything. Let's say I name it num
- 51:17representing some number. All right.
- 51:20After that I'll put an arrow and you'll
- 51:23see that IntelligJ has already shown
- 51:25that it is of the type integer because
- 51:27here in the generics we passed it as
- 51:28integer and whatever we pass here will
- 51:31be the data type of the argument. Okay.
- 51:34And here in the parenthesis this is the
- 51:35argument that we are taking. All right.
- 51:38So this is the number and let's say I
- 51:42want to give some first let me add these
- 51:44braces and semicolon like this and here
- 51:47I will give the implementation of this
- 51:49method test. Okay, I'll give some
- 51:51implementation. So let's say I want to
- 51:54check whether this number whatever we
- 51:55are passing is even or not. Okay. So I
- 51:58can simply give an give a condition if
- 52:01num
- 52:02mod 2 okay like this is equal to zero
- 52:06then we should
- 52:09then we should return true
- 52:12all right
- 52:14else we can
- 52:17return false.
- 52:20Okay so this is the implementation of
- 52:23this method test. Okay, it is taking one
- 52:27argument number and we are saying if the
- 52:30number is even then return true
- 52:31otherwise return false. Okay, so we are
- 52:33checking some condition that whether a
- 52:35number is even or not. Okay, now this is
- 52:38just an implementation. This method will
- 52:40actually be called when I'll call it
- 52:42like this P1.est and here I'll actually
- 52:46pass some value inside the argument. So
- 52:48let's say I'm testing 10 and I'm testing
- 52:52seven. Okay, P1.est 7.
- 52:56All right. So, let me also print the
- 52:58result. System.out.print.
- 53:04Similarly, here as well.
- 53:12All right. Okay. Yeah. So, if I run my
- 53:15code,
- 53:18great. You'll see that this returns true
- 53:20because 10 was an even number and se
- 53:22this returns false. Okay, so this is how
- 53:25we can implement predicate interface. We
- 53:28can give an implementation for this
- 53:29method test and uh it will just return
- 53:32it will take some argument and it will
- 53:34return some boolean value. Okay. Now
- 53:37I'll take one more example. This time
- 53:39I'll take some string argument predicate
- 53:42string and let me call it P2 is equal to
- 53:47this time also I have to take one
- 53:48argument. Let's say I'm calling it some
- 53:50str
- 53:52then this arrow and these parentheses
- 53:55these curly braces here I will provide
- 53:57the implementation. So I'm trying to say
- 53:59if string is empty if the string is
- 54:03empty then return true. Okay. Else
- 54:09return false.
- 54:12Okay. This is the implementation. So
- 54:15here in this condition I'm checking
- 54:17whether or not the string is empty.
- 54:20Okay. Now using this P2 as well I will
- 54:22call the method P2 dot test and inside
- 54:26this test I can pass an empty string and
- 54:29also I can pass some not empty string.
- 54:32Okay let's say my name. All right
- 54:36I'll simply print this system.out
- 54:39dot print
- 54:42test.
- 54:43Similarly, system.out.print
- 54:46ln
- 54:48this one.
- 54:50Okay. So if I run my application,
- 54:54great. We got true false then true
- 54:56false. So true for this, false for this.
- 54:59Now since it's an empty string, so it
- 55:01returns true and then it returns false.
- 55:04All right. So this is how predicate
- 55:06works. Now I also want to tell you that
- 55:08we can optimize this a little bit more.
- 55:11Like we have written a lot of lines
- 55:12here. We could have simply written it
- 55:14like this.
- 55:17Return str is empty. Okay, we could have
- 55:21directly written this. Similarly, here
- 55:23as well, I could have return
- 55:27num
- 55:30mode 2 is equal to zero. Okay, I can
- 55:32directly write it like this. Now there
- 55:34is one thing in this uh functional
- 55:37interface and lambda expression that
- 55:39when the implementation you are giving
- 55:41is of a single line like here you can
- 55:43see it's just a single line in that case
- 55:46you can remove this you can remove this
- 55:48and you can remove this semicolon as
- 55:50well okay it will be simply written like
- 55:52this and you can also remove this return
- 55:55keyword okay so actually you'll write it
- 55:58simply like this so you're saying num
- 56:00and then arrow and then num mode 2 equal
- 56:02to equal to0 we'll directly give the
- 56:04condition. Similarly, here as well, I
- 56:06will remove this and remove this. You
- 56:09can return statement as well. So, I'm
- 56:11able to do this because the statement is
- 56:13of single line. Okay? We are not doing
- 56:15anything else. All right? So, this is
- 56:18all about predicate.
- 56:20Now, let's understand function. Function
- 56:23is a predefined function interface that
- 56:25takes one input and returns some output.
- 56:29It is basically used for transforming
- 56:31the data. All right. So this is the
- 56:33interface function. As you can see it is
- 56:36also a function interface and it has
- 56:39this abstract method named apply. Okay.
- 56:42Now here as well we can take one
- 56:44argument but unlike predicate the return
- 56:47type is not fixed as boolean. Okay.
- 56:50Instead we can take any return type and
- 56:52we can pass it here in the generics. All
- 56:54right. So it has the flexibility of
- 56:56passing the data type for the argument
- 56:58as well as for the return type. Okay. So
- 57:02let's see some examples.
- 57:05I'll just write the interface name. Now
- 57:08let's say I have some string value and I
- 57:11want to return the length of that
- 57:12string. Okay. So I'll take the argument
- 57:15type as string and since I want to
- 57:18return the length, so I'll take the
- 57:20return type as integer. Okay. Then I can
- 57:22give some name to the object. Now here
- 57:25as well we have only one argument. So
- 57:27I'll take the I'll take a single
- 57:29argument like this. str then arrow and
- 57:33curly braces. Okay. Now, whatever I
- 57:36provide here will be the implementation
- 57:38of this method apply. Okay. So, I want
- 57:42to return strlength.
- 57:46Okay. Simple. Now using this f_sub_1 I
- 57:51can call apply. Let's say the string is
- 57:54shimum. Okay. So I'm passing a string
- 57:56value and I want to return its length.
- 57:59Okay. So whatever is returned will be
- 58:01printed here. Okay. I'll just print
- 58:03this.
- 58:06All right. Okay. So let me run the
- 58:08project first. Let me just comment these
- 58:11out. Okay. We have already seen
- 58:13predicate.
- 58:15Cool. So if I run the application.
- 58:18Great. So the length of this string is
- 58:20printed which is six. Okay. Now again
- 58:23here we have a scope of optimization.
- 58:25Since it's a single line, I could just
- 58:27remove this. Remove this and this
- 58:30semicolon as well. And we also don't
- 58:32need this return keyword. Okay. So,
- 58:34simply we can write it like this. All
- 58:37right.
- 58:39Now, I'll take one more example. Let's
- 58:41say I have some number, I have some
- 58:43value and I want to return the square of
- 58:46that value. Okay. So, I'll take another
- 58:48function and I have some integer value
- 58:52and I want to return some integer value
- 58:54only. Okay. Okay, I want to return a
- 58:55square root. Not a square root, a
- 58:57square. Okay, let me call it f_sub_2.
- 59:00And we have some number.
- 59:03I'll first write it like this only.
- 59:05Okay. And I want to return number into
- 59:09number. Okay. Now using this f_sub_2, I
- 59:12will call f_sub_2 do.apply and I'll pass
- 59:1610. All right. So this is the number
- 59:19that I passed which will be this one.
- 59:21And in the return basically in this
- 59:23implementation I want to return the
- 59:25square. Okay. Now again this is a single
- 59:28line. So I'll remove this. I'll remove
- 59:30this. And I'll also remove the return
- 59:32keyword. Okay. So simply I'll write it
- 59:35like this. All right. Now let me run my
- 59:37application again. Okay. I've not
- 59:39printed this. Let me print it.
- 59:48Great. So you can see that we pass 10
- 59:50and 100 was returned. Okay. So this is
- 59:54why we use function. It is mainly used
- 59:57to transform data and whenever we want
- 59:59to map one data into another, we can use
- 1:00:02function.
- 1:00:03Now what is consumer? Consumer is a
- 1:00:06predefined functional interface that
- 1:00:08takes one input but returns nothing. It
- 1:00:12is mainly used to perform any operation
- 1:00:14where you don't need to return anything.
- 1:00:17All right. So this is the consumer
- 1:00:19interface as you can see it takes one
- 1:00:21argument but it has no return type.
- 1:00:25Okay. So this is the abstract method
- 1:00:26except that takes one argument but there
- 1:00:28is no return type. All right. So let me
- 1:00:32show you some examples.
- 1:00:34Let's say I just simply create consumer
- 1:00:38and I want to have some string argument.
- 1:00:42Okay.
- 1:00:44So I will take some argument. Let's say
- 1:00:46the argument is name. Okay, I'm giving
- 1:00:48it its name is name. And simply I want
- 1:00:52to print this name. Okay, I don't want
- 1:00:54to return anything. I simply want to
- 1:00:56print my name is
- 1:01:01name. Okay, like this. All right, so
- 1:01:06this is what consumer is used for. I
- 1:01:08took some argument but I didn't want to
- 1:01:10return anything. Okay. Then using this I
- 1:01:13will just call C1 dot accept and I will
- 1:01:16print the name. Okay. Like this.
- 1:01:20All right. Now again this is a single
- 1:01:23line implementation. So I'll remove this
- 1:01:26and simply write it like this. Okay.
- 1:01:30Similarly I can also take a different
- 1:01:32argument. I could have taken integer.
- 1:01:36Okay. C2 is equal to and I could have
- 1:01:40taken my age
- 1:01:42like this and I can again simply print
- 1:01:45it. Okay, this time I'll not take the
- 1:01:46curly braces. I'll give a single line
- 1:01:47implementation.
- 1:01:49Let's say I'm saying my age is something
- 1:01:54okay so my age is age okay and here I
- 1:01:57will pass it C2 dot accept 28. Okay,
- 1:02:02like this. All right, I'll just run it.
- 1:02:06Right? So both these statements are
- 1:02:09printed. Okay. When C1 do.ccept is
- 1:02:11called, this statement will be printed.
- 1:02:13Shivam will be passed as argument over
- 1:02:15here and here it will be printed.
- 1:02:17Similarly, when C2 dot accept is called,
- 1:02:20this number is passed as a value here
- 1:02:23and this is printed here. Okay. So
- 1:02:26nothing is returned from uh this. Okay.
- 1:02:28There is no return type when you call
- 1:02:30these two methods.
- 1:02:32All right. Now at last we have supplier.
- 1:02:36Supplier is a predefined functional
- 1:02:37interface that does not take any input
- 1:02:40but returns some value. It is mainly
- 1:02:43used when you want to generate or supply
- 1:02:45data. All right. So this is the supplier
- 1:02:48interface. As you can see it has this
- 1:02:51method get but it does not take any
- 1:02:53argument. However, it returns some
- 1:02:56value. Okay. So it is kind of opposite
- 1:02:59to consumer. consumer takes value but
- 1:03:02does not have a return type. However,
- 1:03:04supplier does not take any value and has
- 1:03:07some return type. Okay, so let me
- 1:03:09implement this first. I'll just comment
- 1:03:12this out. Okay, so supplier let's say I
- 1:03:16want to return some integer value. Okay,
- 1:03:20so like this I'll just call it. Now
- 1:03:22since it does not have any argument so I
- 1:03:24leave the parenthesis like this and
- 1:03:27arrow
- 1:03:30let's say I want to return some random
- 1:03:32number okay I'm just simply calling I
- 1:03:35simply say return math dot random okay
- 1:03:40like this now this returns double so
- 1:03:43I'll just cast it to int okay like this
- 1:03:48all right now using this I will call s1
- 1:03:51dot get. Okay. And we will just print
- 1:03:54the output
- 1:03:57system.out.t
- 1:03:59print.
- 1:04:02All right. So we didn't take any
- 1:04:04argument but this implementation
- 1:04:07returned some random number. Okay. Let
- 1:04:10me call it. Great. So it returned zero.
- 1:04:13If I'll call it again. Okay. So random
- 1:04:15number is again and again printing zero
- 1:04:17only. All right. Now I can take one more
- 1:04:21example. Let's say this time I want to
- 1:04:23return some string value.
- 1:04:25So let's call it s_2.
- 1:04:30Okay. And I have a string s string a is
- 1:04:35equal to let's say sham
- 1:04:37string
- 1:04:40b is equal to gupta.
- 1:04:45And I want to return a concatenated
- 1:04:48result. Okay. A do.at B. All right. So
- 1:04:53this supplier takes no argument inside
- 1:04:56the implementation. There are two string
- 1:04:58values and we are returning a
- 1:04:59concatenated result. Okay. So when I'll
- 1:05:02call the method s2.get
- 1:05:05this value will be returned. Okay.
- 1:05:08Simply just print this
- 1:05:11system.out.t
- 1:05:14print
- 1:05:16H2.get get okay I'll run it again
- 1:05:21so the concatenated string value is
- 1:05:23returned all right also here I could
- 1:05:26have removed these braces and this
- 1:05:29return as well okay we can simply write
- 1:05:31it like this but here I cannot do this
- 1:05:33because this is not a single line
- 1:05:35implementation
- 1:05:37all right so these were four interfaces
- 1:05:40that are used very widely in Java 8 and
- 1:05:43uh actually when we'll study about
- 1:05:45streams as well there we'll will see the
- 1:05:47use cases of these. So, stream has many
- 1:05:50methods where we use these directly.
- 1:05:53All right. Hello and welcome to my
- 1:05:55channel, the curious coder. This video
- 1:05:58is part of my ongoing Java 8 series.
- 1:06:01Today, I'll be talking about a very
- 1:06:03important but a little tricky topic,
- 1:06:06method reference. So, what exactly is a
- 1:06:09method reference? Simply put, it's a
- 1:06:12shorter way to implement a functional
- 1:06:13interface. But wait, doesn't lambda do
- 1:06:16the same thing? Yes, it does. However,
- 1:06:20at times we can make the syntax even
- 1:06:23more optimized using method references.
- 1:06:26I know it sounds a bit confusing in the
- 1:06:28beginning. That's why in this video I'll
- 1:06:30explain it in the simplest way possible
- 1:06:32and the video will be full of examples
- 1:06:35to help you understand it easily. Okay,
- 1:06:38so let's get started. So I have created
- 1:06:41a functional interface named calculator.
- 1:06:44Now what is a functional interface? Any
- 1:06:46interface that contains exactly one
- 1:06:48abstract method is called a functional
- 1:06:51interface. All right. So this interface
- 1:06:54contains only one abstract method named
- 1:06:56calculate. That is why this can be
- 1:06:58considered as a functional interface.
- 1:07:01Okay. Now we can implement this method
- 1:07:04calculate in a very short way using
- 1:07:06lambda expressions and sometimes we can
- 1:07:09make it even shorter using method
- 1:07:11reference. Okay. So let me show you.
- 1:07:14Let's first see how we can do it using
- 1:07:16lambda expressions. Okay. So the syntax
- 1:07:18is very simple. First we need to write
- 1:07:20the interface name like this. Then we'll
- 1:07:24give some name to the object. Okay. Then
- 1:07:28since in this method there are two
- 1:07:30arguments. So here as well I will have
- 1:07:32to take two arguments. Okay. Like this.
- 1:07:35After that we use the lambda operator.
- 1:07:37This arrow operator. Okay. And after
- 1:07:40these we put some curly braces. Okay. we
- 1:07:42put these curly braces and end with a
- 1:07:44semicolon. All right. Now, whatever you
- 1:07:47write between these two curly braces,
- 1:07:49whatever you write here actually
- 1:07:51represents the implementation of this
- 1:07:54method calculate. Okay. So, let's say I
- 1:07:57just want to add these two. Okay. So,
- 1:07:59I'll say return a plus b. Okay. Like
- 1:08:04this. So, this part this part is
- 1:08:08actually the implementation of this
- 1:08:10method calculate. Okay. So if I run
- 1:08:13this, if I run this code, will any does
- 1:08:15anything happen? Let me see.
- 1:08:20All right. So as of now, nothing will
- 1:08:22happen because we have only declared the
- 1:08:24implementation. We have not actually
- 1:08:26called this method calculate. Okay. This
- 1:08:28has not been triggered yet. So for that
- 1:08:31we have this object C1. And using this
- 1:08:33C1, we can call this calculate method.
- 1:08:37And here I can pass some values. Okay.
- 1:08:40So as you can see I passed 10 and 20. So
- 1:08:42this 10 will be passed on over here and
- 1:08:4520 inside b and the result will be a + b
- 1:08:48which we'll get here. Okay. Because here
- 1:08:50is where we are actually calling the
- 1:08:52method. All right. So I'll print the
- 1:08:54result.
- 1:08:58All right.
- 1:09:00Great. So you can see that the sum of 10
- 1:09:02and 20 has been printed. All right. So
- 1:09:05this is how lambda expressions work. Now
- 1:09:08can I make it make this syntax even
- 1:09:10shorter and convert it into a method
- 1:09:12reference? So actually no. I cannot
- 1:09:16convert this lambda expression into a
- 1:09:18method reference. Why? Because method
- 1:09:20reference has two conditions. Number one
- 1:09:23that the implementation should only have
- 1:09:25a single statement. All right, which in
- 1:09:27this case is true. You can see that in
- 1:09:29inside this implementation we only have
- 1:09:31a single statement. But there is a
- 1:09:33second condition that says that there
- 1:09:36should be some method call happening in
- 1:09:38that statement. Okay. So is a method
- 1:09:41call happening here? No. We are actually
- 1:09:43just taking two arguments and we are
- 1:09:45adding those. Okay. So it does not
- 1:09:48qualify for method reference. All right.
- 1:09:51Now let me change this example a little
- 1:09:53bit. What I'm going to do is I have this
- 1:09:56class math operations. Inside this we
- 1:09:59have this method add. It is a static
- 1:10:02method and it takes two argument and it
- 1:10:04returns the sum of those two values.
- 1:10:07Okay. So instead of directly just
- 1:10:10returning a plus b, I'll change the
- 1:10:12example. What I'll do? I'll just call
- 1:10:14math operations dot add. And let's say I
- 1:10:18pass a comm, b.
- 1:10:21Okay. So we have changed the
- 1:10:22implementation a little bit. Now there
- 1:10:25is a single statement and inside this
- 1:10:27statement there is a method call
- 1:10:30happening. Okay, we are calling this
- 1:10:32method add which belongs to this class
- 1:10:34math operations. Okay, so now is it
- 1:10:37eligible for method reference? Yes, now
- 1:10:40it is actually eligible for method
- 1:10:42reference. So let me show you the
- 1:10:44syntax. What I can do is instead of
- 1:10:47writing all this, we just have to write
- 1:10:50class name colon method name. I'll
- 1:10:53repeat the syntax is class name colon
- 1:10:56method name. Okay. So again I'll give
- 1:11:00the interface name. Let's name this
- 1:11:02object as C2. And instead of writing all
- 1:11:05this, okay, instead of writing all this,
- 1:11:07what I need to do is I'll just write
- 1:11:09class name then colon and then this
- 1:11:12method name. Okay, like this and end
- 1:11:16with a semicolon. That's it. This
- 1:11:19actually represents this much. Okay. And
- 1:11:22now using this C2, I will call the
- 1:11:25method calculate. And again I'll pass
- 1:11:27the value 10, 20. Okay. And let's just
- 1:11:31print this as well.
- 1:11:36All right. Like this. Okay. Now if I'll
- 1:11:40run my application.
- 1:11:42Great. So you can see that this was the
- 1:11:45implementation that was done using
- 1:11:46lambda and this is the implementation
- 1:11:49that is done using method reference.
- 1:11:51Okay. So the compiler automatically
- 1:11:54knows that whatever value you are
- 1:11:56passing here like you're passing 10 and
- 1:11:5820 that has to be populated inside this
- 1:12:00method add. Okay. So you don't need to
- 1:12:03specify the arguments here. This is the
- 1:12:05syntax. Okay. You just pass the value
- 1:12:08and compiler automatically knows that
- 1:12:10this add contains two arguments and the
- 1:12:13first one will go inside A and the
- 1:12:15second one will go inside B. Okay, this
- 1:12:17is how you write it. So the condition is
- 1:12:19that there should be a single statement
- 1:12:21and inside that statement there should
- 1:12:24be a method call that is happening.
- 1:12:26Okay. Now there are actually four
- 1:12:29different types of method references.
- 1:12:31Static method reference, instance method
- 1:12:33reference of a particular object,
- 1:12:36instance method reference of an
- 1:12:38arbitrary object and constructor method
- 1:12:40reference. So one by one let's
- 1:12:43understand each of them. First one is
- 1:12:45static method reference. It is used when
- 1:12:48we are calling a static method directly
- 1:12:50by the class not by an object. So the
- 1:12:53syntax is class name colon static method
- 1:12:57name. All right let's look at it. So the
- 1:13:00example that I just covered here
- 1:13:02actually belongs to the static method
- 1:13:03reference because this add was a static
- 1:13:07method inside this class math
- 1:13:09operations. Okay. And you can see that
- 1:13:11we have directly called this method
- 1:13:13using math operations. Okay. So this is
- 1:13:16a static method reference. Okay. Now
- 1:13:19I'll take one more example. Let's say I
- 1:13:22have one more method inside this class.
- 1:13:25public static int
- 1:13:28multiply
- 1:13:31int a comma int b. Okay. And it returns
- 1:13:35a into b. All right. So what I'm going
- 1:13:39to do is again I'll first implement
- 1:13:41using lambda and then we'll see how we
- 1:13:44can do it using method reference. Okay.
- 1:13:46So this is how I'm going to do in the
- 1:13:48complete video so that you get better
- 1:13:50clarity and initially you should do it
- 1:13:52like this only that first try to
- 1:13:54implement using lambda and then convert
- 1:13:56it into a method reference. Okay.
- 1:13:59Eventually if you start doing it you
- 1:14:00might be able to do it directly as well.
- 1:14:03Okay. calculator C3 is equal to I have
- 1:14:07to take two arguments a comma B then
- 1:14:10this operator these curly braces and
- 1:14:13here I'll give the implementation which
- 1:14:15is return math operations dot multiply
- 1:14:19and I'll pass a comma b okay using this
- 1:14:22C3 I can call the calculate method let's
- 1:14:25say I'm doing 10 into 3 okay now if I
- 1:14:30want to convert this into a method
- 1:14:32reference the syntax this first let me
- 1:14:35take calculator C4 then this is the
- 1:14:38class name and this is the method name
- 1:14:40so I'll just write math operations colon
- 1:14:43multiply okay and it ends with a
- 1:14:46semicolon okay so like this this is how
- 1:14:49I can implement the functional interface
- 1:14:52and now using the C4 I can call the
- 1:14:54calculate method 10 3 all right so let
- 1:14:59me just comment this one and I'll run
- 1:15:03this part.
- 1:15:05Okay, I have not printed this
- 1:15:10system.out.print
- 1:15:12ln.
- 1:15:16Similarly,
- 1:15:25great. So you can see that we get the
- 1:15:27result as 10 into 330 for both these
- 1:15:30cases. This is how we do we did using
- 1:15:32lambda and this is how we did using
- 1:15:35method reference.
- 1:15:37Okay. Next is instance method reference
- 1:15:41of a particular object. It is used when
- 1:15:44we are calling an instance method of
- 1:15:45some class using its object. The syntax
- 1:15:49is object name colon instance method
- 1:15:52name. All right let's see. So I'll take
- 1:15:55another example. What I'll do is I'll
- 1:15:58create another interface named
- 1:16:00validator.
- 1:16:02It's also a functional interface
- 1:16:05and it has this method boolean validate.
- 1:16:10Okay. And here we take some number. All
- 1:16:12right. So I want to implement this
- 1:16:14functional interface. Okay. Inside the
- 1:16:17math operations I'll add another method
- 1:16:21public boolean is positive. Okay. It
- 1:16:26takes a number and it checks whether the
- 1:16:29number is positive or not. Okay, return
- 1:16:31num is greater than zero. And you must
- 1:16:34note that this method is not a static
- 1:16:36method. Okay, these two are static
- 1:16:38methods but this one is a simple
- 1:16:40instance method. Okay. Now let's say I
- 1:16:44want to give some implementation of this
- 1:16:46validator. So first let me do it using
- 1:16:49the lambda expression.
- 1:16:52All right. Let me comment this part.
- 1:16:55So what's the syntax? First I'll write
- 1:16:57the interface name validator v_ub1 is
- 1:17:00equal to there is a single argument
- 1:17:02number like this and here I'll give the
- 1:17:05implementation. Okay. So the
- 1:17:08implementation is I want to call this
- 1:17:11method is positive okay on the number.
- 1:17:13Now since it is not a static method so
- 1:17:16if I'll try to call it like this math
- 1:17:18operations dot is positive.
- 1:17:22Okay, I won't be able to do this. All
- 1:17:24right, because this is not a static
- 1:17:26method. That is why I will have to
- 1:17:28create an object of math operations.
- 1:17:31Okay, math operations like this.
- 1:17:34Okay, now using this object, I can call
- 1:17:37this method, right? So, all right, I'll
- 1:17:41do it like this. Math operations dot is
- 1:17:43positive and here I'll pass the number.
- 1:17:46Okay, simple. And I'll just return this
- 1:17:50value. All right. Then using this v1, we
- 1:17:53will call the method validate.
- 1:17:56Okay. And let's say I pass the number
- 1:17:5810. And I pass the number let's say
- 1:18:02minus 2. Okay. And we'll just print the
- 1:18:05result. System.out.t print ln.
- 1:18:11Similarly here as well.
- 1:18:17Okay. So this is how we can implement
- 1:18:19using lambda expression. Okay. If I run
- 1:18:23this, great. You'll see that we get true
- 1:18:25for this but false for this one because
- 1:18:27it is a negative number. Okay. Now can
- 1:18:30this be converted into method reference.
- 1:18:33All right. So this is a single line.
- 1:18:35Yes. And here a method call is
- 1:18:37happening. Okay. So yes, I can convert
- 1:18:39this into a method reference. But this
- 1:18:42is not a static method reference. The
- 1:18:44difference is that we have called this
- 1:18:46method using this object. Okay. So the
- 1:18:49syntax is that I'll write the interface
- 1:18:51name. I'll give some object and first I
- 1:18:54will have to write the object name. So
- 1:18:56instead of the class name I'll have to
- 1:18:58write the object name which I've taken
- 1:19:00math operation. I'll take something else
- 1:19:01so that it's a little bit confusing.
- 1:19:04Let's say like this we call it M1. Okay.
- 1:19:07So here I will have to write M1 colon
- 1:19:11and then is positive. Okay, like this.
- 1:19:14So this is the syntax. Using this I can
- 1:19:16again call v2 do.validate I'll just copy
- 1:19:21paste this.
- 1:19:24Okay. Instead of v_sub1 I'll take
- 1:19:25v_sub_2.
- 1:19:27All right. If I run it again you'll see
- 1:19:30we get the same result. True false then
- 1:19:32true false. Okay. So this is how we did
- 1:19:36the implementation using lambda. And
- 1:19:39using this method reference we can
- 1:19:41perform the same action. Okay. So the
- 1:19:43syntax is object name col the method
- 1:19:46name. Okay. Next we have instance method
- 1:19:50reference of an arbitrary object. It is
- 1:19:53used when we are calling an instance
- 1:19:55method on an object that is passed as an
- 1:19:58argument. The syntax is class name col
- 1:20:02instance method. All right let's see. So
- 1:20:05again I'll take a different example.
- 1:20:08Let's say I create an interface string
- 1:20:11operations
- 1:20:13and mark it as a function interface.
- 1:20:17Now this has a method int operate. Okay.
- 1:20:21And it takes some string parameter.
- 1:20:23Let's say string str. All right. So if I
- 1:20:26want to implement this using lambda,
- 1:20:28what I can do first? Let me just comment
- 1:20:31this out. I'll write the interface name
- 1:20:34string operations. give some name to the
- 1:20:37object S1. Now there is a single
- 1:20:39parameter. So I'll take one parameter
- 1:20:41and
- 1:20:43between these curly braces I will have
- 1:20:45to give my implementation. Okay.
- 1:20:47Implementation for this method operate.
- 1:20:50All right. So let's say I want to return
- 1:20:52the length of the string. Okay.
- 1:20:54String.length.
- 1:20:55All right. So this is an int value that
- 1:20:58actually and you'll see that this
- 1:21:00operate method also returns some int
- 1:21:01value. Okay. So I've returned
- 1:21:03string.length.
- 1:21:04All right. Now using this S1 I can call
- 1:21:07S1.operate and let's say I pass my name.
- 1:21:12All right. And let me just print the
- 1:21:14result whatever the result will be.
- 1:21:19Cool. So this is how I can implement
- 1:21:21using lambda. If I run this great you
- 1:21:24will see that the length of the string
- 1:21:26sham has been printed. Okay. Now can I
- 1:21:30convert this into method reference?
- 1:21:32again yes because it's a single line and
- 1:21:35only a method call is taking place. Okay
- 1:21:38so but in this case the thing that is
- 1:21:40different is that here in this example
- 1:21:43you can see that we had an object of
- 1:21:45math operations and using this object we
- 1:21:47were calling a method. Okay but in this
- 1:21:50case we have this argument and using
- 1:21:53this argument we are calling some
- 1:21:54method. Okay that is the difference
- 1:21:56between these two types. Okay. So syntax
- 1:22:01wise there is not much difference. In
- 1:22:03this case the object was used object
- 1:22:06name col the method name. In this case
- 1:22:09again we have class name colon the
- 1:22:11instance method name. So now what will
- 1:22:13be the class here. So this str is of the
- 1:22:16type string. So the class will be
- 1:22:18string. Okay, let me show you. String
- 1:22:21operations s_2 is equal to I have to
- 1:22:26write string colon length. Okay, like
- 1:22:30this. So here we'll have the method name
- 1:22:33and the class name will be the data type
- 1:22:35of the argument that is being passed
- 1:22:37there. Okay, so this is a string
- 1:22:39argument. That is why we'll take string.
- 1:22:42Okay, now using this s2 we can call
- 1:22:44operate and again I'll pass this.
- 1:22:48All right. And let me just run it. Let
- 1:22:50me just print this.
- 1:22:56Great.
- 1:22:58Okay. So you'll see that both will
- 1:22:59return the same result. Okay. Similarly,
- 1:23:02I'll take one more example. Let's say
- 1:23:04there is string operations.
- 1:23:08Okay. And
- 1:23:11instead of returning the length, I want
- 1:23:13to return hash code. Okay. Okay. So hash
- 1:23:16code is another method inside this class
- 1:23:18string. Okay. Let me name this S3.
- 1:23:22All right. So using this I can just let
- 1:23:26me
- 1:23:28copy paste this. Okay. S3 dot operate
- 1:23:32shivam. Okay. This is the string. And
- 1:23:35similarly I'll do this thing using the
- 1:23:39method reference. Okay. So how can I do
- 1:23:41it? Again the object is str which is of
- 1:23:45the type string. So I will have to take
- 1:23:47string operations s4 is equal to the
- 1:23:50class name is string col hash code. So
- 1:23:54this time the method name is hash code.
- 1:23:56Okay like this using this s4 I can call
- 1:23:59the method operate and I'll pass sham
- 1:24:02over here. Okay then again let me just
- 1:24:05print this.
- 1:24:07I'll change this to s4.
- 1:24:10All right. So if I run this now you can
- 1:24:14see that some hash code has been
- 1:24:15printed. Okay. This is using lambda and
- 1:24:17this is using method reference.
- 1:24:20All right. Now at last we have
- 1:24:23constructor method reference. It is used
- 1:24:26when we are calling a constructor to
- 1:24:28create a new object. The syntax is class
- 1:24:31name col new. All right let's see. So
- 1:24:35I'll take another example. I'll just
- 1:24:37simply create one more interface.
- 1:24:39let's say creator
- 1:24:42and mark it as a functional interface
- 1:24:46and I'll create a method named create
- 1:24:50okay string name all right it takes an
- 1:24:53argument string name and the return type
- 1:24:55I want to pass using the generics okay
- 1:24:58so we can have a generic return type
- 1:25:00there is no fixed return type okay so at
- 1:25:02the time of implementing this interface
- 1:25:05I'll decide what return type I want to
- 1:25:07give okay Cool. Now I'll take two
- 1:25:11classes. One is a class named user. It
- 1:25:14has this field string name.
- 1:25:19And I'll simply just generate a
- 1:25:22constructor.
- 1:25:24Okay. Similarly, I'll create one more
- 1:25:26class named product
- 1:25:29and it also has a field string. Let's
- 1:25:32say product name. Okay.
- 1:25:36Don't worry, it will all be clear once
- 1:25:37the example is completed. Okay. And I'll
- 1:25:40just generate a constructor for this as
- 1:25:42well. Okay. So, we have two different
- 1:25:44classes product and user. And we have a
- 1:25:47functional interface named creator.
- 1:25:49Okay. That has this abstract method
- 1:25:51create. It takes a string parameter and
- 1:25:53we can have any return time. Okay. So,
- 1:25:56let's say I want to implement this
- 1:25:58first. Let me just comment this part.
- 1:26:02All right. So again the syntax is first
- 1:26:05let me do it using lambda. So I'll take
- 1:26:07a creator. I'll first write the
- 1:26:09interface name and here in the generics
- 1:26:11I will have to pass the return type. So
- 1:26:14let's say I want to create a user. Okay.
- 1:26:16So the return type I take as user. Okay.
- 1:26:18Let me call it C1 is equal to now this
- 1:26:22method has one parameter. So I'll take
- 1:26:25this parameter
- 1:26:27like this. And let's say in this
- 1:26:29implementation in this implementation of
- 1:26:32this method create I want to return a
- 1:26:34user. Okay, I want to create a user. So
- 1:26:37what I'll do I'll just create return new
- 1:26:40user and I will pass the name. Okay, so
- 1:26:45actually I'm calling this constructor.
- 1:26:47Okay, this constructor that I created.
- 1:26:50All right, now using this C1 I will just
- 1:26:53call the create method and let's say I
- 1:26:55create a user. Okay, like this. So can I
- 1:27:00convert this into a method reference?
- 1:27:03Yes, this is also a type of method
- 1:27:05reference because here we are actually
- 1:27:07calling a constructor. Okay, and
- 1:27:09constructor is also a special type of
- 1:27:10method. Okay, so it's a single line and
- 1:27:13we are calling the constructor. So this
- 1:27:15can be converted into a method
- 1:27:16reference. Okay. So the syntax is the
- 1:27:20class name colon new. All right. So I'll
- 1:27:23take the class name creator.
- 1:27:26Okay, first I have to give take the
- 1:27:29interface name and then I'll take the
- 1:27:31class name. Class name is user. Okay, in
- 1:27:33this case user colon new. All right. So
- 1:27:38this represents this. Okay, I know it is
- 1:27:43new to you and these are very different
- 1:27:45examples but these are four types of
- 1:27:47syntaxes that you must learn and if you
- 1:27:50are going to practice using some
- 1:27:52examples, you'll definitely get a hold
- 1:27:54of these. Okay. So using this C2 I will
- 1:27:57call the create and I'll create the user
- 1:28:00shaman.
- 1:28:03Okay like this. So actually this method
- 1:28:07returns T. So T in this case is user. So
- 1:28:10actually this will be user. Okay. I'll
- 1:28:13extract the variable user u1
- 1:28:17and this one user U2.
- 1:28:21Okay. So we can just simply print the
- 1:28:24name again just to verify. Okay. So I
- 1:28:27will print U1
- 1:28:30dot name
- 1:28:32and similarly
- 1:28:35I can print U2.name. All right. Let me
- 1:28:38run this. Great. So you see that we pass
- 1:28:42sham. We pass sham. A new user was
- 1:28:45created and that user was returned here.
- 1:28:48this user and here we have printed the
- 1:28:50name which is coming out to be shown and
- 1:28:52the same thing we have done using method
- 1:28:53reference we wrote the class name col
- 1:28:56new okay so this represent constructor
- 1:28:58reference and then using this object C2
- 1:29:02we did C2Create and a user U2 was
- 1:29:04created okay and then I have printed the
- 1:29:07name all right similarly I can I'll take
- 1:29:11another example using the product as
- 1:29:13well okay so we have taken create a
- 1:29:15product let's say C3 tree is equal to
- 1:29:19name
- 1:29:22and this time we are returning new
- 1:29:24product. Okay. And I'll pass the name.
- 1:29:27All right. So this time I'm calling this
- 1:29:29constructor.
- 1:29:31Okay.
- 1:29:33And using the C3 I will call the create
- 1:29:35method. Let's say the product is Apple.
- 1:29:40Okay. Apple watch.
- 1:29:43And uh similarly how can I convert it
- 1:29:46into a method reference? I'll take the
- 1:29:48interface name product C4 is equal to so
- 1:29:52the class name here is product okay so
- 1:29:55I'll take product colon new okay like
- 1:29:59this then using this C4 I'll again call
- 1:30:02the create method let's say Apple watch
- 1:30:07okay so this will return a product okay
- 1:30:12product let's call it P1
- 1:30:15and this will also return a product
- 1:30:20okay let me write it like this only
- 1:30:22product
- 1:30:24pay2 is equal to C4 create because
- 1:30:27create returns this type and T is
- 1:30:31product in this case okay so using this
- 1:30:34again I can print let me just copy paste
- 1:30:39P1 dot product name okay we have the
- 1:30:42field product name similarly
- 1:30:463
- 1:30:472 dot
- 1:30:50product name. Okay, if I run it, you'll
- 1:30:54see that Apple watch has been printed
- 1:30:55twice. Okay, once using this lambda
- 1:30:58expression and then using method
- 1:31:00reference. Okay, so these are all the
- 1:31:03different method references that were
- 1:31:05covered in Java 8. I know that the
- 1:31:07syntax is a bit tricky in the beginning,
- 1:31:09but trust me, if you practice it a lot,
- 1:31:12you'll definitely get a hold of it.
- 1:31:14Okay. Hello and welcome to my channel
- 1:31:17the curious coder. This video is part of
- 1:31:20my ongoing Java 8 series. Today we are
- 1:31:23going to explore a really powerful
- 1:31:25feature which was introduced in Java 8,
- 1:31:28the optional class. It's a feature whose
- 1:31:31addition was a complete gamecher in
- 1:31:34handling null pointer exceptions. Now if
- 1:31:37you're a working professional, you have
- 1:31:39probably seen code like these a lot. And
- 1:31:41if you're a student, you'll definitely
- 1:31:43come across optional in future. At
- 1:31:46first, it can seem confusing, but don't
- 1:31:48worry. In this video, I'll explain
- 1:31:51optional line by line, word by word, so
- 1:31:54you can understand it in depth. So, what
- 1:31:56exactly is optional? Optional is a
- 1:31:59special class in Java that may or may
- 1:32:02not contain a value. Its main purpose is
- 1:32:05to help us reduce the chances of running
- 1:32:07into null pointer exceptions, thereby
- 1:32:10making our code safer and more readable.
- 1:32:13So how does optional help avoid null
- 1:32:15pointers? Since an optional may or may
- 1:32:18not contain a value, it forces us to
- 1:32:20think about the case when the value is
- 1:32:22missing. As a result, we naturally end
- 1:32:25up handling those cases instead of
- 1:32:27running into null pointers. All right.
- 1:32:30So let's see it in practical. All right.
- 1:32:33So let's consider this example. Here we
- 1:32:36are fetching a user by calling this
- 1:32:38method get user by ID. So we have
- 1:32:41created a class named user. It has these
- 1:32:44two fields ID and name. And we are
- 1:32:46fetching this by calling this method.
- 1:32:49Okay. Now this method belongs to the
- 1:32:51class user service. Here we have taken a
- 1:32:54list of users user 1 2 and three. And we
- 1:32:58take ID as a parameter. So we loop on
- 1:33:01this list, we loop on this list and if
- 1:33:04we find a match for this ID, okay, if we
- 1:33:07find any match, then we return the user.
- 1:33:10Otherwise, we simply return null. Okay,
- 1:33:12so null represents that user not found.
- 1:33:15Okay, user was not there in this list.
- 1:33:18All right, so simple. So if I run this
- 1:33:21code by taking the ID2,
- 1:33:24then you'll see that it runs perfectly.
- 1:33:26Okay, we got a user and here we are
- 1:33:28printing the name and ID of the user.
- 1:33:31Okay, but let's say I change the ID to
- 1:33:34four. Okay, now if I run my application,
- 1:33:37great. We will encounter a null pointer
- 1:33:39exception. Okay, why? Because there was
- 1:33:43no user present against the ID 4 and
- 1:33:46thereby null was returned and here we
- 1:33:49called get name and get ID on a null
- 1:33:51reference. Okay, that's why we got a
- 1:33:53null pointer exception. Okay. So how to
- 1:33:56handle it? One way is that we can add a
- 1:33:58null pointer check here like this. If
- 1:34:01user is not equal to null
- 1:34:05then we should print this statement.
- 1:34:07Okay. Now if I if I'll run it then
- 1:34:11nothing will be printed but there will
- 1:34:13be no null pointer exception. All right.
- 1:34:16But see the problem here is that in an
- 1:34:19actual codebase many developers might be
- 1:34:22working on the same project. Okay. Okay,
- 1:34:24so it's possible that one developer
- 1:34:25wrote this user service and this method
- 1:34:27was written and then another developer
- 1:34:30came and he just started to use this
- 1:34:32method. Okay, and somehow he forgot to
- 1:34:34add the null pointer check. Okay, that
- 1:34:36is a possibility. All right, and even if
- 1:34:39he doesn't forget it here, but let's say
- 1:34:41then there are many lines of code and
- 1:34:43after that someone uses this user again.
- 1:34:46Okay, we have a null check here but
- 1:34:48after some lines then we forgot to add a
- 1:34:51null check there. Okay, that is quite
- 1:34:53possible. So the thing is no one is you
- 1:34:56know reminding me that we have to add a
- 1:34:58null pointer check. We need a reminder
- 1:35:01that tells us that it is necessary for
- 1:35:03us to add a null pointer check to avoid
- 1:35:05any null pointer exception. Okay, so
- 1:35:08that is what optional does. It helps us
- 1:35:10remind that it is necessary for us to
- 1:35:13add a null pointer check. Okay, so I'll
- 1:35:15show you how. What we do is I'll just
- 1:35:18replace it with optional
- 1:35:22like this. Okay. So don't worry about
- 1:35:24the syntax now. I'll explain everything.
- 1:35:27So we do optional dot of
- 1:35:30user and here instead of returning null
- 1:35:33a little optional dom. Okay. So I'm
- 1:35:37making few changes in my code. Okay.
- 1:35:40I'll remove this null check as of now.
- 1:35:43And this get user by ID returns an
- 1:35:46optional user. Okay,
- 1:35:50like this. And I'll call it
- 1:35:52optional user.
- 1:35:56Okay, import. And let me just remove
- 1:35:58this for now. All right. So what we did
- 1:36:01was instead of returning a user
- 1:36:04directly, we wrapped it inside an
- 1:36:06optional and then returned it. Okay. So
- 1:36:09now what exactly is optional? Let's see.
- 1:36:12You'll see optional is a class. Okay, it
- 1:36:15is a class and it contains only one
- 1:36:17field this value. Okay, this is the only
- 1:36:20field inside this class optional. And
- 1:36:23whatever you pass in this generics is
- 1:36:26the return type of this value. Okay. So
- 1:36:28since we have passed this user that is
- 1:36:32why we are able to you know in this
- 1:36:34return type you'll see that I've taken
- 1:36:35the return type as optional of user that
- 1:36:38is why inside this optional the value
- 1:36:40will be of the type user. Okay we can
- 1:36:43and we can wrap a user value inside this
- 1:36:46optional. Okay this is what we did. So
- 1:36:49now what can I do when I see that an
- 1:36:52optional value has been returned from
- 1:36:53here. Now it is my duty that I have to
- 1:36:57put a null pointer check. Okay, why?
- 1:36:59Because optional may or may not contain
- 1:37:01a value. Okay, so I'm forced to think
- 1:37:04that okay, it might be possible that it
- 1:37:06does not contain the value. That is why
- 1:37:08I will have to put a null pointer check.
- 1:37:11Okay, and I don't even have to put it
- 1:37:12directly. Optional has given me many
- 1:37:14methods that let me handle it properly.
- 1:37:17Okay, so we have one method optional
- 1:37:19user dot is present.
- 1:37:22Okay, so this automatically checks
- 1:37:24whether the user was present or not.
- 1:37:27Okay, so it's kind of a null pointer
- 1:37:28check. Okay, so now what I'll do, I'll
- 1:37:32print the name and the ID. Okay, so
- 1:37:37we'll take optional user dot get name.
- 1:37:41But wait, this is not the type of user.
- 1:37:44It is type of optional user. And this
- 1:37:46get name method is inside optional is
- 1:37:49inside user. Okay, so how can I print
- 1:37:51it? So somehow we need to fetch the
- 1:37:54actual user from this optional user.
- 1:37:56Okay, so for that we have a method named
- 1:37:59get.
- 1:38:00Okay, this get will bring me the actual
- 1:38:03user. All right, so I can write it like
- 1:38:06this. User user is equal to optional
- 1:38:09user.get. And now using this user, I
- 1:38:11will print the name as well as
- 1:38:15I will print the ID. Okay, user dot get
- 1:38:19ID.
- 1:38:21All right, let me just add name
- 1:38:27and
- 1:38:29ID.
- 1:38:32Okay,
- 1:38:34so what we did was I'll just repeat we
- 1:38:38changed it to optional of user and then
- 1:38:40we added this if condition that if the
- 1:38:42user is present then only fetch the
- 1:38:44user. Now this user cannot be null
- 1:38:47because we have already added this
- 1:38:48check. can be uh fested using this
- 1:38:51method get and now using this user we
- 1:38:53are printing the name and the id. Okay.
- 1:38:56So if I run it for ID2 you will see that
- 1:39:00the name and ID has been printed. But if
- 1:39:03I run it for ID4
- 1:39:06now nothing has been printed but there
- 1:39:08is no null pointer exception. Okay there
- 1:39:10is no exception. All right. So now one
- 1:39:13by one I'll tell you what these methods
- 1:39:15do. Okay. So first I'll tell you about
- 1:39:18this optional do of okay because this is
- 1:39:21where we actually created an optional
- 1:39:23object okay this is where we wrapped our
- 1:39:25user inside an optional okay so what it
- 1:39:28does is if I go inside this off you will
- 1:39:32see that it returns a new optional okay
- 1:39:34so this is where the object has been
- 1:39:36created okay so if I go here you'll see
- 1:39:39that this is a constructor okay so this
- 1:39:42is actually populating the value this
- 1:39:45value inside the value which is this
- 1:39:47field. Okay, so this is the constructor.
- 1:39:50So by calling the optional off I've
- 1:39:52actually populated the user as a
- 1:39:54optional user. Okay, so this value field
- 1:39:57has been populated. This value field has
- 1:40:00been populated. Okay, now optional empty
- 1:40:03in case of this what happens is that it
- 1:40:06actually returns empty and empty is also
- 1:40:08an optional object but it does not
- 1:40:10contain any value. Okay, it's it's an
- 1:40:12empty field. So in this case optional
- 1:40:15does not contain anything. Okay. Now
- 1:40:18this method is present. If I go inside
- 1:40:20this it's a very simple method. It says
- 1:40:23if value is not equal to null then
- 1:40:25return true otherwise return false.
- 1:40:26Okay. So it's simply checking whether
- 1:40:28this value field has been populated or
- 1:40:30not. Okay. So in case of empty it is not
- 1:40:34populated. But in case when we actually
- 1:40:37populated it by calling optional.off
- 1:40:40then we get something. Okay. Then it
- 1:40:42returns true.
- 1:40:43All right. Now this dot get method it
- 1:40:46does nothing. If the value is null it
- 1:40:48will return no value present. But if it
- 1:40:51is not null then it will return a value.
- 1:40:53Okay. So somehow if you call this get
- 1:40:55method without this is present check
- 1:40:57then it will return this error. Okay.
- 1:41:00But here since we added the check that's
- 1:41:02why it returned the actual value. Okay.
- 1:41:05Which in this case is user because this
- 1:41:07t is of type user. All right. So these
- 1:41:10were all the methods that were here in
- 1:41:12the picture. Now so far we have seen
- 1:41:15optionals methods like is present of
- 1:41:18empty and get. Now these are few more
- 1:41:21methods that optional class contains.
- 1:41:24One by one we'll understand each of them
- 1:41:26with examples. If present is an optional
- 1:41:29classes method which executes a given
- 1:41:32action only if the value is present. All
- 1:41:35right let me show you. So now instead of
- 1:41:38writing it like this, what I'll do is
- 1:41:41I'll simply take optional user and I'll
- 1:41:43call this method if present. Okay, here
- 1:41:46I can take a user and then we can just
- 1:41:50add this print statement inside this
- 1:41:53lambda expression. Okay, like this. All
- 1:41:56right, so this code has been converted
- 1:41:58into a single line using this if present
- 1:42:01method. Okay, so I'll just simply
- 1:42:02comment this out. And let's say if I run
- 1:42:05for ID 2,
- 1:42:08great. If I run for ID2, then the user
- 1:42:11is present, which means this statement
- 1:42:12will be executed. But if I run for ID4,
- 1:42:17then nothing will be executed. Okay, so
- 1:42:20this is how if present works. It only
- 1:42:22executes this statement when the user is
- 1:42:24present. Otherwise, it does not. Okay.
- 1:42:28Now what we are going to do is I'll go
- 1:42:30inside this method and we'll try to
- 1:42:32understand what exactly is happening
- 1:42:34behind the scene. Okay. So I'll you know
- 1:42:37I'll teach you line by line what this
- 1:42:39code represents and we'll look at what
- 1:42:41exactly is happening behind the scene.
- 1:42:43Okay. So this if present method takes
- 1:42:46something called consumer. Okay. It has
- 1:42:48this argument consumer. So what is a
- 1:42:50consumer? Consumer is a functional
- 1:42:52interface. Okay. And it has this method
- 1:42:55named accept. Okay, it has this abstract
- 1:42:57method named accept that takes one
- 1:42:59argument but it returns nothing. All
- 1:43:02right, so what are functional
- 1:43:05interfaces? These are interfaces that
- 1:43:07have a single abstract method and we can
- 1:43:10implement them by giving a lambda
- 1:43:11expression. Okay, and that is exactly
- 1:43:13what we have done over here. Okay, so
- 1:43:16I'll just break it down in a simpler
- 1:43:17way. I'll first show you how to
- 1:43:19implement the interface. Okay, so we
- 1:43:22have a consumer and we want the argument
- 1:43:24to be user. So I'll just simply show it
- 1:43:27show it to you how we can actually
- 1:43:28implement it using lambda expression.
- 1:43:31Okay. So we'll take a user. We'll take
- 1:43:34one parameter.
- 1:43:36All right. Why? Because this except has
- 1:43:39a single parameter. Okay. So I'll take a
- 1:43:41parameter like this. Then I'll add the
- 1:43:44arrow and here we can provide the
- 1:43:46implementation. Okay. So this
- 1:43:48implementation is of this accept method.
- 1:43:51Okay. So here I can actually add this
- 1:43:53logic. So my logic is that I want to
- 1:43:55print the user's name and ID. Okay, so
- 1:43:58here I can print the user's name and ID.
- 1:44:02Okay, like this. And let me just comment
- 1:44:04this for now. Okay, so this is how I can
- 1:44:06implement a consumer interface. Okay, so
- 1:44:09now if I'll run this, will anything
- 1:44:11happen?
- 1:44:12No, actually nothing will happen. Why?
- 1:44:15Because as of now we have not called
- 1:44:17this accept method. Okay, we have to
- 1:44:19call this method. So if I'll do C1 do
- 1:44:22accept
- 1:44:24and here I will pass the user. Okay,
- 1:44:28I'll pass my user. So where will I get
- 1:44:30the user? Optional user dot get. Okay,
- 1:44:33let me let the ID be two. Okay, now if I
- 1:44:36run it, great. You will see that when we
- 1:44:40call this method accept at that point
- 1:44:43this statement will get executed. Okay,
- 1:44:45so this is how functional interfaces
- 1:44:46work. Here we provide the implementation
- 1:44:48and then when the method is actually
- 1:44:50called this implementation is executed.
- 1:44:53Okay. So that is kind of what is
- 1:44:55happening here in if present. Okay. I'll
- 1:44:57show you. So this is the implementation.
- 1:45:00I can shorten it a bit. I can remove
- 1:45:01this. I can remove this and I can since
- 1:45:05it's a single parameter so I can remove
- 1:45:07this as well. Okay. So this
- 1:45:09implementation looks like this. And it
- 1:45:11is exactly what we have added here.
- 1:45:13Okay. This is exactly what we have added
- 1:45:15here. Now the only thing is whenever
- 1:45:17this accept method will be called then
- 1:45:20this statement will be executed. Okay.
- 1:45:22So I'll just remove this. I just wanted
- 1:45:24to show you how consumer works. Okay. So
- 1:45:26here we have added the implementation.
- 1:45:28Now if the accept method will be called
- 1:45:31okay then it means that it will be
- 1:45:33executed otherwise it will not be
- 1:45:35executed. Okay simple. So what this if
- 1:45:38present is now doing here we have added
- 1:45:40a consumer implementation and it has
- 1:45:42added a check here that if the value is
- 1:45:44not null then only call this accept
- 1:45:47method. Okay you understand? So if the
- 1:45:50value is not null then only this accept
- 1:45:52method of this consumer interface has
- 1:45:54been called and that means that this
- 1:45:57statement will be only executed when the
- 1:46:00value is not null. Okay. So when the
- 1:46:02value was null it was not executed and
- 1:46:04uh when the value is not null then this
- 1:46:07was printed. Okay. So this is how if
- 1:46:09present works. Okay. So if you look at
- 1:46:11it inside it's actually quite simple.
- 1:46:13You just need to understand how this
- 1:46:15functional interfaces work and what
- 1:46:17exactly is happening behind the scene.
- 1:46:20Okay. Now or else is an option classes
- 1:46:23method that returns the value if it is
- 1:46:26present otherwise returns a default
- 1:46:28value. Okay. Let me show you. So what
- 1:46:31I'm going to do is I'll take the
- 1:46:33optional user and let's say I call this
- 1:46:36method or else okay and here I provide a
- 1:46:40new user that has some ID let's say
- 1:46:43minus one and I name it a default user
- 1:46:47okay something like this all right so
- 1:46:51this will actually return me a user
- 1:46:54okay simple let me just comment this all
- 1:46:58right so what exactly is happening
- 1:47:00We had this optional user and we wanted
- 1:47:03an actual user out of it. Okay. But the
- 1:47:06only problem is that there is a
- 1:47:08possibility that this optional does not
- 1:47:10contain anything. Okay. So what I've
- 1:47:12done here is that if it does not contain
- 1:47:14anything then in that case I have
- 1:47:17defined a default user for myself. Okay.
- 1:47:20So this or else works when it does not
- 1:47:23have anything. Okay. So I'll just simply
- 1:47:26print this and we'll understand.
- 1:47:32Okay, we have fetched a user using this
- 1:47:34user. I'm printing the name and the ID.
- 1:47:37Okay, so if the ID was two,
- 1:47:41great. Then the actual user again this
- 1:47:43ID was printed. But let's say if the ID
- 1:47:46is four
- 1:47:48then in that case
- 1:47:50this will this or else will be triggered
- 1:47:52and it will return the default user.
- 1:47:54Okay. So this default user will be
- 1:47:56returned. Okay. Simple. Now if I look
- 1:47:59inside this method you will see that
- 1:48:01it's a very simple implementation. It
- 1:48:03simply says that if the value is not
- 1:48:05null then return the exact value. If it
- 1:48:08is return if it is null then return
- 1:48:10whatever we have passed in the argument.
- 1:48:12Okay. So we pass this default user in
- 1:48:14the argument and that is why it was
- 1:48:16returned. Okay. Now or else get is an
- 1:48:20optional classes method that returns the
- 1:48:22value if it is present. Otherwise it
- 1:48:25calls a supplier to generate the default
- 1:48:28value. All right let me show you. So now
- 1:48:31what we can do is we can actually
- 1:48:33replicate this entire thing using a
- 1:48:36different method. Okay let me show you
- 1:48:38how. Instead of or else I will simply
- 1:48:41write or else get. Okay. And here
- 1:48:45instead of passing the user directly I
- 1:48:47will just simply change it to a lambda
- 1:48:49expression. Okay. Like this. That's it.
- 1:48:53All right. So if I'll just run it for
- 1:48:56two.
- 1:48:58Great. You will see that it returns the
- 1:49:00user AIT. But if I run it for four
- 1:49:06then it returns the default user. Okay.
- 1:49:08So this is what is happening here. Uh it
- 1:49:11is exactly it works the same way as or
- 1:49:13else works. The only difference is that
- 1:49:15here it takes a supplier implementation.
- 1:49:18Okay. And here we can pass on the user
- 1:49:20directly. All right. That's the
- 1:49:21difference. So what is a supplier?
- 1:49:23Supplier is a functional interface which
- 1:49:26has this abstract method named get.
- 1:49:28Okay. And it it has some return type.
- 1:49:30All right. So that is why we have to
- 1:49:32pass and pass a lambda implementation
- 1:49:35here. Okay. How can I implement a
- 1:49:37supplier? Again, it's simple. I want to
- 1:49:40supply a user. So let's say I take a
- 1:49:45parenthesis. Okay, since there is no
- 1:49:47argument in this get, so I'll simply
- 1:49:49take parenthesis without any argument
- 1:49:51and we can just return a user. Okay. So
- 1:49:55like this.
- 1:50:01All right. And that is exactly what we
- 1:50:03have done over here. Okay. So this is an
- 1:50:05implementation of this supplier. All
- 1:50:08right. Now when will this be executed?
- 1:50:10When we'll actually call S1.get.
- 1:50:13Okay. Then only this lambda gets
- 1:50:16executed. Okay. And that is what is
- 1:50:19happening here in this or else get here.
- 1:50:22What they are doing is if the value is
- 1:50:24not equal to null then simply return the
- 1:50:26value. But if it is null then call the
- 1:50:29get method on this implementation. Okay.
- 1:50:32So this means that when the value is not
- 1:50:34null then the exact user will be
- 1:50:36returned. But if it is null then this
- 1:50:38lambda implementation that we have
- 1:50:40provided here it will trigger the get
- 1:50:42method which means that this statement
- 1:50:45will be executed and a default user will
- 1:50:48be returned. All right. This is how this
- 1:50:51or else get works.
- 1:50:53Now or else throw is an optional classes
- 1:50:55method which returns the value if it is
- 1:50:58present otherwise it simply throws an
- 1:51:00exception. All right, let me show you.
- 1:51:03So what I'll do is I'll simply take the
- 1:51:05optional user and I'll call this method
- 1:51:09or else throw. Okay, and here we pass on
- 1:51:12the implementation where we are just
- 1:51:15returning some exception. Let's say I
- 1:51:17want to return a runtime exception
- 1:51:20that says user not found. Okay,
- 1:51:23something like this. All right, so this
- 1:51:26time I'm saying that if I find the user
- 1:51:28then it should just simply return that
- 1:51:30user. Otherwise, we should throw some
- 1:51:32exception. Okay. So, we have a method
- 1:51:34for that as well. All right. So, let me
- 1:51:38just run it for two.
- 1:51:42Okay. All right. I have not added the
- 1:51:44print statement. Just a second.
- 1:51:51Yeah. So, if I run it for two,
- 1:51:55then the value gets retrieved. But if I
- 1:51:59run it for four in that case, we get the
- 1:52:03exception. Okay, that user not found. So
- 1:52:06it provides you a provision that if if
- 1:52:08you want to throw some exception that
- 1:52:11also you can do. Okay. Now inside this
- 1:52:14method also it it simply takes a
- 1:52:16supplier and if the value is not null,
- 1:52:18it returns the value. If the value is
- 1:52:20null, then it calls the get method on
- 1:52:23this supplier. Okay. And if you'll look
- 1:52:25at it carefully, this is a supplier
- 1:52:27implementation. Okay. The same as we
- 1:52:30discussed over here in or else get.
- 1:52:33All right. Now, offnable is an optional
- 1:52:36classes method which is used to create
- 1:52:38an optional that holds the value if it
- 1:52:41is non-null. Otherwise, it returns an
- 1:52:44empty optional. All right. Let's see.
- 1:52:46So, we have already seen that we can use
- 1:52:49optional dot off to create an optional.
- 1:52:53Okay. If I pass a user inside this
- 1:52:56optional doof, then an optional of this
- 1:52:58user will be created. Okay. Similarly,
- 1:53:01we can also use optional dot of null
- 1:53:03level. Okay. Like this. Both perform the
- 1:53:06same action. What they do is they
- 1:53:09actually create an object of optional.
- 1:53:11Okay. Now the difference is that in case
- 1:53:14of this off method, we actually cannot
- 1:53:17take a null value. Okay. So let's say
- 1:53:20let me just comment this out for now.
- 1:53:23Let's say I have a user
- 1:53:27user which is null. Okay, by chance it's
- 1:53:31null and if I pass pass it inside this
- 1:53:34method then it will throw me some
- 1:53:36exception. But in case of offnullable
- 1:53:39there won't be any problem. Okay, that
- 1:53:41is the only difference. So I'll show
- 1:53:42you. Let's say I just run it.
- 1:53:46Great. So you can see that we get a null
- 1:53:47pointer exception. Why? Because if I go
- 1:53:50inside this you will see that here it
- 1:53:52calls a new optional and in this private
- 1:53:55constructor we have this condition that
- 1:53:58says objects do require non-null. Okay.
- 1:54:01So it says that a non-null object is
- 1:54:03required. So if the object will be null
- 1:54:05then it will throw a null pointer
- 1:54:06exception. Okay. And that is what
- 1:54:08happened when we call when we use this
- 1:54:11user along with optional.off. Okay. But
- 1:54:15in case of a of offnull labelable if I
- 1:54:17just run it there will be no issue.
- 1:54:20Okay. Because offnully has that
- 1:54:23handling. So it simply says that if the
- 1:54:25value is is null then simply return
- 1:54:28optional empty. Okay. So it is returning
- 1:54:30optional empty. All right. So that is
- 1:54:34the only difference. Okay. Hello and
- 1:54:37welcome to my channel the curious coder.
- 1:54:40Today we'll be learning about one of the
- 1:54:42most powerful and game-changing features
- 1:54:44introduced in Java 8 stream API. Trust
- 1:54:48me after this video your entire approach
- 1:54:51to coding will change. So what exactly
- 1:54:54is a stream? A stream is a sequence of
- 1:54:57elements that supports various
- 1:54:59operations to process data. Formally
- 1:55:02speaking, stream is an interface
- 1:55:04introduced in Java 8 that provides
- 1:55:07methods to perform data processing in a
- 1:55:09functional style programming. Now, don't
- 1:55:12worry if that sounds a bit heavy. So
- 1:55:14far, what do we know? Stream is an
- 1:55:17interface. It has a lot of methods. Its
- 1:55:20purpose is to process data and it does
- 1:55:23so in a functional programming style.
- 1:55:26All right. Now, what does all of this
- 1:55:28actually mean? Let's understand it
- 1:55:30practically. So as I told you stream is
- 1:55:33actually an interface and as you can see
- 1:55:36it contains a lot of methods. Okay. We
- 1:55:38have this filter map map to int.
- 1:55:41Similarly there are a lot of other
- 1:55:43methods inside this interface. Okay. So
- 1:55:46if we look at this method filter you'll
- 1:55:49see that it takes a parameter of this
- 1:55:52type predicate. All right. So if I go
- 1:55:54inside this you'll see that predicate is
- 1:55:57actually a functional interface. Okay.
- 1:56:00Now what are functional interfaces? Any
- 1:56:02interface that contains a single
- 1:56:04abstract method is called a functional
- 1:56:06interface. Okay. So this interface
- 1:56:09predicate contains only one abstract
- 1:56:11method which is this one boolean test.
- 1:56:14That is why it can be treated as a
- 1:56:16functional interface. All right. Now
- 1:56:18when I say that stream supports
- 1:56:20functional shell programming, this is
- 1:56:22what I actually mean that the methods
- 1:56:24inside stream they work with these
- 1:56:27functional interfaces. Okay. So you can
- 1:56:29see that filter works with predicate.
- 1:56:31Similarly, map works with this interface
- 1:56:33function. Okay, which is another
- 1:56:35functional interface. All right. So this
- 1:56:37is how these this is how stream supports
- 1:56:39functional style programming that all
- 1:56:41that the methods inside this interface
- 1:56:44actually work with functional
- 1:56:45interfaces.
- 1:56:47Okay. Now what's the benefit of having
- 1:56:49these functional interface? The benefit
- 1:56:51is that we can implement these
- 1:56:54interfaces, we can implement these
- 1:56:56methods in a very short and concise
- 1:56:58syntax. Okay, that is what we call
- 1:57:00lambda expressions. All right, let me
- 1:57:03show you. So what I'm going to do is in
- 1:57:06reality we have three to four very
- 1:57:08important functional interfaces that we
- 1:57:10use quite a lot along with streams. So
- 1:57:12one by one I'm going to show you how we
- 1:57:14can implement those using lambda
- 1:57:16expressions. Okay. And that would be
- 1:57:18very helpful in the video when we'll
- 1:57:20move ahead. Okay. So, first I'll start
- 1:57:23with predicate. What we are trying to
- 1:57:25achieve is that we want to implement
- 1:57:26this method test. Okay. It takes a
- 1:57:28single argument and it returns a boolean
- 1:57:31value. Okay. So, how will I do that?
- 1:57:33I'll simply write predicate.
- 1:57:36Let's say it is of the type integer.
- 1:57:38Okay. So, this means that whatever I
- 1:57:40pass here is actually of the type of
- 1:57:42this argument. Okay. So, let's say I
- 1:57:44name my object something P1. Now from
- 1:57:47here I will start the lambda expression.
- 1:57:49All right. So how does it work? Since
- 1:57:52this method contains a single argument
- 1:57:54so I will also take a single argument
- 1:57:56like this and I can name it anything.
- 1:57:59Let's say I name it num. After that I
- 1:58:02will have to add a arrow operator. Okay.
- 1:58:04Then followed by the curly braces and a
- 1:58:07semicolon. All right. This is the
- 1:58:09syntax. Okay. So whatever I write
- 1:58:12between these two curly braces actually
- 1:58:14represents the implementation of this
- 1:58:16method test. Okay. So whatever I am
- 1:58:19writing here is representing the
- 1:58:20implementation. Okay. Now what do I need
- 1:58:23to implement? Let's say I simply want to
- 1:58:26check whether this number is even or
- 1:58:28not. Okay. So what I can do? I can
- 1:58:30simply write if num mod 2 is zero then
- 1:58:34return true.
- 1:58:36Okay. Otherwise return false.
- 1:58:40All right. So this is how I have
- 1:58:42implemented this predicate interface.
- 1:58:44Okay. It is very simple, very short and
- 1:58:46very concise. And this is what we call
- 1:58:49lambda expressions. Okay. Now I could
- 1:58:52have simplified it a bit more. Instead
- 1:58:54of writing all this, I could have simply
- 1:58:56returned return num mod 2 equal to zero.
- 1:59:01Okay. We could simplify the if else like
- 1:59:03this. And now that's the beauty of
- 1:59:06lambda expression that when the
- 1:59:08implementation is of a single line.
- 1:59:10Okay, in this case the implementation is
- 1:59:11of a single line. So I can remove this.
- 1:59:14I can remove this curly brace and I can
- 1:59:16remove the semicolon. And I also don't
- 1:59:19need the return keyword. Okay. So I can
- 1:59:22simply write it like this. All right.
- 1:59:24That's the whole implementation. We just
- 1:59:26have to write num which is the argument.
- 1:59:29Okay. Which is this argument here. And
- 1:59:32then I'm saying num mode 2 equal to
- 1:59:34equal to0. So this means that I want to
- 1:59:37return true if the number is even
- 1:59:39otherwise I want to return false. Okay.
- 1:59:42Similarly let's see some other
- 1:59:44interfaces. We have this interface
- 1:59:46function. Okay. Here it it takes an
- 1:59:48argument but it does not return boolean.
- 1:59:51Okay. It can return anything. All right.
- 1:59:53Whatever you pass here it can return
- 1:59:55anything. All right. Let's see. I'll
- 1:59:58simply let's say I take integer and
- 2:00:01integer only. Okay,
- 2:00:06let me call it f_sub_1. So this time
- 2:00:08again here we have a single argument.
- 2:00:11Okay, so I will just take a single
- 2:00:13argument. Let's say I take it as number
- 2:00:14only. And I want to return I want to
- 2:00:18return the square of this number. Okay,
- 2:00:21like this num into num. All right. So
- 2:00:26this is how I can provide an
- 2:00:27implementation. Now again it is of a
- 2:00:29single line. So I will remove this. I'll
- 2:00:31remove this and I will remove the
- 2:00:32semicolon and also the return keyword.
- 2:00:35Okay. So this is how I can implement
- 2:00:37this interface function. All right. Now
- 2:00:39we have two more interfaces. There is
- 2:00:42this interface consumer. Okay. It is
- 2:00:44also very important. So it is a
- 2:00:45functional interface and you will see
- 2:00:47that it takes a single argument but it
- 2:00:49has no return type. All right. So I'll
- 2:00:52give one example.
- 2:00:54Let's say I'll take integer only again
- 2:00:58and what I need to do is it takes a
- 2:01:00single argument. Okay. So again I'm
- 2:01:02taking a number and let's say I just
- 2:01:06simply want to print this number. Okay.
- 2:01:08We don't have to return anything from
- 2:01:10here. So I'll simply print it.
- 2:01:12System.out.print
- 2:01:14number. All right. Like this. Again it's
- 2:01:17a single statement. I'll remove this and
- 2:01:19I'll remove these two. And this I can
- 2:01:22write over here like this. Similarly,
- 2:01:25there is one more. There is an interface
- 2:01:27supplier. Okay? So, it's kind of
- 2:01:29opposite to consumer. What it does is it
- 2:01:32does not take any argument, but it has a
- 2:01:34return type. All right? So, this can be
- 2:01:37used. Let's say the return type I want
- 2:01:40to give as integer. Okay. So, I'll
- 2:01:42simply say S1. Now, now this time I
- 2:01:45don't have any argument. So, I'll simply
- 2:01:46write it like this followed by this
- 2:01:48arrow operator. And let's say I want to
- 2:01:52return some value. Okay, simply I want
- 2:01:54to return math dot random. I'll return a
- 2:01:58random number. Okay, and let me cast it
- 2:02:01to int.
- 2:02:02All right, that's it. Simple. Now again
- 2:02:06uh now why did I do that? Because this
- 2:02:07does not take any parameter but it has
- 2:02:09some return type. Okay, that's why I
- 2:02:11returned some value. And in case of a
- 2:02:13consumer, it took a parameter but it
- 2:02:15didn't have any return type. Okay, so
- 2:02:17that's how we implemented it. All right.
- 2:02:20So again in this case it's a single
- 2:02:22line. I'll remove this. I'll remove this
- 2:02:25and I'll also remove the return
- 2:02:27statement. Okay. So we can simply
- 2:02:29implement it like this. All right. So
- 2:02:31these are four interfaces that I
- 2:02:33implemented. You will see that it's a
- 2:02:35very simple implementation. We can do
- 2:02:37all this in a single line. Now so far we
- 2:02:40have seen that stream is an interface.
- 2:02:42It has a lot of methods and it uses
- 2:02:45functional programming. Now let's
- 2:02:47understand how it actually processes the
- 2:02:49data. So this is the data that I have
- 2:02:52taken. It's a list of integers and
- 2:02:55somehow I want to convert this list into
- 2:02:57a stream. Okay. So how can I do that?
- 2:03:00It's very simple. We can just call this
- 2:03:02method named stream. All right. Now many
- 2:03:06developers get confused here. The
- 2:03:08confusion is that whether stream is an
- 2:03:10interface or whether stream is a method.
- 2:03:13Okay. So stream is actually an interface
- 2:03:15only. But we also have a method named
- 2:03:18stream which we use when we want to
- 2:03:20convert any collection into a stream.
- 2:03:23Okay. So I'll repeat stream is also a
- 2:03:26method which we use when we want to
- 2:03:28convert any collection into a stream.
- 2:03:30Okay. So you'll see that the return type
- 2:03:32of this method is actually stream only
- 2:03:35which is the same interface that we just
- 2:03:36saw. Okay. Now this stream is inside
- 2:03:40this interface named collection. Now
- 2:03:42what is collection? collection is
- 2:03:44actually a parent interface of many data
- 2:03:46structures. Okay, so list is one of
- 2:03:49those data structures, right? I'll show
- 2:03:51you. You can see that one of the
- 2:03:53implementation is list. Similarly, we
- 2:03:56have set as well and we have other data
- 2:03:59structures as well. Okay, we have dq and
- 2:04:01many more. All right, so any any
- 2:04:04collection can be converted into a
- 2:04:06stream just by calling this method
- 2:04:08stream. All right, so that is what we
- 2:04:11have done. We have converted this list
- 2:04:13into a string by calling the method
- 2:04:14stream. Okay, so I'll just extract a
- 2:04:17variable.
- 2:04:20Great. Okay, so we got a string. Now
- 2:04:23what can we do? We can perform various
- 2:04:25operations. Okay, so let's say I'll
- 2:04:27start with a very simple operation. We
- 2:04:29just want to print all the elements.
- 2:04:31Okay, I want to print all the elements.
- 2:04:33So for that I'll call a method named for
- 2:04:37each. Okay. And here I'll say that for
- 2:04:40every element we just have to print the
- 2:04:44element. All right. Like this. Cool. So
- 2:04:48you'll see that this is actually a
- 2:04:50lambda expression. And if you look at it
- 2:04:53carefully, this for each method takes a
- 2:04:55consumer and this lambda expression is
- 2:04:58actually an implementation of consumer
- 2:05:00only. Okay, you can see here that this
- 2:05:02is what we implemented a little while
- 2:05:04ago. All right. So that is how we can
- 2:05:06write it. Now if I'll just run it.
- 2:05:10Great. You can see that all the elements
- 2:05:12are printed. Okay. So this is one of the
- 2:05:15method which loops on every element and
- 2:05:17we can just provide a consumer
- 2:05:19implementation here and that will be
- 2:05:21executed. Okay. So we have written a
- 2:05:23print statement that is why all the
- 2:05:25elements are printed one by one. Okay.
- 2:05:28Now I'll take another example. Let's say
- 2:05:30this time I want to print only the even
- 2:05:32elements. Okay. Only the elements that
- 2:05:35are even that I want to print. So what I
- 2:05:37can do is okay first one thing that I
- 2:05:39want to do here I'll not write it like
- 2:05:41this okay we don't need this stream
- 2:05:43variable we can do it directly like this
- 2:05:45one list dotstream
- 2:05:47dot for each okay generally we do it
- 2:05:50like this okay and I'll tell you why all
- 2:05:52right in a little while we'll get to
- 2:05:54know why that we why don't we extract it
- 2:05:56in a variable okay for now we'll do it
- 2:05:59like this now let's say I want to print
- 2:06:01only the even elements okay so what can
- 2:06:04I do list dotstream dot filter. So we
- 2:06:08have another method filter. So it will
- 2:06:10take every element and we can add some
- 2:06:13condition. Let's say the condition is
- 2:06:15element mode 2 equal to equal to zero.
- 2:06:18Okay. And that's it. So what I'm saying
- 2:06:20here is that this filter takes a
- 2:06:22predicate and this is how we give a
- 2:06:24predicate implementation. So here we can
- 2:06:26pass on a condition and what filter will
- 2:06:29do is that it will filter out all the
- 2:06:31elements who which does not satisfy this
- 2:06:34condition. Okay. So only for the
- 2:06:36elements that will return true for this
- 2:06:38those will be kept in the stream and
- 2:06:40rest all will be removed. Okay. And one
- 2:06:43more thing this filter actually returns
- 2:06:45a stream only. Okay. So we still have a
- 2:06:48stream. So we can still again call
- 2:06:49another method. So now what I'll call is
- 2:06:51I'll call for each because I also want
- 2:06:54to print the elements. Okay. So again
- 2:06:58I'll say for every element I just simply
- 2:07:01want to print the elements. All right.
- 2:07:04Like this. So what filter did it will
- 2:07:07filter out all the elements that does
- 2:07:09not satisfy this condition. Okay. So
- 2:07:11only the elements that satisfy this
- 2:07:13condition will remain. So after that
- 2:07:15only the even elements will be present
- 2:07:17and here we are printing those elements.
- 2:07:20Okay. Okay. So I'll show you. Let me
- 2:07:21just comment this out.
- 2:07:25Great. So you will see that 2 4 and six
- 2:07:28have been printed. Okay. And as I told
- 2:07:31you this filter takes a predicate. Okay.
- 2:07:33This you should know for for interview
- 2:07:35point of view. Okay. Cool. Now I'll
- 2:07:38perform another operation. What I want
- 2:07:40to do is I'll just simply take
- 2:07:44a stream and I want to print the square
- 2:07:47of all these elements. Okay. So how can
- 2:07:50I do that? Again I'll call a method
- 2:07:53named map. Okay. And it takes every
- 2:07:57element and it maps it into its square.
- 2:08:01Okay. Like this.
- 2:08:03All right. And then I want to print it
- 2:08:06because map also returns a string. So I
- 2:08:08can still call another method. So I call
- 2:08:10this method for each. And here I'm just
- 2:08:14printing every element. Out. Element.
- 2:08:19Okay. Now here also we have given a
- 2:08:22lambda expression and this one is
- 2:08:25actually of the argument is function and
- 2:08:27this expression is of the type is is a
- 2:08:30lambda expression of function. Okay
- 2:08:33good. So now this map what this map has
- 2:08:35done is that it will take every element
- 2:08:37and it will convert it into its square.
- 2:08:39Okay. And then here we are printing
- 2:08:41those. All right. So I'll run it again.
- 2:08:44Okay. Let me just comment this.
- 2:08:48Great. So you'll see that all the
- 2:08:50elements are printed. One, all the
- 2:08:52squares are printed. 1 4 9 16 25 36.
- 2:08:56Okay. Now I could have also added a
- 2:08:58filter here. What I can do is I'll just
- 2:09:01simply change the example. First I I
- 2:09:03want to filter all the elements and I
- 2:09:05only want to keep the even elements.
- 2:09:07Okay, like this.
- 2:09:10And after keeping all the even elements,
- 2:09:12I want to map it to their square and
- 2:09:15then I want to print them. Okay. So that
- 2:09:17also I can do. Great. 4 16 and 36. All
- 2:09:21right. So first I called a filter that
- 2:09:24filtered the elements. Now since it
- 2:09:26still returns a stream. So I can call
- 2:09:28another method. That's why I called map.
- 2:09:30Now map has mapped these elements to
- 2:09:32their square. Now it also returns a
- 2:09:34stream only. This map also returns a
- 2:09:37stream. So I moved ahead and I called
- 2:09:39for each. And now using for each I am
- 2:09:42printing all the elements. Okay. of
- 2:09:44filter is taking a predicate
- 2:09:45implementation. Map is taking a function
- 2:09:48implementation and for each is taking a
- 2:09:50consumer implementation.
- 2:09:52All right, I'll take another example.
- 2:09:56Let's say we have a list of employees.
- 2:09:58Employee is a class which has these
- 2:10:00fields ID, name and department. Now in
- 2:10:03an interview, someone asks you to print
- 2:10:06the name of all the employees whose
- 2:10:08department is tech and do this using
- 2:10:11functional programming. So how will you
- 2:10:13do this? Let's find out. So we have this
- 2:10:16list of employees inside this class
- 2:10:18employee. We have ID, name and
- 2:10:21department. And we have created getter
- 2:10:23methods for all these fields. Okay. I
- 2:10:25have a get ID, get name, and get
- 2:10:27department. Okay. Now what I want is I
- 2:10:31want all the employees whose department
- 2:10:33is tech and I only want to print the
- 2:10:36name of those employees. Okay. Only want
- 2:10:38to print the name. All right. So how can
- 2:10:41I do that? First I'll take the list and
- 2:10:45convert it into a string. Okay, then
- 2:10:48I'll just simply filter out all the
- 2:10:51employees whose department is tech.
- 2:10:53Okay, so I'll take an argument employee
- 2:10:56and I'll simply add a condition that
- 2:10:59employee dot get department dot equals
- 2:11:03tech.
- 2:11:05Okay, this is how I can add a predicate
- 2:11:07condition. Okay, filter takes a
- 2:11:09predicate and the condition says that
- 2:11:12all the employees whose department
- 2:11:13equals to tech those should be kept and
- 2:11:16rest all should be filtered out. Okay.
- 2:11:19Now what I want is that as of now in
- 2:11:22this list we only have employees. Okay.
- 2:11:24You can see that here also we are
- 2:11:25looping on employees and using this
- 2:11:27employee reference we have called this
- 2:11:29get department method. Okay. But in the
- 2:11:31end what I want is I want the name of
- 2:11:34those employees. Okay. I don't want the
- 2:11:36employees. I want to print the name. So
- 2:11:38what I can do is I can just take a map.
- 2:11:42I can call this method map. And we have
- 2:11:45employee but I will map it to employee
- 2:11:48dot get name. Okay. And now I will just
- 2:11:53print all these names like this.
- 2:11:57All right. System.out.t
- 2:12:00print ln name. Okay. So what we did was
- 2:12:05first we converted the list into a
- 2:12:06stream. Then we filtered out all the
- 2:12:10employees whose department is equal to
- 2:12:12tech. Then here we mapped the employee
- 2:12:15into its name. Okay. So now after that
- 2:12:18the list this list the stream has become
- 2:12:20a stream of names. Okay. So here when
- 2:12:23I'll loop on it using for each here I
- 2:12:25will get a name. Okay. You can see that
- 2:12:27this is string and this is of the type
- 2:12:28employee. And then here I'm just
- 2:12:30printing all those names. All right. So
- 2:12:33this is how filter map and for each
- 2:12:35works. Okay. So I'll show you this as
- 2:12:37well.
- 2:12:40Great. So you will see that Amit, Rahul,
- 2:12:43Vikas and Arjun. So Amit, Rahul, Vikas
- 2:12:46and Arjun all these have their
- 2:12:48department as tech. And you'll see that
- 2:12:50not all the employees are printed. Only
- 2:12:52their names are printed. Okay.
- 2:12:56Now let's look at another important
- 2:12:58method. Collect. Collect is a method
- 2:13:01inside stream interface which takes all
- 2:13:04the elements from the stream and
- 2:13:06transform them into some final result
- 2:13:09like a list set or a map. All right. So
- 2:13:12if you want to convert your stream back
- 2:13:14into a data structure, you can use
- 2:13:16collect. Okay. So let's see how. So I'll
- 2:13:20take the same example. Initially I had a
- 2:13:23list of employees. I converted it into a
- 2:13:26stream. Then I did some filtering, some
- 2:13:29mapping and in the end I'm looping on
- 2:13:31this stream and I'm printing all the
- 2:13:34names. Okay, but let's say I don't want
- 2:13:37to print all the names. Instead, I want
- 2:13:39this stream to be converted back into
- 2:13:42some data structure. Okay, so after the
- 2:13:45filtering and the mapping, I don't want
- 2:13:46to print right away. I just want to
- 2:13:48convert it back into some data
- 2:13:50structure. Okay, so for that I can use
- 2:13:53the method collect. Okay. Now let's say
- 2:13:56I want it in the form of a list. So for
- 2:13:58that I will have to write it like this
- 2:14:01collectors dot to list. Okay. And here I
- 2:14:05can add it in some variable.
- 2:14:08All right.
- 2:14:10Let's say I say names.
- 2:14:14All right. So eventually I will get a
- 2:14:16list of string. Okay. So I had a list of
- 2:14:18employee. I filtered all the employees
- 2:14:20whose department were tech. Then I
- 2:14:23mapped it into employee.get. get name.
- 2:14:25So at this point of time when when this
- 2:14:27map is called now we have a stream of
- 2:14:30names. Okay, a stream of strings and
- 2:14:32then we convert it back into a list.
- 2:14:34That is why we get a list of string.
- 2:14:37Okay, we get only the names because we
- 2:14:39have mapped the employee to employee.get
- 2:14:41name. Okay, so this is how we can use
- 2:14:44collect. Now if I wanted it to be in the
- 2:14:47form of set, so for that I could have
- 2:14:50called to set and in this case it would
- 2:14:53be a set of string.
- 2:14:56Okay, cool. Now I will take one more
- 2:14:59example. Let's say I want to convert
- 2:15:01this into a map. Okay, now map is a bit
- 2:15:05tricky because for map you will have to
- 2:15:07define the key as well as the value.
- 2:15:10Okay, so you will have to define what
- 2:15:12key you want to take and what value you
- 2:15:14want to take. So let's say we have this
- 2:15:16list of employees and I want to convert
- 2:15:18it into a map whose key is the employee
- 2:15:20ID and value is the employee name. Okay,
- 2:15:24let's say I want to convert it into
- 2:15:25something like that. Okay, so how can I
- 2:15:28do it? I'll take the employee. I'll
- 2:15:30convert it into a stream. I'll call the
- 2:15:33method collect and here I can just write
- 2:15:36collectors
- 2:15:39dot to map. Okay, but it does not end
- 2:15:43here like this because now you will have
- 2:15:45to define what is the key and what is
- 2:15:47the value. Okay, that is why this two
- 2:15:49map takes two arguments. The first one
- 2:15:52defines the key and the second one
- 2:15:54defines the value. Okay, and you can see
- 2:15:57that both these arguments are actually
- 2:15:59of the type function. Okay, so in both
- 2:16:01these arguments we have to provide
- 2:16:03lambda expressions and those lambda
- 2:16:05expressions the first one will represent
- 2:16:07the key and second one will represent
- 2:16:09the function. Okay, lambda expressions
- 2:16:11will be of the interface function. All
- 2:16:13right, so we have already seen how we
- 2:16:15implement function. Let's say I want the
- 2:16:18key to be integer. Okay, and I have my
- 2:16:23argument as employee. So I'll take it
- 2:16:25like this. Function employee, integer.
- 2:16:29Let's call it f_sub_1. Okay. So I have
- 2:16:32employee that I know and
- 2:16:36I want to convert it into I want to
- 2:16:39fetch the key out of it. Okay. So I want
- 2:16:41to map it into the ID. Sorry, I want to
- 2:16:43fetch the ID out of it. Okay. So this
- 2:16:45will be my first lambda expression.
- 2:16:48Similarly, I'll create one more. I have
- 2:16:51a employee and from this employee, I
- 2:16:53want the name. Okay, that will be my
- 2:16:56value. So I have this employee
- 2:17:00and I'll simply do employee dot get
- 2:17:03name. Okay, like this. So this is the
- 2:17:07first lambda expression which will
- 2:17:08represent the key and this is the second
- 2:17:10lambda expression which will represent
- 2:17:12the value. All right. So here I will
- 2:17:14just add f_sub_1, f_sub_2 like this. Got
- 2:17:19it? So in this method to map we pass on
- 2:17:22two function arguments and first one
- 2:17:25represent the key and second one
- 2:17:26represents the value. Okay. So the key
- 2:17:28will be ID and the value will be name.
- 2:17:31All right. Let's see. I'll just extract
- 2:17:33a variable.
- 2:17:37Great. So let me call it employee map.
- 2:17:43Okay. So you'll see that the key is of
- 2:17:45the type integer and value is of the
- 2:17:47type string.
- 2:17:49So now I'll just print this map to show
- 2:17:52you what exactly is the final result.
- 2:17:55Okay. So we have this employees map. If
- 2:17:57I simply just run it.
- 2:18:01Great. So you will see that we have a
- 2:18:03map where key is the id. Okay. We have
- 2:18:06all the ids and value is their
- 2:18:09corresponding names. Okay. So against
- 2:18:11one we have a similarly against two we
- 2:18:13have nha and so on. Okay. So this is how
- 2:18:17we can use collect along with these
- 2:18:19various functionalities. Collectors dot2
- 2:18:22list to map and to set. Now let's
- 2:18:25understand another very important
- 2:18:26concept. Intermediate operations and
- 2:18:29terminal operations in streams. First
- 2:18:32let's talk about intermediate
- 2:18:33operations. These are the operations
- 2:18:36that transform or process the data in a
- 2:18:38stream but they do not actually produce
- 2:18:41a result of their own. Some common
- 2:18:43examples of intermediate operations are
- 2:18:46filter, map, sorted, etc. Terminal
- 2:18:50operations are the operations that
- 2:18:52trigger the execution of the stream and
- 2:18:54produce a final result. Examples of
- 2:18:57terminal operations include collect, for
- 2:18:59each, count, etc. All right, let's see
- 2:19:02it in practical. So I have taken the
- 2:19:05same example. We have a list of
- 2:19:07integers. We convert them into a string.
- 2:19:10Then we apply a filter and we keep all
- 2:19:13the even elements. Then we provide a
- 2:19:15map. We apply a map and we convert these
- 2:19:18elements into its square. Okay. Then we
- 2:19:22call for each and we print all the
- 2:19:24elements. All right. So here filter and
- 2:19:27map are intermediate operations and for
- 2:19:30each is a terminal operation. Okay. Why?
- 2:19:33Because filter returns a stream. And
- 2:19:35that is why even after calling this
- 2:19:37method, I can still do further
- 2:19:40processing. Okay, because I still have a
- 2:19:42string. Similarly, map also returns a
- 2:19:44stream. So that is why I was able to
- 2:19:46call this method for each because after
- 2:19:50calling this method, I still had a
- 2:19:51string. Right? So that is why these are
- 2:19:54called intermediate operations. But for
- 2:19:56each actually does not return anything.
- 2:19:59That is why it is a terminal operation.
- 2:20:01Okay? because it will end the stream and
- 2:20:04now we cannot do any further processing.
- 2:20:07Okay. Similarly, we have other examples.
- 2:20:10I have a method named count. Okay. So,
- 2:20:13this count returns a long value. But
- 2:20:16again, it does not return a stream.
- 2:20:18Okay. So, when we call this method, the
- 2:20:20stream has been ended. All right. Now,
- 2:20:23what's this method used for? It actually
- 2:20:25returns the count of all the elements
- 2:20:27that are remaining in the stream. Okay.
- 2:20:30So I'll just print this long count.
- 2:20:36All right. So it will actually return
- 2:20:38the count if we just print this.
- 2:20:44Great. So the count is three because
- 2:20:47after we applied the filter only three
- 2:20:49elements were remaining. Okay. Count is
- 2:20:52another terminal operation. Similarly we
- 2:20:54have collect. Collect is also another
- 2:20:57terminal operation because this also
- 2:21:01ends the stream and it converts the
- 2:21:03stream back into some data structure.
- 2:21:05Okay, like in this case we have
- 2:21:07converted it back into some list.
- 2:21:10All right, list of integer. Let's call
- 2:21:13it a final list.
- 2:21:15Okay, now there is a very important
- 2:21:17interview concept that comes from this
- 2:21:19topic. The thing is that once a stream
- 2:21:22is used, it cannot be reused.
- 2:21:25Basically once you call a terminal
- 2:21:28operation on a stream then you cannot
- 2:21:30reuse a stream. Okay, let me show you
- 2:21:33what I what I'm trying to say. If you
- 2:21:36remember initially we just took this
- 2:21:38list stream and we extracted it in some
- 2:21:42variable okay like this and then let me
- 2:21:46call it just s okay and then we applied
- 2:21:49filter or map or anything on it. Okay,
- 2:21:51we can do it like this also. I know we
- 2:21:53can just uh take call this method
- 2:21:55stream. We can add it in some variable
- 2:21:58and then we can call these methods.
- 2:22:00Okay, so let me run it like this. Great.
- 2:22:03So you will see that there is no issue
- 2:22:04in this. Okay, but let's say let's say I
- 2:22:09took this s again and I called
- 2:22:13s dot
- 2:22:15let's say anything I called s do. Okay.
- 2:22:17Again and I'm trying these elements and
- 2:22:22I write anything element
- 2:22:24mod 3 equal to equal to0. Let's say this
- 2:22:27is the condition that I want to give.
- 2:22:29Okay. So now if I run my project again.
- 2:22:33Yes. So we will have some exception and
- 2:22:36the exception says that stream has
- 2:22:38already been operated upon or closed.
- 2:22:41All right. So what I mean by that is
- 2:22:43that when we had a stream we can perform
- 2:22:46as many intermediate operations as we
- 2:22:48want but after we call a terminal
- 2:22:50operation then the stream has been
- 2:22:52closed. So you cannot use this variable
- 2:22:55again to call another intermediate
- 2:22:57operation. Okay that will give you an
- 2:22:59error. That is why it is not recommended
- 2:23:02that you extract this in some variable
- 2:23:04because it this kind of code is possible
- 2:23:07and it will throw you some exception.
- 2:23:09Okay. because at this point at line
- 2:23:11number 20 the stream was actually
- 2:23:13closed. Now if you will try to call
- 2:23:15another intermediate method it will give
- 2:23:17you some exception. Okay. So this is a
- 2:23:20very important interview question that
- 2:23:22is asked a lot. Okay. So you should know
- 2:23:24about this. All right. So I'll just
- 2:23:26change it back to list.stream.
- 2:23:29list stream like this. Okay. Let me just
- 2:23:32remove this. Great.
- 2:23:35All right. So at this point the stream
- 2:23:37has been ended. Okay. Now there is one
- 2:23:39more interview question that comes from
- 2:23:41this topic. It says that all the
- 2:23:43intermediate operations are lazy loaded
- 2:23:46and the terminal operations are eagerly
- 2:23:48loaded. Okay. So what exactly do I mean
- 2:23:51by that by lazy loading? I mean that
- 2:23:54these methods are not called until this
- 2:23:57ter until we add a terminal operation.
- 2:24:00Okay, I'll show you this. Let's say if I
- 2:24:02just remove this operation and I end it
- 2:24:04here. Okay, I just write list.tra.filter
- 2:24:08filter.m map. Okay. And uh let me just
- 2:24:11change the implementation a little bit.
- 2:24:14Okay. I'll take a completely different
- 2:24:16example altogether. All right. Let me
- 2:24:19remove this. So I have a list. I take a
- 2:24:22stream. Let's say I call a method
- 2:24:24filter. Okay. So I'm taking every
- 2:24:27element
- 2:24:29and I provide some implementation here.
- 2:24:31Okay. So here I am writing a print
- 2:24:33statement as well just to show it to
- 2:24:35you.
- 2:24:36I'm saying filter method called. Okay.
- 2:24:42And I'm saying if element mod 2 equal
- 2:24:46equal to0 return true. Okay. If you
- 2:24:50remember it, we did it like this
- 2:24:52initially. Okay. Now it's not a single
- 2:24:55line implementation. So that's why I
- 2:24:57cannot remove these curly braces. Okay.
- 2:25:00Similarly, I'll call do map. And here as
- 2:25:03well, I'll do kind of a same thing. I'll
- 2:25:05take an element
- 2:25:08and let's say I write another print
- 2:25:11statement that says
- 2:25:14map method called
- 2:25:17okay and my implementation is that I am
- 2:25:20returning element
- 2:25:22into element
- 2:25:24okay and I just ended over here all
- 2:25:28right so I've called a filter method
- 2:25:31I've called a map method I've added
- 2:25:33print statements in their implementation
- 2:25:34ations and I've not called any terminal
- 2:25:37operation. Okay. So if I'll just run my
- 2:25:40project.
- 2:25:42Great. So you will see that these
- 2:25:45statements have not been printed. Okay.
- 2:25:48So this means that filter and map has
- 2:25:50not been called at all. Okay. It has not
- 2:25:53been executed. Now why? Because they are
- 2:25:56lazy loaded which means that they will
- 2:25:58only be executed when we'll add a
- 2:26:00terminal operation on them. Okay. So
- 2:26:02let's say now I just add a simple
- 2:26:05operation dot collect collectors dot to
- 2:26:08list. Okay. So I've done nothing else.
- 2:26:10I've just added another operation which
- 2:26:12is a terminal operation. I'm not even
- 2:26:14using this list. Okay. I've just added
- 2:26:17this method. And now if I'll run it
- 2:26:20great you will see that these these
- 2:26:23statements have been printed. So this
- 2:26:25filter and map has been called again and
- 2:26:27again like again and again because they
- 2:26:29loop on this list and filter out
- 2:26:30elements one by one. Similarly map all
- 2:26:32the elements one by one. That's why you
- 2:26:34see so many print statements. But the
- 2:26:36crux is my point is that both these
- 2:26:38methods were executed only when we add a
- 2:26:41terminal operation. Okay. So this is
- 2:26:43what lazy loading and eager loading is.
- 2:26:46Again it's a very important interview
- 2:26:48topic and you should definitely know
- 2:26:49this. All right.
- 2:26:52Now these are a few other commonly used
- 2:26:54stream methods. Let's go through them
- 2:26:57one by one and understand how each of
- 2:26:59them works.
- 2:27:01Find first is a terminal method that
- 2:27:03returns the first element of the string.
- 2:27:06All right, let's see. So I have taken
- 2:27:09this list of different names. Let's say
- 2:27:12I want to convert it into a stream and
- 2:27:15call this method find first. Okay, so
- 2:27:18this will return the first element from
- 2:27:20this list. Okay, so let me just extract
- 2:27:24it in some variable.
- 2:27:26Okay, so this actually returns an
- 2:27:28optional. So if I actually want to print
- 2:27:31it, I will have to call this method get.
- 2:27:34Okay, first dot get.
- 2:27:37All right, like this. Okay, if I run it,
- 2:27:41great. So you'll see that ammon has been
- 2:27:43printed because ammon is the first
- 2:27:45element. Okay, now generally we use this
- 2:27:48find first along with some condition.
- 2:27:50Okay. So, generally we add a filter and
- 2:27:53let's say I apply a filter that says
- 2:27:57that the string starts with
- 2:28:01let's say a okay or let me call it some
- 2:28:05other example let me take it as r all
- 2:28:08right now if I run it
- 2:28:11okay so it will return raj all right all
- 2:28:14right why because this was the first
- 2:28:16string that starts with r okay so first
- 2:28:18I applied a filter and Then as soon as
- 2:28:21it gets the first element, it will
- 2:28:23simply return it. Okay, find any is a
- 2:28:27terminal method that returns any element
- 2:28:29from the string. It is not guaranteed
- 2:28:32that it will be the first one. All
- 2:28:34right, let's see. So in this example
- 2:28:36only instead of find first, I can just
- 2:28:39simply call find any. Okay, so it is not
- 2:28:41guaranteed that it will return the first
- 2:28:43element. It can return any of the
- 2:28:45element. But generally when the list is
- 2:28:47this small and uh we are calling this
- 2:28:50find any it mostly returns the first one
- 2:28:52only. Okay. So you'll see that it
- 2:28:53returns Raj only. But there is a concept
- 2:28:56of parallel processing. Okay. Sometimes
- 2:28:58we work parallelly on the streams and at
- 2:29:01that point of time this method is used
- 2:29:03quite a lot. Okay. Because at that time
- 2:29:05it can return any result. Okay. Now any
- 2:29:09match is a terminal method which returns
- 2:29:11true if any of the element matches a
- 2:29:14given condition. So we have these names.
- 2:29:17Let's say I convert it into a stream and
- 2:29:20call the method any match. Okay. Now
- 2:29:23inside this method we can actually pass
- 2:29:25a predicate implementation. Okay. So I
- 2:29:28can pass a lambda expression with some
- 2:29:30condition. So let's say I say that if
- 2:29:32any of the name starts
- 2:29:37starts with R. Okay, this is a predicate
- 2:29:42implementation that I can add here. So
- 2:29:44what any match will do is that if any of
- 2:29:47these element satisfy this condition
- 2:29:50then it will return true. Okay. So this
- 2:29:52method actually returns a boolean. Okay.
- 2:29:55Otherwise it will return false. So I'll
- 2:29:57just extract it in some variable. Let me
- 2:29:59call it boolean value. Okay. And we'll
- 2:30:02just simply print it.
- 2:30:05All right. If I run it.
- 2:30:09Great. So it returns true because we
- 2:30:11have elements that start with R. Okay.
- 2:30:14But let's say I just write starts with
- 2:30:16Z. Okay. Now if I run it. So now it will
- 2:30:20return false because none of the element
- 2:30:22starts with zed. Right? Now all match is
- 2:30:26a terminal method which returns true if
- 2:30:29all the elements match a given
- 2:30:31condition. Okay. Let's see. So if I'll
- 2:30:34just replace it with all match. Okay.
- 2:30:37And let's say I just test it for R.
- 2:30:41Okay. So if I run it, it will return
- 2:30:44false because all math says that if all
- 2:30:47the elements would have been started
- 2:30:49from R, then only it will return true.
- 2:30:52Okay. So let's say I provide a different
- 2:30:54condition I can just simply say
- 2:30:58str.length
- 2:31:00is greater than one. Okay. So we have
- 2:31:02all the strings whose length is greater
- 2:31:03than one. Now if I run it, so now it
- 2:31:06returns true. Okay. So this is how all
- 2:31:09match works. Now none match is a
- 2:31:12terminal method which returns true if no
- 2:31:15elements match a given condition. All
- 2:31:17right. Let's see. So if I replace it
- 2:31:19with none match. So none match returns
- 2:31:22true if none of the element matches the
- 2:31:25given condition. Okay. So if I'll run it
- 2:31:27for this, it will return false because
- 2:31:30all the elements matches this condition.
- 2:31:32Let's say I change it back to strarts
- 2:31:35with R. So again you'll see that it will
- 2:31:39return false because few because most of
- 2:31:42these names start with R. Okay. In fact
- 2:31:46if I'll do it for A then also it will
- 2:31:49return false because Ammon starts with
- 2:31:51A. Okay. So it it requires that none of
- 2:31:54the elements should match that
- 2:31:55condition. Okay. So let's say I say
- 2:31:58starts with zed. Now if I'll run it. So
- 2:32:01now it returns true because none of the
- 2:32:04elements start with zed. Okay. So these
- 2:32:06were few of the methods that we we can
- 2:32:08use quite a lot. Okay. Whenever we want
- 2:32:10to fetch some boolean value in the end.
- 2:32:13Okay. Now sorted is an intermediate
- 2:32:16method which returns a string with all
- 2:32:19the elements sorted. All right. Let's
- 2:32:21see. So I have taken an unsorted list
- 2:32:25and if I simply just call numbers
- 2:32:28dotstream dot sorted. So this will
- 2:32:31return me a sorted list. Okay. But it is
- 2:32:33an intermediate method. So I will have
- 2:32:35to call a terminal operation. Let's say
- 2:32:38I call this. And now if I print all the
- 2:32:41elements.
- 2:32:43Okay. If I print all the number, you
- 2:32:46will see that these are in sorted
- 2:32:49format. Okay. So sorted is quite simple.
- 2:32:52We can just directly call it and we can
- 2:32:54sort any list. All right. Now let's talk
- 2:32:58about another interesting concept.
- 2:33:00Primitive streams in Java. Normally
- 2:33:03stream works with collection but Java
- 2:33:05also provides special streams for
- 2:33:07primitive types like int, long and
- 2:33:10double. These are called int stream,
- 2:33:13long stream and double stream. All
- 2:33:15right, let's look at them in practical.
- 2:33:18So we have this interface named int
- 2:33:20stream. If I go inside this, you'll see
- 2:33:24that it is another interface which is
- 2:33:26quite similar to stream. As you can see,
- 2:33:28it has all these methods. We have
- 2:33:30filter, we have map and we have various
- 2:33:32other methods. Okay, but this is not
- 2:33:35actually a stream. It is a different
- 2:33:37interface and it is specific for
- 2:33:40primitive data type. Okay, so int stream
- 2:33:42is specific for int only. Okay, so you
- 2:33:45we use it with some int values. Let's
- 2:33:48how can I create it? It's very simple. I
- 2:33:51can just call instream dot range. Okay,
- 2:33:54so it has this static method inside it
- 2:33:56which we can call and in the range I can
- 2:33:58just pass the range. Okay, so 1 to 5.
- 2:34:01Okay, so this means that it's a stream
- 2:34:03of 1 2 3 4 and 5. All right. Now using
- 2:34:07this end stream we can perform various
- 2:34:09operations. Let's say I want to
- 2:34:11calculate the average. Okay. So I can
- 2:34:14just call this method average. I'll just
- 2:34:17extract this variable and I'll simply
- 2:34:20print it. Okay, average dot get as
- 2:34:24double. Okay, so in the end we have to
- 2:34:26convert it into a double value. Okay, so
- 2:34:29if I run it,
- 2:34:31great. It will return the average of all
- 2:34:33the values. Okay, similarly we have a
- 2:34:36method named sum. Okay, if I want the
- 2:34:38sum of all the values, so it will return
- 2:34:41me an int value. Okay, int sum. And I
- 2:34:45can just simply print this as well. All
- 2:34:48right.
- 2:34:51Great. So you can see it gives me the
- 2:34:53sum of all the values. Okay. Similarly,
- 2:34:56we have other interfaces. We have this
- 2:34:58interface double stream. This one is
- 2:35:00meant for double values. Okay. So how
- 2:35:03can I create it? Can create it like
- 2:35:05this. Double stream dot of off. And here
- 2:35:09I can pass any value that I want. Okay.
- 2:35:12Let's say I pass random double values.
- 2:35:14So a stream of double has been created.
- 2:35:17Okay. Similarly, this also has this
- 2:35:20method average. And we have other
- 2:35:23methods as well. Okay. So I'm just
- 2:35:25showing you a few of them.
- 2:35:29Alen
- 2:35:31double stream dot average dot get as
- 2:35:34double. Okay. And we can run it and it
- 2:35:38will just give me the average of these
- 2:35:40three numbers. Okay. Similarly, we have
- 2:35:43another interface named long stream.
- 2:35:46Okay. So, this also I'll show you. We
- 2:35:47have this interface long stream. Okay.
- 2:35:50This also we can create in the same
- 2:35:52fashion. We can just say long stream dot
- 2:35:55off or we could have used a range as
- 2:35:57well. And let's say 1 2.3 4.5. Okay. We
- 2:36:02can give different values here. Okay. We
- 2:36:04have to give these long values.
- 2:36:08Okay. Sorry, my bad. It's long. So we
- 2:36:10can take values like this.
- 2:36:13Okay. So similarly I can perform
- 2:36:14operations on long stream as well. All
- 2:36:17right. So the crux is that for primitive
- 2:36:20data types as well we have a stream
- 2:36:22interface. We have different interfaces
- 2:36:24for all of them. But you should know
- 2:36:26that this is different from the actual
- 2:36:28stream interface. Okay you should know.
- 2:36:31All right. Now let's talk about parallel
- 2:36:34streams. Normally streams process the
- 2:36:37elements sequentially one after another.
- 2:36:40But sometimes we want to process large
- 2:36:42collections faster by using multiple
- 2:36:45threads. That's where parallel stream
- 2:36:47comes in. It allows the stream to
- 2:36:50process elements in parallel. Using this
- 2:36:52parallel stream method, we can make
- 2:36:54operations faster especially for larger
- 2:36:57data sets. But for small collections, it
- 2:37:00may not make much of a difference. But
- 2:37:02still for large collections, we can use
- 2:37:03this method. All right, let's see. So,
- 2:37:06we have this list of names. Now, instead
- 2:37:09of calling the method stream, I will
- 2:37:12call dot parallel stream. Okay. And then
- 2:37:15I can perform the same operations that I
- 2:37:18used to do earlier. All right. So, I can
- 2:37:20call a filter method. I can just simply
- 2:37:23filter out some values.
- 2:37:26Let's say I want to add a condition that
- 2:37:28string starts with R. Okay. And then I
- 2:37:31want to call for each and I'll just take
- 2:37:34every name and we'll just print these
- 2:37:37names.
- 2:37:39Okay. So the only difference is that
- 2:37:42instead of running sequentially instead
- 2:37:45of looping on the list sequentially now
- 2:37:47the stream will work in a parallel way.
- 2:37:49Okay. Using multiple threads. All right.
- 2:37:52So if I run it
- 2:37:56great. So you will see that these names
- 2:37:59are not printed in a sequential order.
- 2:38:01You see that Rahul is printed first then
- 2:38:04Raj then Ria Reena. Okay. So they are
- 2:38:07not in their actual order. This is
- 2:38:10because we were processing on the stream
- 2:38:12in a parallel way. Okay. So it would
- 2:38:14have actually been processed faster
- 2:38:17because multiple threads are being used.
- 2:38:19However, when it's a very small list, so
- 2:38:22it won't make much of a difference. But
- 2:38:23when we are working with larger data
- 2:38:25sets at that point of time if we want to
- 2:38:28process faster then we can use parallel
- 2:38:30string okay because it will open
- 2:38:32multiple threads.
- 2:38:34All right. Hello and welcome to my
- 2:38:36channel the curious coder. This video is
- 2:38:39part of my ongoing Java 8 series and
- 2:38:42today we are going to talk about another
- 2:38:44important feature of Java 8 the new
- 2:38:46datetime API. In Java 8 a new package
- 2:38:50called Java time was introduced. This
- 2:38:53was added to fix the shortcomings of the
- 2:38:55older classes like date and calendar.
- 2:38:58Now what exactly was improved? Earlier
- 2:39:01the old classes were mutable which made
- 2:39:03them errorprone. But in the new package
- 2:39:06all classes are immutable and thread
- 2:39:08safe making them much safer to use. The
- 2:39:12old classes were also confusing and
- 2:39:14poorly designed. But the new package
- 2:39:16provides clear and separate classes like
- 2:39:19local date, local time and local date
- 2:39:21time. each serving a specific purpose.
- 2:39:24Also, handling time zones was quite
- 2:39:26complicated before, but now it is much
- 2:39:28simpler with dedicated classes like zone
- 2:39:31date, time, and zone ID. All right, now
- 2:39:34that we understand why this new API was
- 2:39:36introduced, let's start exploring it
- 2:39:39step by step. So, this was the package
- 2:39:41which was introduced in Java 8. As you
- 2:39:44can see, I have already imported it.
- 2:39:46I've imported all the classes from this
- 2:39:48package. Okay, so let's start by looking
- 2:39:51at some of the important classes that
- 2:39:53this package contains. First, we have a
- 2:39:56class named local date. This class
- 2:39:59represents the date. Okay, but it does
- 2:40:02not have the time component. All right,
- 2:40:04so for time we have another class local
- 2:40:06time. It only has the time component. It
- 2:40:09does not contain any date. Okay, then we
- 2:40:12have another class local date time.
- 2:40:14Okay, so this contains both date as well
- 2:40:17as time. Right? So let me show you how
- 2:40:19we can create them. It's very simple. We
- 2:40:22can just call a static method dot now.
- 2:40:26Okay. So this will create the current
- 2:40:28date. Similarly,
- 2:40:30we can call this method on local time
- 2:40:33and it will give me the current time and
- 2:40:37this
- 2:40:38local date time
- 2:40:40also as now and it will give me the
- 2:40:42current date time. Okay. So we have
- 2:40:44these three classes and we can create
- 2:40:47the objects of these using this method
- 2:40:49named now. Okay. So let me just print
- 2:40:52this
- 2:40:55LD.
- 2:40:56Okay. Let's just copy this.
- 2:41:05All right. So if I run it.
- 2:41:08Great. So you will see that the first
- 2:41:10one contains the date contains only the
- 2:41:12date. Then the local time has the time
- 2:41:15component and in this local date time we
- 2:41:18have both date as well as the time.
- 2:41:22Okay, cool. Now there are other ways of
- 2:41:26creating these dates. Okay, so here we
- 2:41:28have only created the current date.
- 2:41:29Similarly, we can create a date for
- 2:41:32create a specific date as well. Okay,
- 2:41:34let's say we want a specific date or a
- 2:41:35specific time. So for that we have
- 2:41:38another method which is local date
- 2:41:43dot of off. Okay. So here we can specify
- 2:41:46the year month and the day of the month.
- 2:41:49Okay. So let's say the year is 2025. I
- 2:41:52want the year as 2025, the month as 3
- 2:41:55and the date as 12. All right. So I can
- 2:41:59save it inside this variable. Okay. So
- 2:42:02this is how I can create a date of I can
- 2:42:04create some specific date. Okay.
- 2:42:06Similarly, I can also create a specific
- 2:42:09time. We have local time dot of off.
- 2:42:13Now, here you can pass r, minute, second
- 2:42:15and also nan of second. So, I'll use
- 2:42:17this one r, minute and second. So, let's
- 2:42:20say the r is 18, minute is 25 and second
- 2:42:25is 50.
- 2:42:27Okay, like this. Similarly for LDT we
- 2:42:31have local time local date time
- 2:42:36dot of off. Okay. So here here we can
- 2:42:39specify all these things year month day
- 2:42:42and then hour and minute and then second
- 2:42:44as well in this method. Okay. So that's
- 2:42:47what I'll do. Let's say 2023
- 2:42:50and month is April. Then we have some
- 2:42:53date let's say 13 and time is 12.
- 2:42:5830 and 21 second. Okay. So this is how
- 2:43:04using off method we can create a
- 2:43:06specific date, a specific time and a
- 2:43:08specific date time. Okay. Now if I print
- 2:43:10these
- 2:43:12great. So you'll see that the date
- 2:43:14updated dates have been printed. Okay.
- 2:43:16So this is 2025. Then we have this time
- 2:43:191825. And here we have this date time
- 2:43:222023 and 123021.
- 2:43:26Okay. Now what else? I can also change
- 2:43:29these dates. Okay, so we have some
- 2:43:31methods like I'll show you LD. Plus okay
- 2:43:37so you can see that I can add some days.
- 2:43:40I could add months. I could add weeks or
- 2:43:42I could add years. Okay, so let's say I
- 2:43:44just add a few days. Okay, so in this
- 2:43:47date, let's say I just want to in this
- 2:43:49date I want to add a few days. Okay, so
- 2:43:52I add three days. Similarly in LT we
- 2:43:57have plus hours plus minute plus nanos
- 2:44:00plus second. Okay. So we could add r
- 2:44:02minute or nancond or second. Let's say I
- 2:44:04add some minutes. Okay. So plus minutes
- 2:44:07I add 12. Okay. And
- 2:44:14similarly we also have LDT. And here we
- 2:44:17have both the options. We could add
- 2:44:19days, minutes, hours or we could just
- 2:44:22add seconds, weeks, years. Okay. can
- 2:44:24also we can minus as well. We can
- 2:44:26subtract these things. Okay. So what
- 2:44:28I'll do I'll just minus our year. Okay.
- 2:44:30So minus years let's say three. I reduce
- 2:44:343 years from it. Okay. So now if I run
- 2:44:37it what exactly will happen? Let's see.
- 2:44:41All right. So you will see that the
- 2:44:44dates are actually same. Okay. This is
- 2:44:462025 312 and all these are exactly same.
- 2:44:51Okay. If you'll check it carefully. So
- 2:44:53why didn't it work? Now see as I told
- 2:44:56you in the beginning if you remember
- 2:44:58that these classes are immutable okay so
- 2:45:02what does immutable mean that they don't
- 2:45:04change the same object. Okay like we
- 2:45:07created this object here like local date
- 2:45:09dot now. So the same object does not get
- 2:45:12changed when we are calling this method
- 2:45:13plus days another local date has been
- 2:45:16returned. Okay. So if I'll just do it
- 2:45:18like this that LD do plus date it will
- 2:45:20not change the actual object. Okay,
- 2:45:22instead a new object has been created
- 2:45:24when I call this plus days. So the
- 2:45:26change will be reflected only if I'll
- 2:45:29change my reference variable to this new
- 2:45:31object. Okay, so LD do plus days
- 2:45:34actually returned a new object and this
- 2:45:36reference object this this reference
- 2:45:38variable that I created earlier only if
- 2:45:41I explicitly change it to this object
- 2:45:43then only the change will be reflected.
- 2:45:45Okay, this is what immutability means
- 2:45:48that the the existing object is there.
- 2:45:51It's still in the memory but when we are
- 2:45:53calling these methods a new object has
- 2:45:55been returned. Okay. So now when I'll do
- 2:45:58it like this. Okay. So all these methods
- 2:46:00are returning a new object and we have
- 2:46:03we have explicitly said that these
- 2:46:04reference variables should now point
- 2:46:06towards this new object. Okay. Now the
- 2:46:09change will be reflected. All right. Let
- 2:46:11me show you.
- 2:46:13Great. So you can see that uh the day
- 2:46:16was 12 but now the day is 15 because
- 2:46:18three days have been added. Similarly,
- 2:46:20here 12 minutes have been added. So now
- 2:46:22the minutes are 37 instead of 25. And
- 2:46:26here three years have been reduced. So
- 2:46:28that's why the year is 2020.
- 2:46:31All right. So this is how these methods
- 2:46:34work. Now what else can be done? We can
- 2:46:37also perform comparisons between these
- 2:46:39dates. Okay. Let me show you. Let's say
- 2:46:42I take this date LD and I can simply
- 2:46:45call this method is after. Okay. And
- 2:46:49here I can pass another date. Let's say
- 2:46:51I pass local date dot now. Okay. So what
- 2:46:56it is doing is that it is actually
- 2:46:58comparing this date with this one. Okay.
- 2:47:01So it is checking that whether this date
- 2:47:03is after this date. Okay. So let me just
- 2:47:06print the result. It will actually
- 2:47:07return a boolean result. Okay.
- 2:47:12Similarly we can do it for others.
- 2:47:17Let's say I take LT. So in case of LT
- 2:47:20you can see I cannot pass a local date
- 2:47:22here now. Okay. We can compare local
- 2:47:23time with local time only. That is why
- 2:47:26let's say I take local time dot now.
- 2:47:29Okay. And for LD let's say for LDT sorry
- 2:47:34uh in this case also I will have to pass
- 2:47:36a local date time. Okay. So I'll pass
- 2:47:39local date time dot now. Also like is
- 2:47:41after we have one more method we have
- 2:47:43this method is before. Okay. So it
- 2:47:46checks that whether this local date time
- 2:47:48is before this local date time. Okay.
- 2:47:52And uh what I'll do it let's just first
- 2:47:55print these dates and then let's print
- 2:47:57the comparisons and also I'll print the
- 2:48:00local date as well. Okay. So that you
- 2:48:02know what exactly is the local date and
- 2:48:03time
- 2:48:06date dot now.
- 2:48:11Okay. So this is local time and local
- 2:48:14date time. All right. So let me run
- 2:48:18this.
- 2:48:20Great. So you will see the output is
- 2:48:23false then true and then true. Okay.
- 2:48:25Why? Because in this case we have LD dot
- 2:48:31is after local date dot now. Okay. So
- 2:48:33what is LD? The local date is 2025 315.
- 2:48:37Okay. and local date dot now is 2026.
- 2:48:41Okay. So we are checking that whether
- 2:48:43this date is after this local date which
- 2:48:45it is not. Okay. Because 2025 is before
- 2:48:482026. All right. Similarly for LT is
- 2:48:51after here we are checking the LT is
- 2:48:54183750
- 2:48:56but the current time is actually 108.
- 2:48:59Okay. So this is actually this is yes
- 2:49:02this date is this time is after local
- 2:49:04time dot now. Okay. That's why it
- 2:49:06returned true. And in the last case for
- 2:49:09LDT the current uh the the LDT is
- 2:49:12actually 2020 and you will see that the
- 2:49:15current date is 2026. So that is why
- 2:49:18here we have checked that whether it is
- 2:49:20before. So yes this local date time is
- 2:49:22before this current date current date
- 2:49:24time. Okay. So that's why this also
- 2:49:26return true. Okay. So this is how we can
- 2:49:28also make comparisons between dates.
- 2:49:31Now there are two more important classes
- 2:49:33in this package. Duration and period.
- 2:49:37Duration represents a time based amount.
- 2:49:40Whereas period represents a date based
- 2:49:42amount. Duration is compatible with
- 2:49:44local time as well as local date time.
- 2:49:47Whereas period is only compatible with
- 2:49:49local date. All right. Let's see it in
- 2:49:52practical. So I have already taken this
- 2:49:55example. I have created a start date,
- 2:49:57end date, a start time, end time and
- 2:50:00similarly a start date time and an end
- 2:50:02date time. Okay. So now how can I create
- 2:50:05duration? I'll simply write it like
- 2:50:08this. Duration duration is equal to
- 2:50:11duration dot between. Okay. So we have
- 2:50:14this method between that we can call.
- 2:50:16Here I will pass the start time and in
- 2:50:19the second argument I'll pass the end
- 2:50:20time. Okay. So now what exactly is
- 2:50:23duration? Basically we are we we want to
- 2:50:26calculate the time between this end time
- 2:50:29and the start time. Okay. That is what
- 2:50:31duration represents. Okay. So I'll just
- 2:50:34simply print this system.print ln. What
- 2:50:37I can do is I can just simply print
- 2:50:40duration dot2.
- 2:50:42Okay, I'll just show you duration.2.
- 2:50:45We have these different methods. Okay,
- 2:50:48so I want to know the time difference
- 2:50:50between these two times. And this is
- 2:50:52where I specify in which format I
- 2:50:54actually want them to know. I wanted to
- 2:50:56know. Okay, so let's say I I do 2 hours.
- 2:51:00Okay. Now if I'll print it.
- 2:51:03Okay. So it comes out to be 9. This
- 2:51:06means that these two time have a
- 2:51:08difference of 9 hours between them. And
- 2:51:10as you can see that is the case. This is
- 2:51:1218 and this is 9. So the difference is
- 2:51:149. Okay. But I could have taken this
- 2:51:17difference in the format of minutes as
- 2:51:19well. Okay. And it says it gives me the
- 2:51:22exact minutes which is 569. Okay. So the
- 2:51:25difference between these two time is 569
- 2:51:27minutes. Okay. Similarly, I could do it
- 2:51:30for seconds as well. Okay, I'll just
- 2:51:31show you that as well. Okay, we don't
- 2:51:34have seconds. I guess we have nanos and
- 2:51:36millies. All right, good. Okay, so this
- 2:51:40is how we can create a duration. Now,
- 2:51:43instead of start time, I can also take a
- 2:51:45start date time and an end date time.
- 2:51:48Okay, it is compatible with these as
- 2:51:50well. Okay, so if I run it,
- 2:51:54great. So, you see that it works fine.
- 2:51:56Okay, so these are the dates. These are
- 2:51:58the date times and uh this much is the
- 2:52:01difference but in minutes. Okay. So I'm
- 2:52:03not going to calculate it but yeah this
- 2:52:05is the difference. I could have also
- 2:52:07taken it in hours and yes in this case I
- 2:52:10could take it in some other format as
- 2:52:12well. I could have also taken it in
- 2:52:14days. Okay. So now if I run it.
- 2:52:17Okay. So you'll see that the difference
- 2:52:18is 193 days. Okay. Between these two
- 2:52:21start date time and end date time. All
- 2:52:24right. Similarly we can create a period
- 2:52:26object as well. Let's call it period.
- 2:52:28Period is equal to period dot between.
- 2:52:32Okay. Now here I can pass a start date
- 2:52:35and then end date.
- 2:52:37Okay. And similarly I can just print
- 2:52:42system dot out dot print ln period dot
- 2:52:48get days or get months or get years. So
- 2:52:52this represents the difference in terms
- 2:52:54of date. Okay. So here we don't have the
- 2:52:56time component. So we are saying the
- 2:52:57difference in term of days or months or
- 2:53:00years. Okay. So that is the difference
- 2:53:01between period and duration. Duration is
- 2:53:04basically representing time and period
- 2:53:07is representing days or dates. Okay. So
- 2:53:10let's try to get days. And if I run it
- 2:53:15so you'll see that the difference is 16
- 2:53:16days. Okay. So between these two dates
- 2:53:19the difference is 16 dates. If I would
- 2:53:22have done get years,
- 2:53:25okay, then there is no difference
- 2:53:27because they are in the same year. Okay,
- 2:53:29but let's say I change it to 2026.
- 2:53:32Okay, now the year is one. All right, so
- 2:53:35this is how period and duration works.
- 2:53:38Now we have another very important class
- 2:53:41inside this package zone date time. Zone
- 2:53:44date time represents a date and a time
- 2:53:47along with a time zone. It is used when
- 2:53:50you want to handle date and time across
- 2:53:52different time zones like scheduling
- 2:53:55events globally or working with users in
- 2:53:57different regions. Okay. So let's see.
- 2:54:01So I can just simply use this class zone
- 2:54:03date time. Let's say I want a default
- 2:54:07zone and I can just simply call it like
- 2:54:10this. Zone date time dot now. Okay. Let
- 2:54:14me just print this. System.out.print
- 2:54:17print. Okay, let me call it default.
- 2:54:20Okay, default zone.
- 2:54:24So you will see that it has the date
- 2:54:27component, the time component as well as
- 2:54:30the time zone. Okay, which in this case
- 2:54:32is Asia/colkata
- 2:54:34because the default zone for me is
- 2:54:36Asia/colkata.
- 2:54:38Okay, similarly, I can create a zone
- 2:54:40date time of some particular zone. So I
- 2:54:43can just call it like this. We have a
- 2:54:45class named zone ID and here I can pass
- 2:54:48the zone ID of some particular zone.
- 2:54:51Okay, so let's say I want it for Europe.
- 2:54:54Okay, I want the London time. So let me
- 2:54:57just create this inside
- 2:55:00some variable
- 2:55:06London time. Okay,
- 2:55:09great. So now this will represent the
- 2:55:11time in Europe/ London. Okay. Similarly,
- 2:55:16I can uh what I can do is I can just uh
- 2:55:19uh change a time to a different time
- 2:55:21zone. So, we have this method with
- 2:55:25zone same instant. Okay. Here I can pass
- 2:55:29zone ID dot of
- 2:55:31let's say America/ new york. Okay. So, I
- 2:55:34want to convert this London time into
- 2:55:36this America time. Okay. So now I can
- 2:55:39just take it inside some variable. Let's
- 2:55:41say US time.
- 2:55:46All right, let me just print this as
- 2:55:49well.
- 2:55:52print ln US time. Okay,
- 2:55:57great. So you will see that this was the
- 2:56:00India time then this is the London time
- 2:56:02and now this London time was converted
- 2:56:04into this US time. Okay, so this also we
- 2:56:08can do using this new datetime API.
- 2:56:12All right, so these were all the
- 2:56:13important features which were introduced
- 2:56:15in this new datetime API. I hope this
- 2:56:18video was helpful. For more such
- 2:56:20content, please follow my channel, The
- 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.