- Podcasts
- The Java Developers Hub
- Java Lambda Expressions & Functional Interfaces Explained | Method References and Composition
Java Lambda Expressions & Functional Interfaces Explained | Method References and Composition
The Java Developers Hub
- December 3, 2026
- 1 h 55 min
- Episode 25
About this episodeModern Java programming embraces functional programming concepts that help developers write cleaner, more concise, and reusable code…
- Understanding functional programming in Java.
- What Lambda Expressions are and why they are useful.
- Lambda syntax and practical examples.
- Understanding Functional Interfaces.
- The role of the @FunctionalInterface annotation.
- Exploring built-in functional interfaces:
- Predicate
- Function
- Consumer
- Supplier
- BiFunction
- Predicate
- Understanding lambda parameters and return values.
- Working with lambda expressions in collections.
- Understanding method references in Java.
- Using static, instance, and constructor method references.
- Differences between lambda expressions and method references.
- Understanding functional composition.
- Using andThen() and compose() with Function.
- Combining predicates using and(), or(), and negate().
- Applying functional interfaces with the Java Stream API.
- Best practices for writing clean functional Java code.
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Java Lambda Expressions & Functional Interfaces Explained | Method References and Composition
Welcome back Coder Army to our Java series and today we are going to go into the depth of our lambda expressions. We are going to understand what are the method references, how many types of lambda expressions we can make, how many types of functional interfaces are there in Java. You will definitely get to learn a lot. So let's quickly go to the screen and start. So in our last video, we talked about lambda expressions. And we saw that who supports lambda expressions? They support our functional interfaces. Now, we also saw that to make a functional interface, we use an annotation, at the rate function interface, which tells a compiler that the interface we are going to make below will be a functional interface. This gives us a compiler time advantage, so that if someone tries to put an abstract in it, then the compiler will give us an error. Okay, we understood all this. But till now, we had not made our own functional interface. So today we will do the same. First of all, let's go to the code and make our own functional interface. Let's make a lambda expression of it. But will you ever need to make your own functional interface in Java? The answer is maybe never. It will be rare sometimes. Why? Because in Java, It provides its own functional interfaces, such functional interfaces, with the help of which you can do 95-99% of the work on your own. You just use them and all your work will be done. All the lambdas that you have to make, all those use cases will be covered in those functional interfaces. So it is very rare that you have to make your own functional interface. So you only do this when you have to put a wrapper on the existing functional interface. So we will see that too. First, let's go to the code and make our own functional interface and make its lambda expressions. So here I have taken a demo.java empty file and we have to create a functional interface of our own. Interface and its name is Calculator. So let's say this is a function interface so there should be only one method. So let's name it int calculate. And it will take two numbers int a comma int b. And it will perform a calculation on them. So let's say it can perform addition, subtraction, multiplication, division, anything. And it depends on what class it wants to implement. Now again if this is a function interface then I should add annotation on it. Functional interface which is optional but it's good to use. Now if I want to use this calculate function, then what was the traditional method I used to make my own class which was a lengthy method. So we used to write class addition and it implements this calculator. And if it's implementing then we have to override its method. So let's say public int calculate and we'll take int a comma int b. and let's say return return a plus b something like this comma will come here something like this we used to do now we can directly go to our main function and we can make our calculator object let's say reference variable c equal to new and here we can pass a concrete class let's say new addition and then we can simply call c.calculate and let's say I gave value 5 comma 4 Sorry, 5 comma 4. Something like this. Now I know what will happen, these 5 and 4 values will go here and they will be added and I will get a return of no. Now I will get a return of no. So let's say I have to print this on the screen. So one way is that I first take its answer in a variable sum and what do I do with it? I print it directly on the screen. I print it directly on the screen, sum. Something like this. Now if I run it, then my answer should be 9, which we already know. It has come, okay. Now I want to make my own lambda expression. For that I will change the code a little bit. So let's say first of all I write a new function void function static void. So I write it like this let's say public static void. And this function's name is print. This function works to print. It will take two numbers int a, int b. And it will take a reference variable of this calculator. Calculator C. And again it will call the method of this calculator. So let's say system.out.println will directly do c.calculate. And it will also give a comma here. No problem. So that we don't have to calculate by ourselves. In fact, we don't have to write this too. We just have to make an object of calculator and call our print directly. Let's say, by giving 5 comma 4 and this variable c. Something like this. Okay. Let's make a little gap. Yes, something like this. Now, what happened here? 5 comma 4 is our a comma b. And what is c? This is the object of calculator. It will go here. This function is in print function. And what will this print function do? It will print on screen. c dot calculate a comma b. That means, it will print by giving 5 comma 4. That means, we should get 9 again. Absolutely right. Now, I will change this code in the form of lambda expressions. For that, I don't need this implementation, so I can remove it. Because I only have the interface, not the implementation. As soon as I put it, an error will appear here. So, I will remove this line too. I don't need it. Now see what I had to pass in the print function. I had to pass a comma b, which I have just done. And I had to pass an object of the calculator, a reference variable. This expects my print function from me. Remember, just as my collections dot sort function expects me to pass a list, and pass an object of our comparator here. So instead of this object, I could have passed an actual object. What could I have done? I could have implemented my comparator from a new class and written my function compareTo on it. And I could have passed the object of that class that I had made in the new class here. Like we saw, I passed c1, c2, c3. Otherwise, I could have passed a lambda expression here. And what is a lambda expression? Our Java compiler understands that the only method in the comparator is the implementation of that. So I can do the same thing here. I passed a, b. And instead of c, I pass a lambda expression. So the lambda expression will be something like this. I will say a, b maps to a plus b. Something like this. So the compiler will understand that this is what I want to do. I am implementing this calculator in such a way that the only method in this calculator is calculate. How am I overwriting it? I take two variables a, b and return a plus b. So I am working in the same way. Now if I run it, the output will be the same. 9, right? In fact, now I have this advantage that I can return a comma b to a into b so that I can multiply. So now my 5 into 4 will be plus 20. If I run this, then see, I have 20. In fact, I can do anything with 5 a minus b, a divided by b. I have this freedom because I just have to pass a lambda expression here. Okay, very good. Now let's remove this, we don't need it. Now let me tell you one more thing. Where are we passing this lambda expression? Where an object of the calculator was expected. So basically what is this? I have implemented my calculator class in one way. It has only one method. Which is calculate. I have overrided it. And I have written its logic here. Java will understand all this by itself. So can't I assign any C variable to this lambda expression? What if I do this? Instead of calculator C equal to new addition. I will pass a lambda expression here. Something like this. Can I do this? The answer is yes, I can do this. So, I didn't get any error. And I will pass this C here. Something like this. See, I didn't get any error. I can do this. Right? So what did I do here? I passed a lambda expression in the object of calculator C, stored it in C, and I passed that C in this print function, which I can do. When I was passing lambda here, I was still assigning a reference variable called calculator C here. So I can also put a lambda expression in a reference variable called calculator C. And this is very important because we are going to use it a lot now. So if I run it now, my answer will be the same. It will be 5 plus 4, 9. Right? Yes, so we discussed that Java provides us with its own functional interfaces and we can use them to make lambda expressions and those interfaces are enough. So first of all, let's talk about it. If we look at it, then Java.util.function is a package in Java. Java.util.function in which we get all our functional interfaces which Java wants to provide us. First of all, I will list them down and they are divided in such a way that we have four Core interfaces, basically functional interfaces that java provides us and this is very interesting The first name is function, there is an interface whose name is function, one is consumer, one is supplier and one is predicate These four are the core, the rest will be made using these. And I am telling you that almost 95-98% of the problems will be solved by using these interfaces. You will need to make your own interface. It can be that you want to make a wrapper on it or you want to do something very specific or very niche that they will not be able to do. Now let's look at all these interfaces one by one, what are their methods, and how can we replace them with lambda expressions. Okay, so first of all we are going to talk about our function interface. Okay, an interface whose name is function. Okay, now see what this interface says or what it tries to implement. This interface says that I will take an input, it will take an input. will do some processing on it and will give you an output, the simplest form of a function That's why its name is function. What we can think of a function, this mathematical function, so basically it will simply take an input and transform it into an output. It is the simplest form of an input or output or function. That's why its name is function. Like we understand a function, a mathematical function, which we also call f of x in math. So now I said, what does it do? It takes an input and gives an output. If you ever want to do something like this in Java, then you can use this function, this interface as a functional interface. You can provide a lambda in it, wherever there is a demand for it. Now we will see how. So first I will talk about that someone will take an input and give an output. Which type of input will it take? Any type. Because we know that our functional interfaces use generics from inside. And what type of output will it give? Any type. So I can write something like this, that someone takes a T, where T and R can be of any type. So now I will tell you how it looks, how its syntax is. Simple, here we have an interface called function, which is made from inside. It works on T, R. We know. It should have only one method, again because it is a function interface, it should have only one abstract method. And it has only one method, which is called apply. which takes a t-type object, let's say small t, and performs something on it, and returns us an r-type object. Right? So this is our functional interface, which Java provides us, which is called function. And it has only one method, apply. And you know what it will do? It will work on an input t, and return an output r. Simple. So anywhere, if you need an interface, where you have to work on an input, and generate some output, you have to transform that input into an output. Transform. So you can use this function, you know it can be used in many places. You don't need to write such a big syntax that first you make a class which will implement this function and then you overwrite the apply method. No. Wherever this function will be demanded, you will pass a lambda expression. And this is exactly the same as we represent any function in mathematics. So you can assume, I said take input. So you took an input x and you gave the output f. Function of x. You know that mathematically we say that if x is an input, then f is a function which will give you an output. Now if I try to make a lambda expression of this functional interface, then how can it look? It can look like this. What did I say? It will work on an input. Let's say it worked on an input x. Here instead of t we have x and mapsTo and it will give you an output. So let's say output is given x into x. Something like this. So our lambda expression is written in the same format of the function. So it is overriding this apply method. It takes an input x and returns the square. So this is x and this is f. So it is the same. So wherever you have a scenario where you want to convert an input into an output, transform it, let's say you want to take out its square, its addition, or anything else you can use our function interface so here I will make a function object of function interface and it works on t, r now again before using it I have to import it so I can write import java.util. function package and I have imported star in it so that we don't get any error now I have imported it let's quickly make an object of it let's say square And I have to tell here on which it will work. I have just passed t, r. Actually, let's say I have to work on integer. So I accept integer value and return integer value. Okay, I have passed here. Its object is square. Now can I pass a lambda expression here? Yes, I can. So let's say I can pass a lambda expression. I will take x as an input. And I will map it to x into x. Something like this. Let's make a better gap. Something like this. So what I am saying is, if I go inside this function, then you will get to know that there is an interface called function, which has only one method, apply. So when I passed this lambda expression, then Java understood that this lambda expression is an override of this apply method. I am saying that do you know how I am overriding this apply method? I will take an input t, instead of that I took an input x. which is of type integer and I will give an output in R. I will give an output in R. So, instead of that, I have given an output of x into x. Right? So, I can do this. Now, if I want, I can print on my screen. Let's say, what is the object name of this? Square. So, I will write square. dot and call apply on it and let's say I'll give 5 in the input in this way, square dot apply 5 and I'll get the answer 25. It's here. Right? If you understood the function then you'll understand the next three very easily. So let's work on the next one. The next one is consumer. Now, what is the functional interface of consumer? Consumer will take an input t but will not give an output. That's why I have written only one t. Its name is consume. It will consume an input. Its generic form can be that it will take an input t and give an output void. It will work on t, void. If I show you its interface from inside, it looks like this. public public interface and we will get the name of the interface our consumer and it works on T and it has only one method and the name of that method is apply and what will be the return type of that? void so void apply and it will take an object of type T and it will perform an apply method on it. Now what is apply? Apply is nothing. You take an input and do something with it but don't return any output. If you have a scenario, a situation where you have to work on an input and don't return any output, let's say you just have to print something on the screen, you have to put a value in some log file, in that case you can use our consumer interface. Again, you don't need to overwrite all this that you implement it, overwrite apply, you will just pass the lambda expression. So if I say, what can we do with its general form? We can say that it takes an input, does something with that input and returns no output. Right? This is its generic form. So if I say, how can we make a generic lambda expression of this? Right? Which can represent the consumer a little bit and show us. So we can't write something like this. We can write, let's say, an input takes x. So I said, it will take an x and map it with what? There is no output to be given. So let's say I printed something on the screen. So I wrote system.out. println and I will print x on the screen. So something like this. I will take x and print x on the screen. I can make a lambda expression like this. So this means if I want to print something on the screen, then I can use this consumer lambda expression x maps to system.out.println. So let's use it and see. Yes, so we had used a function here. Let's do one thing. Let's use another interface called consumer. Which will work on integer. Let's say I make it an object. Print equal to. And here I can pass a climate expression. So I can write x maps to. Let's say system.out.println. And here I can write x. Something like this. Right? I will do this comment for now. And see what I have written for now. That my consumer named function interface. Its object name is print. And what does it do? It does something like this. x maps to print x. Right? If we go inside consumer. Then you know that consumer is an interface. Which has only one method. Accept. So here I have passed this lambda expression. This one. It is overriding this accept and providing its logic. And what is the logic? This is its logic. That whatever x you give me, I will print x on the screen. Right? Now what will I have to do to implement this logic? This reference variable I have, print, I will have to call its accept method. Right? Like we have here, Here, we called the apply of square because there is a method called apply in the function. We made an object of that, a reference variable called square and we gave it this lambda expression. And you know what it means to give a lambda expression? That in a way, we are implementing this function interface, we are overriding this apply and whatever will be its logic, it will be something like this. Right? You have assigned it logic directly, we know that. So, what we will have to do is, we will have to call this print, accept method and let's say I gave it value 7. So I am saying, print.accept 7, take 7 and accept it. And as soon as the method calls, what is its logic? Its logic is this. Your screen should print 7. I will comment this again. So I am just saying, as soon as I am giving print.accept 7, my logic should apply. And what does this logic do? It takes 7 and prints 7 on the screen. So if I run it once, then see, my 7 is printed on the screen. If you understood the consumer, which was very easy, the next is completely opposite of this, whose name is Supplier Supplier Supplier is the opposite of consumer I will not take any input but I will give you some output I will supply some value to you Supplier does not take any input but it gives an output Let's say T Supplier's generic form is such that it will not take any input Let's say it will take a void input But it will give you some T output So basically it takes no input but gives something as output. If I represent this interface from the inside, then how will it look? Its internal implementation will be something like this. We will have a public interface called supplier. Supplier will give the output. T works on T. There will be only one method in it. The name of that method is get. So it returns, let's say, T. The name of this method is get. It doesn't take anything. And here we have just declared it and left it. This is how our supplier interface will look like. Now when will you use the supplier? Whenever you have to return something without taking any input, right? If you want to take input and output, then we had a function. If you just want to take input and not output, then it was a consumer. And if you don't want to give input but you want to give output, then the supplier interface works for us. So let's say I want to write a lambda expression for this. So can't I write something like this? I don't take any input and I map it. And let's say I want to give a random value on the screen. So I write something like this, I have a class Math, it has a static method, Math.random. What does it do? It generates a random value and outputs it on the screen. So I gave a lambda expression like this. Nothing, no input maps to Math.random. Now what can I do to implement this? I will call the object I have made, this get method. Because in supplier interface we have only one method, get. So let's quickly do this too. So after consumer, I am going to create a supplier. So let's say I wrote supplier and it works on someone. So let's say I have to return integer. And let's name it random value. And it doesn't take any input and it maps it. Let's say math dot on random, something like this and what can I simply do? I can print on screen whatever random value I have,. get so I did random value.get I should get a random value now what error is coming here? it returns double type object so let's say I want to work with double Lets do something simple. As soon as I call randomValue.get, if I go to the supplier, you know that there is an interface that has the same method. It doesn't take any input but returns a T-type object. So I am saying that it doesn't take any input but it will return a double type object. And whatever it returns, I will print it on the screen. So if I run this once, then I got a random value of 0.0234. I hope you understood this. So basically, it is telling you that it works to supply data. It can supply data without taking any input. So wherever you need something like this, you will use supplier interface. Now let's move towards our last interface, which function interface Java provides us, whose name is predicate. Okay, what does predicate say? This is very interesting. Its generic form looks something like this. It says that it takes an input, let's say it takes an input t and it returns a boolean. So predicate basically performs some test on an input and returns either true or false. So basically it returns a boolean value. If the test passes, it returns true, if it fails, it returns false. So if I show you how it looks from inside, we have an interface, public interface, let's say predicate. Predicate. And if it is taking T, then it must be working on T-Generics. So I can take T here. And it will have only one method. The name of the method is test. And it will return boolean. So I can write boolean. The name of the method is test. Which will take a T-type object and perform a test on it. Something like this. So I have made an internally implemented predicate interface. And whenever you have a condition that you have to perform some test on an input and return true or false, then you can use this interface. So if I tell you a generic use case, then you understand a situation. I have to take a number and tell if that number is even or not. If it is even, then I have to return true. And if it is not even, then I have to return false. So I can write some such lambda expression. I can write x maps to What will I do? What is x modulo 2 equal equal 0? So, you know if I divide x by 2 and if the remainder is 0, then it is odd. So, if this situation is true, then the answer should be true because the number is even. And if it is not even, then the number will be odd. So, we will return false. So, in this way, I can make a generic lambda expression. Let's run this quickly and see. Okay, so here we will quickly comment this too and make our fourth interface which is predicate. And again it will work on an integer, let's say. And let's name it isEven. And here I can give a lambda expression. So I will give something like this. x maps to let's say x modulo to equal equal zero. Something like this. Even if I don't give a bracket here, it's optional if only one input comes. So I can write something like this. If I go to predicate, you will find out that this is a public interface. So you will find out that this is a functional interface predicate which has only one method, test. Which runs boolean and takes an object of T type. Okay. So how can I test this? What I simply have to call is, let's say system.out.println is even, I will call test method. test method and let's say I will pass 8 then I should get the answer true is even.test should be true because I have passed 8 so I got true and if I pass any odd number let's say 7 then I should get false if I will run then I will get false ok so basically you will use predicate only when you want to test a condition now if we have seen all the interfaces the functional interface that java provides if I conclude that So, I can say that we have four interfaces, function, consumer, supplier, predicate, and these are all their generic forms. Function means, it will transform a T into R. So, basically, the word here can be transformed. Okay. And the next one is, we have consumer. Consumer will take T and return void. Basically, it will consume data. Right? Consume data. Next, we have supplier, who will take nothing, void, and will return t. Basically, he will supply some new value to us. So, he does supply. And what will he do? Predicate, you know, he will take a t and test it on boolean. Again, his work is to test. Right? I hope you understood this well. Because it is very simple. If you see, then almost all problems... will be completed with these four interfaces. Either you will be returning R from T or void from T or Boolean from T. All these things almost cover all the scenarios. The few remaining scenarios, we will see further that by tweaking these, we can make some more functional interfaces in Java. But before that, I want to talk to you about a very interesting thing. And that thing is that, first of all, let me comment on this. I have made these four function, consumer, and supplier predicates. I have not called them. When we talked about the iterable interface, I will write it as iterable. I will import it as java.util.star. Some of you might be confused that Java.Util.Function is a different package and Java.Util is a different package Obviously, this Java.Util.Function will be written in Java.Util But when I do star here, it means that I am importing all the classes of this Java.Util But I am not importing all its packages, there is a sub-package called function in it And there will be a lot of classes in it So I will have to import it separately, right? So remember this once So now let's talk about iterable interface. So what I said is that we can make an iterable in Java. If I go into this interface, it will give us an iterator. There is another method in this interface. So basically, let's say I have a list. So let's say I write list of integer. L-I-S-T equal to new array list. Something like this. And let's say I have added some values in this list. So list dot add one. Or let's do something better than this. Let's square it here. Let me write it. list.off, I have a method of, and here 1, 2, 3, 4, 5, 6, 7, 8, 9, 0. I pass some such numbers. Okay. So, if I was saying that I have to print all these, then I have one way, that I put a simple for loop, for int i equal to 0, i less than, length list dot size i plus plus, something like this. Second, I had a for each loop, I simply do, for every integer i in this list, I simply do system dot order println i, something like this, then also my list gets printed, right? We have done this many times. But in Java, you know when we write like this, the iterator of the iterable interface is being called from inside. And we change this syntax in the iterator syntax. Now look, in our iterable, there is another method which is called foreach. So when we were reading the methods of iterable, we left it because it uses lambdas from inside. If I go inside foreach, which is inside iterable, it won't let me go like this, I write list.foreach. If you know it is in itrable, then I can access it through the list as well. If I put it in foreach, then you will get to know that this is a default method foreach which exists in itrable.java. And look what it is taking. It takes a consumer type object named action. If it takes consumer, then you know what method is there on consumer. The consumer method is accept because consumer accepts if I go inside it. So here, either I make a proper consumer object, or I just put a lambda expression. And you know what a consumer does? It consumes. It takes some input, t, but doesn't return any output. It returns void. So t maps to void. You know we have a consumer mapping. So what can I do? I can provide a consumer in this foreach. Basically some input that doesn't give any output. And foreach means that the foreach method I'm using, And it will work for every value of that collection. So what I mean to say is that I have another way to print all these. So let me write one above that I saw this method above that I had written let's say for every integer i in list and I did system.out.println i here. So this was one way, I commented this. There is another way that I simply write list.foreach and here I have to give a consumer, so how do I do it? Map x System.out.println. See what I did here. I said list.forEach I will give you an x and print that x. And this is true for every value in this list. That's why its name is forEach. If you see its implementation, it's using a for loop from inside. It's using a for each loop. So foreach is using a for loop from inside. And it works on this. So, this means, I have put it on this list. So, it is working on this list. And it will print all its values. Here, I had to give consumer. So, I passed a clamped expression. Will it work? If I run it, then it will work. So, this is a very interesting method. For each method. If you understood all this. So, now let's talk about primitive functional interfaces. Till now, all the interfaces we were talking about, functional interfaces, they all use generics, we already know that. And in those generics, you write like this, integer, or if you are working with double, then its wrapper class double, or if you are working with a user-defined data type student, then like this. You never work with primitives. And we saw this in generics too, that generics doesn't work with primitives. But why do we need primitives? We do all our work with an object class because we have a wrapper class. Let me tell you why we need it. Remember, we had written an implementation of a function in our code. We have a functional interface. So we had written, let's say, it will take integer comma integer. And I will make an object of it. Let's say, I will call the object square. And I will give it a lambda expression. And what will it do? For every x maps to x into x. Something like this. Right? So, I will basically get the square of every x. And I said, this is a lambda expression. We had seen it in code too. Right? Absolutely, brother. Now think about what is happening in the background. Do you know what is auto boxing and unboxing? Auto boxing, right? Because Java can convert any primitive x to object x and object x to primitive x. Now think, what is this x? This is an integer by default and we are performing x into x. But x is an integer. Integer means an object. You can't perform a square on that object. So java must have unboxed from inside. So java must have converted this integer into int first. And then performed x into x. And finally because we have to return an integer. So java must have converted this primitive back into our object integer. And returned its value. Right. So basically first of all we must have unboxed this x. Right, we have done unboxing in step 1 So that this integer x is converted to normal int After that some action will be performed on it So basically I can write operation, something was performed And when operation is performed on this int into int So again our value will be made as int After that when return type is integer So again we have to auto box it in integer class So after that I can write auto boxing So basically, all these operations will be done from inside. Now, you know that these operations can be expensive as well. That you will first make an object into an integer, simple, primitive, and perform some operations from it, and then again convert that primitive into your wrapper class, right? Now, see, here, we did it just once, so we didn't know much about the difference. But now think, if I were doing this work in a loop, in a for loop, and I was doing it millions of times, then think, millions of times, first there would be unboxing, then there would be operation performance, and then there would be auto-boxing. then our entire operation would have slowed down a bit. So this is what came to Java's mind. What happens is that I make some functional interfaces that work on primitives. Right? So that I don't have to use this one, the function that I have used. Because what is it doing? It is doing auto-boxing and unboxing from inside. I know that this calculation is a simple int calculation. Basically it can work on primitives. So why not I make such a functional interface that works with primitives. Not with objects. And from there our primitive functional interfaces are born. So let's see, this is nothing new. We already have some alternatives of function, consumer, supply and predicate. And let me tell you which ones they are. Before that, which primitives are we going to support? We are going to support only three primitives. One is int, one is long and one is double. Only these three. Why these three? We have a lot of primitives. We have short, byte, float, etc. The reason for this is that these three are the most used. For example, we are running a fall loop for loops and millions of times we are performing this action. So we don't want to have auto-boxing and unboxing again and again. That's why we have to optimize them. And you know that in loops or most of the time when you are talking about primitives, you talk about these three. You use double because you use less float. We talked about it. Float is less precise, so we always use double in production. Now, the most used integer is int, because you know, whether it is short or byte, they all transform automatically in int, we also know that. And again, if we want something bigger than int, then we have long. So these three cover everything. Now you will say, why not care? Because we don't perform iterations on character, we perform some transformations on it. Have you ever seen that we are using care in a for loop? Very rare, maybe we will never do it. So these are the three that are most used, so Java optimized these three. So this is very simple, let me explain it to you. First of all, we had a function, an interface function, which you know, what was the generic form? t is to r, right? So Java made a primitive family of this function. Primitive family. What did it make? Let me tell you. It made a primitive family of this function, which we call int function. Int function. Okay. So its generic form was t is to r, right? Int function says that I don't take t, I work on a small int and return an object. Right? So basically I work on a small int and return an object. Now obviously if we have an int function, then we will have an alternative of a long function. Okay? You know what it will do. It will work on a small long and return an r. Basically an object. Same we will have a double function. Double function. This is also a functional interface. It will work on double and return an object. So if there is a condition that you have to work on a primitive and return an object. Let's say you want to return a user defined student object, any object, then you can use int function, long function, double function. Again, this is an alternative to a function. The generic form of a function is t is to r. You fixed this t, once you fixed int, once you fixed long, once you fixed double. Now you will say that primitives don't use generics, but we won't use generics here. If you see the implementation of this int function from inside, then it will be something like this. int function will be our interface and here it will not write int comma r it will only write r because we only use r with generics and from inside the method of our function which we call apply in that apply it will work with int let's say int a and it will return r this will be its generic form here it is not writing any generic it has fixed it as integer right that's why we can work here with int, long and double If you understood, then we have another hierarchy of this function. Like we have int function, long function and double function. There are some other functions. Let's say one is two int function. Just put two in front of all these. One is obviously two long function. And again you must have understood. One is two double function. Two double function. Again all these work with t. t, t, t. I forgot to write here because again we take the same generics t rest is fixed. Now what is 2int, 2long and 2double? Nothing, it is the opposite. It is saying that it will take an input object but will give an output primitive either int, long or double. Depending upon whether you have used 2int function, 2long function or 2double function. So it is the opposite of what you saw here. int function says that I will take integer but I will give output object. to int function says that I will take object input but I will give output an integer. Right? And same goes for long function, to long function, double function, to double function. All these functions come under this family. So, if there is a use case that you have to work with primitives as an input or you have to work with primitives as an output. But there will always be one object, either input or output. So, you can use these families. If you understand this, then you will also understand primitive consumer family. So if I talk about our consumer family, primitive consumer family. So I will write primitive. So what can be these? Now, first of all, what was the generic form of the consumer? It was this. T is to void. T maps to void. It took a T and did not give any output. So in the same way, we may have an int consumer. One can be a long consumer and the other can be a double consumer. Again, there is no gap between long and consumer. Now you must be understanding the generic form of int consumer. Int to void will convert an integer into a void. Long to void will be of long consumer. And double consumer will be double to void. Double to void. It is very basic. I hope you understood this. If you understood this, then there is an alternative to it too. For example, you wrote int consumer, so here we have object int consumer. Now, if it took int long double and returned void, then object int consumer will take two inputs, let's say one will take t and one will take int and return void. Right? So the same happened, like object int, in the same way an object long consumer happened. Which will take one t, basically one object, and one primitive int, and return void. And in the same way, you can assume that we have double long, we have double consumer. Okay? Now here, Now why does this exist? Because consumer as in general does not give any output, there will always be a void output in any case. So consumer says that if there is such a situation that you have to work with an object and with it you have to work with a primitive, whether with int, long or double. If there is any such condition, then you can use our object int consumer, object long consumer, or object double consumer. And this return will always be void because consumer never returns anything. So this is the class that if you just want to convert int to void or long to void or double to void, then you can use this. But if you also want an object, t comma int, t comma double, t comma long, then you can use this variation. Both variations have been given to you. If you understood this, then we have primitive supplier family right because if we read normal supplier then let's see primitive supplier now you know what is normal supplier normal supplier doesn't take anything but returns t so you must be understanding what can be its primitive version one can be your int supplier one can be your long supplier and again you can have double supplier double supplier Something like this. And what will they do? Exactly the same. This does void to t. So this will do void to integer. Normal int. This will do void to long. And you understood. This will do void to double. Something like this. So if there is such a use case. Then you can use these primitives. Supplier's primitives. Now they don't have any alternative, they just have these alternatives Because you can't return two values What was the alternative of the consumer? That once int long will take double and once it will take two inputs Object, primitive, right? Here what we did? We saw the variation of this only That there is no input because the supplier doesn't take any input But the output will give primitive Whether int, long or double What can you do here? The output will give int, t, not at all You can never return two things in java So here it has only one variation, primitive supplier Okay Now let's talk about our last one which is our primitive predicate family primitive predicate Do you know what is the generic form of predicate? It is t is to boolean It takes an input of object type and returns true or false Now again you must have understood what are the alternatives of this in the case of primitives One is obviously int predicate One is long predicate and again double predicate And what will int predicate do to you? It will convert int to boolean, obviously. Basically, remove the t part from the constant. One will convert long to boolean. And one will convert double to boolean. Something like this. Right? I hope you understood this, it was very simple. We had the primitive predicate, supplier, consumer and function. Their use cases are very limited, but they are there. Sometimes you have to work with primitives, then you can use these interfaces. Java has made them all from inside. Apart from this, there are some other special functions that Java has given. Like one is primitive operator family. Now what is this family tree? It says, what happens if your input and output, both are primitive. Till now, we were either making the input primitive or the output primitive. But if both are primitive, let's say you take int at the input and int at the output, then this can also happen. In these cases, we call it primitive operator family. And our variations are something like this. One is our, let's say, int unary operator. Again, this is an interface and its generic type is int to int. Something like this and you would have understood that this will be long to long and this will be double to double. So this is one variation of it. We have int unary, long unary and double unary. What does unary mean? Unary means single input. So it is working on single input and it will give a single output. If you want to work on double input then you can use binary. Basically its name will be int binary operator. And it works on two inputs int comma int and returns the same integer int. Something like this. So let's say you took a comma b as an input and returned the output. Let's say a small r, that too will be of int type. So you can do it like this. And again we have long binary and double binary. Both exist. And you must know their alternatives. Long comma long to long and double comma double to double. Right? You can understand that. So there are a lot of them. And again, the use cases of the primitives are limited. But again, wherever there is a use case and you use them, you will get a lot of benefit in optimization. If you want, you can always do the normal ones. Normal functions, supplier, predicate, and consumer. The problem I told you about auto boxing and unboxing is a very genuine problem. That's why we always prefer that you use the family of primitives. Whether it's the primitive consumer, the primitive supplier, the primitive function, or the primitive predicate. And I have listed all the functions for you. It's simple if you understand it once. There is no need to remember. You will put it in your ID. You get everything easily by putting a dot. Or simply by writing its name. Okay. So I just told you that we have to remember these four classes. Function, predicate, consumer, supplier. And we have to remember the methods inside them. Apart from that, everything else is derived from them. Now let's move on to our next topic. Whose name is method reference. And this is a very interesting topic. This makes Java more optimized Now look What we were doing till now? We were going deep into functional programming in Java. We were understanding why everything should be object-oriented programming. We should deal with some things with functions too. We can work with functions too. Like we saw that we wrap a function in an object and provide that object to any method. Let's say to an argument of a method. We optimized this using lambda expressions. You will remember this. Method reference says, I can further optimize lambda too. Lambda too. How? See. It says, whenever you are doing something in lambda expression, which is just calling an already existing function. Okay. For example, let's say, you write such a lambda expression. You say, take an input x, and map it, from system.out.println x. system.out.println XC. Escuche ese. So what are you trying to do here? You are just trying to print whatever x you take on the screen. And do you know what kind of lambda expression is this? This is a consumer type lambda expression. Why? Because an input takes T but the output doesn't give anything. It is printing on the screen and the return type is void. So T to void. This is a consumer type family. So wherever the consumer is expected, I can pass this lambda expression. For example, we have a method on the list. Let's say for each... Not just on the list, but on every collection. I can do list.foreach. Because foreach is not described in our iterable. And what can I pass here? I can pass this lambda expression. Because foreach expects a consumer from me. So instead of that consumer, I can pass this. And again, what does foreach do internally? Whatever I pass here, like if I pass a lambda expression, it will collect it in a reference variable c. And it will call the accept method of that c, c.accept. Right, you know this. It will work on some input and will not give you any output. We know this story from the inside. But method reference says that you will write this big x maps to system.out.println. But what are you actually doing? You are just trying to pass this x here, right? So, the method reference gave a new syntax. It said that you just refer the method. Which method are you trying to call? Are you calling the println method? Where does this println method exist? This exists in system.out. The system is a class. In it, out is an object of printStream class. If you didn't know this, no problem. We had understood this well in the input-output class. You can assume that println is a function of a printStream class. And an object of printStream class is out. Which we have made inside the system class. It is statically defined inside the system class. We have read this. So you can write like this. System.out This is the name of your class And from here you are doing method reference To println I don't need to use these brackets Because this bracket means that I am calling it But I am not calling it I am just referring it So I wrote the syntax here System.out method reference operator println This operator in C + + you would call it scope resolution operator or something else But in Java we call it method reference operator method reference Because what it does is, it refers to a method. So again, if I write its generic syntax, it will be something like this. You will write class name, method reference operator, method name. Right? So, you can write something like this. So, here, I can pass this in this forEach. I can pass this as well, instead of this lambda expression. And this is much better to look at. Because here I am just telling, list.foreach, system.out, method reference, println. So basically, whatever I give here, you just print it. I am just telling Java internally, that whenever I am writing list.foreach and system.out, method reference, println, then I am telling, that you just refer this println method. Refer means go and see what he does. Whatever he does, do that with every value of my list. Right? Java will understand. You don't even need to write all this. If I give x, then print it on the screen. Java, you just write system.out method reference println. Okay? Now method reference is amazing because it makes your syntax even better, even smaller. Now you just have to remember this thing. We are just saying this. Use this method. Use this method when needed. This is what we are trying to tell Java. Whenever you need it, call this method. Whenever you need it, whenever you write system.list.forEach and pass this method, what will foreach do from inside? You know, we had seen that foreach method from inside calls for loop only. So how will it call for loop only? He must be iterating in this list. You saw that he was trying to iterate in this list. And what is this? This is a list. So he must be iterating in this list. And while iterating, I said that whenever you want to understand, whenever you basically, what I have passed here, he must be expecting a consumer type object. So what will that C dot call on that consumer? It will call accept. Because in consumer we have accept method. So when you will accept C, what will it do? It will try to implement this logic. And this logic is telling Java to use this method when needed. Which method? Println method. You just go and see the println method and use that method to implement this logic. Right? So as soon as you go and see the printAlign method, you will understand that printAlign works with print. Who does it print? Whatever input you give. Right? Inputs are not given by itself here, but Java understood that with which input do I have to work here? What I have written here is list.foreach, so I have to work with the input of the list. So basically it will print every input of the list on the screen by itself. It will understand from the context that you don't need to tell it anything by itself. Right? So you must be understanding that Java has implemented method reference and lambdas from inside. So a lot of code is written by itself. so that you don't have to explain everything to yourself. You just tell us what to do, how to do it, and Java will handle it from within. Okay? So use this method when needed. Another interesting thing is that you are not calling the println method because you have written a bracket here. You are just telling Java to use it. Java will use it when it needs it. For example, when you used to write this lambda expression, you forget this method reference, when you used to write this simple, did something get printed on the screen? Because you have written system.out.println. It didn't happen. Why didn't it get printed? Because again, we haven't actually printed anything yet. We just said that wherever a consumer expected, a consumer expected, and there was a method in the consumer, except, I have written the logic of that method here, till now I have not called that logic i.e. accept, java will call that accept from inside, so the lambda expression that we have passed here, java will take it in a c variable and will call c.accept, we know this, in fact we have shown this in the code also, when we were making our consumer object, let's say we had written consumer integer And we have named it, let's say if you remember some print And here we have given a lambda expression And let's say we have given the same x map to maps to system.out.printl and x Something like this So we were trying to do the same thing there too We were saying that we don't have to print it now I have just told you that this is logic Whose logic? The logic of the accept function And where is the accept function? Inside the consumer We have just told the logic Then when we used to call print.accept and some string pass here, let's say hello. So print.except took this string and saw which logic I want to implement, logic is here, lambda is here, what is logic? Whatever string comes, print it on the screen and it did it. So the same thing happens in method reference, we are just telling logic what to do, java will understand, whenever list.foreach will visit on every element, it will work to print it. So let's quickly do it on code and see. So see, As soon as I came to the code, we have already passed a list.foreach and a lambda expression. You know what foreach does? It takes an object of consumer type, action, and performs some operation on that action. So let's see what it does. What is this lambda expression that I have given? Do you know what logic it has? We have this consumer interface. It has a method, accept. Here is that method, accept. I have written the logic of that right here. So when we put this action object in the action variable, After passing this lambda expression, Java understood that whenever I call accept on this action, action.accept, what is accept? Our method in consumer is accept. So whenever I call action.accept, this logic should be called. This forEach loop is coming from inside. It is using our for loop itself. Basically it is using the normal forEach loop. And this loop is doing this. Now what is this? On which we have called forEach, list.forEach. So basically it is trying to loop through this list. And this is the list of integers. It is filled with integers from 1 to 0. So it says, I will loop through this list. So for every t in this, this will be the integer type of t. And it will call action.accept on every integer. And it will pass an integer here. And its logic is that this should be implemented. The logic is that if you get any integer, then print it on the screen. I am just telling Java this. You know how it was long. We could have done it ourselves. We could have made a class, consumed. Let's implement it to the consumer. Consumer. You already know this. Let me just tell you what we do. Let's consume it in integer format. This is consume. Okay. Then what do we do? Then we override our only method. We have only one method. Public. Public void except. In this. What will it do? It will take an integer. Let's say x. Okay. And what will it do? It will print that on the screen. x. Something like this. Here I forgot to write class. Okay. Okay. Something like this. Now see. I forgot to write class here. I wrote that. Now what I used to do here. Instead of this. I could also do this. That I pass this new consume. New consume. So now Java will understand that I passed its object. As soon as I passed its object, let's say in the action name variable, I called the accept of that action by giving an integer. So whenever you call the accept of the action by giving an integer, that integer used to get printed on my screen. So if I run it now, then you will see that everything should get printed. So without doing this, I will replace it with the lambda expression because it looks easy. I will just do it like this. x tends to system.out.println x. Something like this. Now I am saying that we can make it smaller. We can write it like this. We can write it like this. That the name of our class is system.out. Going inside this class.. out. Method reference operator. And inside it, the function is println. Println. Refer this. So do whatever it says. If you go inside println. So what does it say? I will take some input. And I will print it on the screen. Through some logic. So basically, we are passing a method reference there. So if you run it again, then our 1 to 0 will be printed. Now, the biggest question is, where can we use this method reference? Can we use it everywhere? Can we change every lambda expression, every lambda, with a method reference, with MR? The answer is no, not at all. Why? Look, we can call or use a method reference at the same place where we are using an inbuilt function. That's why we can write this format className, methodReference, functionName or methodName. So let's say you have written such a lambda expression that you take some x and make it x plus 5. Basically, you increase it by 5. So can you replace it with a lambda expression? Sorry, with a method reference. There is no answer at all because there is no such method in Java that takes an input and increases it by 5 and returns it to you. If it would have existed, we would have done it, but because it doesn't exist, So we can't replace it, we have to write the same method reference, the same lambda expression wherever we want it. So now how can we write method reference? So let me tell you, it's very easy. We have written types of method reference. You can talk about any existing function and you can replace it if you want. For example, we have static method reference which refers to a method through a class name. For example, let's say we have an expression that x maps to let's say math dot absolute value of x. Something like this. We have a math class and it has a method abs, absolute, which gives us the absolute value of x. So if we want, we can replace this with method reference and we can simply write Math. Method reference operator. ABS. That's it. Right. Because look what we are doing here. We are doing a pass through. We are passing one x in this method. So we said do it like this. Simple. If we are just doing a pass through. If we were doing something x square here. If we were passing x into x. Then I would not be able to write this. Then this would be wrong. Right. If I am passing my x as it is as a method, I can replace it with my method reference. Java will understand that. If you write x into x instead of x, then it won't work. Because you haven't given x to Java. So Java doesn't know what to do from inside. Right? If you have written this, then Java will understand that I have to call input as it is in this method. Or I have to put it in this method as parameter. As it is. It will not change anything in it. So if the input is x, then it will put x as it is. So what does static method reference mean? You take the name of the class and then the method reference and then you take the name of the function. Apart from this, we have an instance method reference in which you can refer a method by the name of an object. Instant method reference. For example, we just saw that take some x and print it on the screen. So, let's say system.out.println x. Something like this. So, how did you change this? You just wrote system.out and method reference operator println. Okay. You see what you are doing here. You are referring this println name method using system.out. Now system is class but out is object. So you are referring through an object. That's why we have put it in instance method reference. Now again their name is not necessary. It's all static method, instance method. It's just necessary that how do we do it. So here we can do method.absolute through class name. That means system.out method reference operator println can also do this. Just to say this, the way you can refer it in java, in that way we have given its name. Now there is one more way. Let's say you gave another lambda expression. Let's say for every string str, map it to str.isEmpty. Do you know what type of lambda expression it will be? It will be a predicate type of lambda expression. You are taking an input t and evaluating it. Either it is true or false. Or you are basically returning a boolean. So this is a predicate type of lambda expression. So you are just saying that I will give a string. Map it and check if that string is empty or not. Now, how can I change this in method reference? At the time of method reference, I don't have a variable named str, so I can write it simply. In my string class, method reference operator is empty. I can write something like this, and this is also correct. Although I am referring to it through an object, but I can simply say that our string class has to refer to this empty method. Now wherever I have passed this, let's say I have given it a predicate. I have made an object of predicate of type string and I have called it p equal to and I have assigned this. So whenever I call p.test on the screen, p.test and I will give a value here, let's say hello. So my screen should be printed as false because I have passed a string as hello. It is not empty, so it should be passed as false. But if I pass an empty string by doing p.test, then the answer should be true, right? And there is one last type, which we call constructor reference. Basically, you can refer through a constructor, for example. So, let's say, I am saying, you are talking about a lambda expression of some supplier. So, you know what a supplier is. It works on some T and what does T do? It returns. It doesn't take any input. It has a generic form. Let's say, something like this. Void. you have to generate something so what I will do is I will give it a lambda expression that I will not take anything obviously and map it to and every time I will return an array list object so I can write here new array list something like this and here I can also pass let's say integer that I have to pass array list of integer So I can give this lambda expression. That I don't take anything but return an object of array list. So let's say I changed the supplier's object to a small s. And whenever I call s.get in the code, what would I get? I would get an object of array list. So basically I can write the code like this. List of integer Let's say LIST is equal to s.get I don't need to do a new ArrayList here Whenever I need an object for an ArrayList, I can call s.get Now see, how can I change this in my method reference syntax So I can write here Let's say s is equal to Who am I generating an ArrayList with? So I can write ArrayList method reference new This is its syntax It's a very weird syntax, but again, you will understand. Because we are calling a constructor, here we are calling new ArrayList and constructor. So what I will do is, the one I am making, ArrayList, method reference operator, new. And again, many times there is a problem in understanding this, so we avoid it. We write it directly like this, even if we want to make it. There is no need to write method reference everywhere. Many times your code is not readable. So if you know that your method reference makes the code a bit unreadable, it looks complex, then it is better to pass a simple lambda. And many times method reference makes the code very readable. Like you passed method reference operator of system.out. Println. Right? By looking at this, you can understand that you are printing every variable. So sometimes it will make it more readable, sometimes it won't. Sometimes you should use it, sometimes you shouldn't use it. So I just told you that this is the syntax, whatever you are creating, your method reference operator and new keyword. Now, if you have read about all your functional interfaces, which were common, function, supplier, consumer, and your predicate, we have a very interesting thing in Java, that is functional composition. Functional composition is a very interesting thing of Java. And it takes functional programming one step further. Now what does it say? It says that it is better to write a big complex function and break it into small functions. Okay, what does it mean? Let's say you have calculated a result int result equal to And you have written Let's say x plus 2 First multiply x with 2 And then multiply it with 3 So do you know how important brackets play a role in this? We will tell Java to first finish the inner bracket So do x plus 2 And then multiply it with 3 So basically if I write it step by step What are we doing here? First add 2 And then multiply 3 Multiply 3 We are doing something like this. So can I mathematically, I can't write it like this, if I say it like this, then mathematically, I make a function f of x, which does x plus 2, f of x is x plus 2, and I make a new function, let's say g of x, which takes an x and multiplies it by 3. So think, this statement I have written here, This one. That do x plus 2 and then do into 3. If I express this in its form, in the form of mathematics, then is it not equivalent? Something like this. I cannot write it like this. That this is g of f of x. Something like this. Now think, what does it mean here? That I apply g of, g of, but for what? For f of x. Basically, I multiply x with 3, but what is x? This is x, x plus 2. So, first I do x plus 2, and then I multiply this whole expression with 3. So, basically, I can write g of f of x. Right? By combining this equation, this equation, right? What I have to do is, I have broken it into two parts. I called x plus 2 as f of x, and x plus 2 into 3 as g of x. And I have broken it like this. To do these things, we use functional composition in Java. Basically, Java has provided us with such functions and methods in its interfaces, like consumer, predicate, supplier, and our function. By doing this functional composition, we can break complex functions in small functions and make them more readable. Now again, when we look at functions, you will understand better why I took this example here. So first of all, we had an interface called function. You will remember function. Let's go to this interface. This works with t, r. And what is its generic form? It takes t as an input and r as an output. So this is its input and this is its output. We know this about the function. And the method inside it is called apply. Right? We also know this. And if I make it properly, its apply method returns r, apply and takes the value t. Something like this is declared. Right? In this same function interface, we have two more methods which are defined there, not just declared. One is called and then and the other is compose. Now this is very interesting. If you understand this, you will enjoy it a lot. Especially when you write a code, you can break a complicated function in this way. And it will look very readable. The whole concept of functional programming is to make your code as readable as possible. So here are two functions, and then and compose. And see how interesting this function is. Now first of all, let's understand what is and then. Compose is the opposite of that, so you will understand. See, we have a function and then. What is this? Let's quickly understand this in code, it will be easier to understand there. Yes, I have made an empty file by doing demo3. java. So first of all, let me tell you what we had to do. We had an equation that let's say x plus 2 First multiply x with 2 and then multiply it by 3. So, according to the rule of the board mass, first addition will be done because there is a bracket here and then it will be multiplied by 3. So, if I write a lambda expression of this, then I write a function type of lambda expression because I have both input and output. So, I write integer on input and integer on output. Let's say name it equation and something else. like this. If I get x. Then map it like this. x plus 2. Into 3. And I put all of this in a bracket. Something like this. And I close it here. Now if I wanted, I could have printed on the screen equation.accept and passed 2 here. Do you know what my answer would have been? It wouldn't have been accept, it would have been apply. The apply method is in the function. So if I had passed 2 here, it would have been 2 plus 2 would be 4 and 4 into 3 would be 12. So let's run this once. Now we have 12, there is no problem. But I thought, this equation seems a bit complicated to me. Right now it's not complicated, it's very simple. But let's assume it's a bigger equation than this. Which seems complicated to me right now. So I'll break it down into two equations. Okay? So I'll call one x plus 2. Let's say this. x plus 2 is an equation. Now I'll consider this whole x plus 2 as x. And I'll call the other one x into 3. Something like this. So let me tell you what I'm doing. Let's say I make two more functional interfaces. Something like this. Let's make it a little lower. Let's close this. Let's comment. Let's name it add2. And here I'll just do x plus 2. x plus 2. Something like this. And let's name it multiply3. Multiply 3. And I'll just do x into 3. Hmm. So, I have added 2 and multiplied by 3. So, I have broken a big equation into two equations. Now, I have to print this equation. So, can't I print something like this? My answer will be final in int. So, let's say I call this answer ans. And how will it come? First of all, I will apply add2. apply. and let's say I keep its answer in a and here I pass 2 so add here 2 passed so 2 plus 2 is 4 that means a is 4 then I will write int b and what I will do in multiply 3 I will multiply 3.apply and I will pass this a which is above me. So I passed 2 here, so 2 plus 2 is 4 and a is 4. Now I passed 4 here and multiplied 4 by 3. So 4 into 3 is 12 in b and I can print b here. My answer will be the same. So 12 is correct. So I divided one equation which I had written earlier into two parts. So basically what I wrote here, I divided this equation into two parts like this. Now think, do I still need to write something like this? Can I write it like this, int answer equal to, what do I have to do? I have to write it like this, multiply 3 dot apply, multiply 3 dot apply, and here in the same place, pass this directly, pass this directly like this, something like this, and remove this. Now I have only one answer. Multiply 3.apply. What should I apply? Add 2.apply to. And here I will print the answer directly. Answer. Something like this. So now my 12 will come. So am I not doing it like this? That if I multiply 3. I call this g of x. If I assume this. This is my g of x. And I call this f of x. This is my f of x. Okay. This is one function. This is another function. So here I am not doing it like this. g of f of x. Something like this. Right? So basically, on this g function, instead of x, I am passing f of x here. Meaning, the entire body of this function. So this is my g of f of x. I can do this too. But again, this is also complicated. Multiply 3 dot apply add 2 dot apply 2. It looks complicated to hear. So java gave us a function and then. So we can write something like this. I can write int answer to. And I simply tell you how I can write. Whatever I have to do in which format First of all I have to add After that I have to multiply So I will write simply First of all add 2 Then I will call and then First add 2 And then And then multiply it with 3. So add 2 and then multiply 3.apply. And apply all of this. Let's say by giving number 2. We gave 2 only. So here I gave 2. Something like this. And whatever answer you get, print it. Now see, I will have the same answer printed on the screen, which is 12. Absolutely correct. But my syntax has become so simple. You must have forgotten this too. Now my syntax is done. Add 2 and then multiply 3. You can understand what I am doing. First add 2 and then multiply 3. And apply the whole thing using 2. By passing 2. So this is the method of and then. We call and then left to right. That first do left then right. And here in fact I can add more and then. You think I would have done more complicated equations. After this let's say one more would have happened. In which I have to multiply 3 and divide by 2. So then I would divide x by 2. So after that I could also write this. Let's say dot and then divide 2. dot apply to. Something like this. First add 2, then multiply 3, then divide by 2, and then apply the whole thing with 2. So if I pass 2 in this, then 2 plus 2 will be 4, then 4 will pass here, 4 into 3 will be 12, then I will divide 12 by 2, the answer will be 6, so the answer to should be 6. If I print like this, then it will be 6. So, I can do full chaining of my function like this. And see how readable my code will be. Add 2, and then multiply 3, and then divide 2, apply 2. Something like this. I hope you understood this. Now, if I think about how and then works. So, think about what it takes in input and what it takes in output. Let's simplify this. Let's remove the divide part. To make it simple, I have written add to and then multiply3. apply. Now how will it work from inside? It is very simple. I have written add to and then multiply3. Now this and then method is taking in some parameter. See in parameter it is taking multiply3 which is a function itself. So it will take a function in the input. And what will give the output? Now whatever output it will give, we are calling apply method on it. Means obviously it will give a function in the output also. So it is taking an input and an output function so that we can call the apply method on that function and finally we get an integer. So let's go inside and see. It is doing exactly the same. It takes a function in the input and gives us a function in the output as well. Okay, very good. Now what is it doing from inside? Actually, what is it doing? Look, can't I divide this line, the line of answer 2, into two parts? In one part, I should do this. function integer comma integer. Let's say I say this. Let's call it answer function. And here I will pass this much. Add 2 and then multiply 3. Because this function is returning. And finally I will make an integer answer 3. And on this answer function I will call apply. I can also divide one line into two lines and print the same answer. Now focus on this line. Add 2 and then multiply 3. This line will implement the default method in Function.java. If I want to implement these two things, add2 and multiply3 in one equation, then I will do multiply3. apply and pass add2. apply so that it becomes 2 plus 2 is 4. And if 4 comes here, and if I pass 4 on multiply3. apply, then basically 4 x 3 becomes 12 and the entire answer becomes 12. Right? So I am doing this. Now look here, in this answer function, I called addTo, now forget this for now. So I did addTo and then multiply3. So in and then I passed multiply3, which was one of my functions. So I passed multiply3 in the parameter. Now let's see its implementation. This parameter has been collected in this after name variable. And see what it is doing in return. It is checking objects.require non-null, which should not be null when you are passing after. Apart from that, you see, after.apply. It is calling apply on this after. Means it is calling apply on this multiply3 which we did. Multiply3. apply. Right? And what were we doing from inside? We were passing our second equation addTo.apply. So it will do this. After.apply. And what should we pass from inside? Call this.apply which is the apply of the current object. Now what is this? This is where we are calling add then. Basically on add to. So when we are writing add to.add then, then the apply method will also be called add to. So we are doing the same thing, multiply.apply and add to.apply from inside. And finally we passed t from inside. So this became my entire expression which is as same as what we wrote here. Okay, it's exactly the same. Whatever we have passed here, after dot apply and in the bracket, this dot apply. Here it has written this because it is again optional. Simple this dot apply and pass your variable. Now see, if it had done it like this, which we have done here, then it would have returned an integer. But it didn't have to return an integer, it had to return a function. Why did it have to return a function? You think, it had to return a function so that we can put another and then on top of it. Right? We can put another and then here too. and then let's say divide 2. Right? And we can apply that only because it has returned a function. If it had returned an integer, then we can't apply and then on it. So that we can do chaining, no matter how big. So that we can do like this, f1 and then f2 and then f3 and then f4. That's why it will always return a method, a function. So look, if it returns a function, will pass a lambda expression instead of that function. Which it can obviously do. So here it passed. Take a t, which is our input. And call this on it. So again you got a function. On which you can finally apply a call. And your work will be done. Now see an interesting thing. Let's say I have two functions. Function. and I have another function let's say r, v and I call it g something like this now imagine when I apply their composition I write something like this f dot and then g right What does this line mean? This line means that first do f, whatever is written in f and then after that do the work of g, okay? Basically we had written that first add 2, dot and then multiply with 3, multiply 3, dot and then apply and we had passed a value here, let's say 2 and our whole work was done. So basically I can write it like this that first f and then g and then g First write f and then write g. Right? I can write it. Now you think. Let's say my f was x gives x plus 2. And my g is x gives x into 2. Something like this. I had called it add 2 and multiply 3. Here I have called it f and g. Simple. So when I will write f and then g. It means something like this. Where we discussed above. It means something like this. g of x. f of x. This is what it means. So basically it is as equivalent to g.apply and pass f.apply in it. And give some value, let's say 2. And this will be fully implemented. So let's say I gave 2 on f.apply. So as soon as I pass 2, 2 plus 2 will be 4, x will be 4. And I passed this 4 here and called g.apply. Now when g.apply calls 4, then this call will be 4 into 2. And 4 into 2 is 8, so this x will be 8. So we know this. So my answer to this equation will be 8, which is good. But think, when I implemented f, it returned x plus 2. Now x plus 2 is an integer itself. So basically it took an integer and returned it. So finally when it returns an integer Who took this integer as an argument? G took it G.apply and worked on this integer That means it will also take an argument as an integer And finally it will return an integer Because it is also multiplying x into 2 So here all the integers We didn't get any problem But you imagine a case Where there is an f and a g And the f is integer Comma string And g is is string comma string. String comma string. Right? Now imagine this. Now if I write f dot and then g So it means that first do f and then do g But if you perform f first Basically which means g of f of x This is what it means But if you perform f of x first Then it will convert the integer into a string So as it converts f of x into a string Then it will finally give an answer in a string Now you are performing the g operation on this string What can you do? You can do it Now g takes string in input and string in output So it will convert string into string So this will work But you think, will we be able to write it the other way around? Can I write it like this? G dot and then f. Can I write it like this? The answer is no. Because what does this mean? This means the opposite. f of g of x. So first do g and then f. So first if you do g, it will take a string input and convert it into a string. So here the output will be a string. And you are asking this string to work with f. But f doesn't work with string, it works with integer. So you can't write this here, it won't work. Right? So what I mean to say is that if I make a generic form of this, then g of f of x will only work if f takes, let's say, t comma r and g takes r comma let's say some v. Okay? After this, you can do g of f of x. Something like this. Why? Because you think, first you do f of x, f of x will take t in the input and r in the output. Now the output of r, which will it bring? It will bring g, right? And g itself, And G takes R as input and V as output. So basically, the output of F should be the input of G, then only it will work. Consider it like a chain. Here you have F and after that G is connected to the chain. and from here you provide an input and from here you get an output. So basically, you can imagine this whole model from the inside. What is this? This is what it represents from the inside, g of f of x, something like this. That first your input passes through f of x, then passes through g of x, and then you get the output. Something like this. Whatever input you give in f of x, you give it the same as the input you expect, you give it t, but it returns r. Now, the R that is returning, G of X should accept this R. So, if G of X accepts this, then it can return something else, V. Right? So, you can understand it this way. Its output and input should be the same. Okay. So, the final answer of G of F of X, in which format will we get it? You know that it is a function only. And what should be the return type and input type of that? Now you understand this whole model together. It takes t as input, so from here you give t as input. f of x gives output r, so here you give out output. Now this r input takes g of x and finally output gives v. So here you gave t as an input, I will write it outside. You gave t input here. And you got v as output. For you, the entire box is the same. You don't know that f and g are happening from inside. According to you, the entire operation is performing the same. So, think that you gave the input t and you got the output v. So, g should not work like this. Basically, the function that will return you will take the input t and give the output v. Something like this. The input and the output of this will be combined to get the function t, v. So this is it, you have to remember how it will work. And this is true for everyone. Now we will study Compose, if you understand it, then you will understand it as it is. Now let's move on to our next method, Compose, which is very easy because it is the opposite of this. Now let's take the same thing, you had a function f, You said that x gives x plus 2 And the function g you said that x gives x into 2 And if you write f dot compose g So this means the opposite of and then If you write f dot and then g And then g So you say first do f and then g Basically g of f of x Right? But f.compose g goes the other way around. It says, first do g and then f. Meaning, f of g of x. Exactly the opposite of this. So, you can think of it like this. f and then g goes left to right. It says, first do left and then right. And compose goes right to left. So, whatever you have given in the argument, first do that and then perform f. So, if you see its internal implementation, it will be exactly the opposite of this. If you talk about f.compose g, then I will express it in brackets. f.compose will be something like this, first it should be g, means first multiply x with 2 and then add 2 to it, something like this, this is called f.compose and you must have understood what and then g says, and then g says the opposite, that first add x with 2 and then multiply it with 2, something like this. So this is the difference between the two. Compose works the other way around, right to left. If you see its internal implementation, it will be the other way around. We can apply the same procedure on this as well. Let's say I have two functions. One function is f and the other is g. Let's take their return value first. This works on t, r. And this works on v, t. Let me tell you what this is. Now think about it. So think of this as your f and this as your g. When you write f.compose g, what are you doing? You are saying that first perform g and then perform f. Meaning f of g of x. Something like this. So if I make its boxes, then first g of x should be used, then f of x should be used. And by combining all this, And by combining this, it represents you as a box in which you give some input and you get some output. Right? Now think again in the same way. What is g? It takes v in the input and gives t in the output. So what did g do in the overall input? Here, g took v and gave you t in the output. Now, this f of x should also take t, so here it is taking t in the input and finally returning r. And finally, what did I say? It is returning r. Right? So according to you, it takes v and finally returns r. So the return type of this function will be V, R. It will take V as an input and R as an output. It will take V as an input and R as an output. This time it will get its input and its output. And because you are doing g of f of x, so its output should match with its input. These are common things, which are basically mathematical things, I hope you are understanding. I hope you are able to understand. These are very interesting things. You just have to remember the basics. Which operation is going on above the previous one. that. So basically, you can use both functions. Whether you use AND or THEN or you use COMPOSE. Do you know what you make by using these two functions? You make a pipeline of the method. You make a pipeline of the method. You write something like this, first do f1, dot and then let's say f2, dot and then let's say f3. Something like this. And then finally apply all of these with a value. Let's say you gave a value of 5. You didn't give a complete pipeline. So we understand that first we have to do this, then this, and then this. And what is the input of these three things? It is 5. So 5 will come in the input first. It will go from here, it will go from here, and finally we will get some output. Right? We are understanding this from this. And the compose is the opposite of this. Java has given you both the things. You understand left to right better or right to left. You can either use and then according to that. Or you can use your compose. Use both of them. It's all about understanding. Generally, people prefer and then because it's more intuitive. Now, can you use method references in and then and compose? The answer is yes. You can use method references too. Let's say I have two functions. again it works on all strings so string input, string output and let's say there is one more function which will work on two strings again string input, string output and let's say there is one toUpper which converts a string to upper if I write its lambda then let's say it takes a string and calls string.toUpperCase you know this and let's say I have length which will take a string and tell its length str.length Now if it is telling the length, then its return type shouldn't be a string, it should be an integer, right? So I will make it an integer. Integer So you know if I would have joined these two and composed them. So let's say I could have composed something like this that I would have got a function. And you know if I have to do two upper first and then calculate the length then what function would I get? String comma integer because I am taking the input string and finally giving the output integer. And let's say I would have called it result and I would have done something like this. I would have written two upper dot and then length. apply will not be applied here because we are returning a function here apply will come after this so I would have provided something like this to upper and then length we know this but I am saying that I can write a lambda expression instead of this I can write method reference here so I can write like this that let's say its string method reference operator to uppercase and here also I can pass string Method reference operator length. Something like this. Even then it will work. So I can also use method references with my compositions. With both end then and compose method. And again everything will work the same. Now many people think that why do we have to complicate it so much that there is any benefit from it. There is a lot of benefit. You think that you get a string in the input. And you have to go through a lot of filters. And finally you have to output a string. And if you want the code to be very readable, then imagine how much benefit it will have at the end. If I have a string let's say hello And I know that in this string there will be many extra spaces at the end and extra spaces at the start So I have to trim it So let's say first I have to trim my string So that my string becomes like this without extra space. After that, let's say I want to convert it to uppercase. So, all my characters should be converted to uppercase. Something like this. I want to perform these two operations. So, I can write it like this. You already know in your logic. So, you know, let's say I make two lambda expressions. I make a function of string comma string. I write it trim. And you know what it will do? It will directly call the string's trim operation. I passed it through method reference. And same, I made one more function. of again type string comma string and let's say I said to upper and again you know what it will do? it will call to upper case of string ok now I just have to write this much ok now think I just have to write statement like this I will write trim dot and then to upper means convert to upper case And let's say apply a string hello to all of them. Hello, which has extra characters. So let's say I left some space here to show spaces. So as soon as I write this, all your work will be done. And see how readable the code is. We are giving priority to functional programming in this. First I have to take a string, trim. First I have to take a string, let's say hello. What I have to do on that is trim. Then I have to do upper. And after that I can write something else. Let's say I have to reverse it. Or I have to remove some substring etc. So because of functional composition, I can combine complex behaviors like first do one thing then do another using and then or using compose and this code becomes readable. And the concept of functional programming is that we have to make our program readable. We have to show what we have to do rather than how we do it. Which is also called abstraction. So trim dot and then to upper dot apply hello. Something like this. Now, very interesting thing, if you can have chaining in functional, then there should be chaining in predicate as well, right? Absolutely. If you go to the predicate interface, it gives you three more methods. One is AND, one is OR, and one is NEGATE. And this is a very interesting method. It works exactly the same way as your AND, THEN and COMPOSE work in the functional interface. Do you know what predicate is? Predicate is a function that returns true or false. If any object returns true or false, then imagine if I can join multiple conditions, then I won't need if-else. What I mean to say is, let's say I give you a problem. Suppose, I want to tell you that I will give you a number x that should be greater than 100 and that should be even. So, x modulo 2 should be equal to 0. You have to check these two conditions. So, one way is that you write like this, if x greater than 100 and x modulo 2 equal equal 0 so return true return true or return false something like this or if you don't want to return you have to perform some action here else you have to perform some more action then you can write this type of code but now think that in my program I have to check this condition again and again so every time I have to write this code in if else which will become a little complicated and won't look good So predicate chaining means that you make a predicate of this whole thing. How? See, what I simply do is I make a predicate. I make a predicate. Which will take an integer. Here I will keep its name. Let's say is greater. And it will take a lambda expression from inside x. And it will check x is greater than 100. Something like this. Same I made one more predicate. Again it will work on an integer. and this will check is even and this will check x whether x modulo 2 is equal or equal to 0 you say these are predicate independently so these two will return boolean variable so I can join them I can join them like this is greater dot and a function and pass in bracket is even So this is also a predicate. I can take it in a predicate. Predicate of type integer. And I will name it result. Something like this. I got a result. So now whenever I want to check, I can check this result. Or I can check like this. Is greater and is even. Now see how readable it is. I know what will happen and again. There will be a method, a default method in my predicate. Which again will take a predicate in input. That's why it took a predicate here. And will give predicate in output. As we saw earlier, because I can add a dot or and after this, or I can add another dot or and after this. Right? So again, I will take it in the predicate input and give it in the predicate output. And so that I can change my methods. It will work in the same way. Now this and works in the same way as our and operator works. Exactly the same. It says that this should also be true and this should also be true. If both are true, then only I will consider myself true. If not, then I will not consider myself true. Right? So again, if you see its code from inside, then it implements it like an AND. See, if we talk about our predicate, then we make two predicates here. Whatever we saw as an example. So let's say, one works, sorry, one works on the predicate of integer. Let's check this, is greater. And here we gave the calendar expression, x maps to, x should be greater than 100. Something like this. And again, let me make one more predicate of type integer. Which integer, which will do, will map is even. Okay. And it will take an x and map x should be even. So, modulo 2 should be equal to 0. Something like this. Okay. Here, it will come equal to. Okay. Right. So, now I can check like this. Let's say, I will print it and see what is is greater dot and Again, if I take a predicate here, then I gave here is even dot here I can pass test and let's say I pass 102 102 so what happens the answer should be true 102 is above 100 and it is divisible by 2 it is even so again if I print it then my answer will be true so again I combined multiple methods if I go inside this and it takes a predicate and gives a predicate Now I have passed it as isEvenNumPredicate and it has been called as other from inside. So what I have done is that it must be using the AND operator from inside. So what will it do first? It will call this.test which is my current on which I have called, on this greater. It will call its test and it will call other's test. And if the output of both of these is true, then only the answer will be true because the AND operator is calling from inside. And you know what AND does? If both are true, then only it will be true. If even one is false, then the answer will be false. Right? So basically, it is calling the test method of both. So is greater and is even means it is testing both. It is testing both using AND operator. See this. And finally, it is not returning a boolean, it is returning a predicate. So it is returning this condition as a lambda expression, so that you can put another predicate on it. If I want to add something more on it. Okay, if there is an add, then how will it work? Now look, this or method, It works exactly the same way as our OR operator works. This one. Okay. From which, if any of these conditions are true, then our answer is true. So, here I can write OR also. Okay. Is greater or is even. If any of these conditions are true, then our answer is true. For example, if I pass 56. Sorry. If I pass only 56, then here my is greater is not true, but is even true. So, my overall answer should be true. It is absolutely true. Right. So, if I see OR's implementation from inside. Again, it also takes a predicate and gives a predicate. And see, it tests it. The current one and the other.test. It calls the test of both of them and puts an OR operator between them. And finally returns a lambda of this logic. So I hope you understood how OR works. In fact, we have another method which is negate. And it works just like my not operator. Just like not operator. So again, let's see this. This function I made is even. Similarly, I will make another function. Let's assume I want to make another function, isOdd. And to make this, I don't need any lambda expression. I can just write, isEven.negate negate this even and this will be my lambda expression. And this will be my expression for isOdd. Now you will see, it doesn't take anything from the input. It doesn't need any input. It just negates the call on which it is called. How? See. On which will the call be called? So whatever output it gets, it has reversed it. It has put a not operator from inside. So if true comes, it becomes false. If false comes, it becomes true. And simply it didn't return true or false. It has to return a predicate. So it has made it a lambda expression and returned it. So that we can put another and or negate on this negate. Okay. So you can think for yourself. System.out.println If I pass a number here, 45. In this odd. Let's say I called. isOdd and here we call test and pass the number let's say 55 so what do you think the answer should be true because my 55 is odd so the answer is true now you think is even on negate, I have negated it again. What is the negate of negate? It will become is even again. So it will be working like is odd is even. So I can do all this. I can change it in any way. Mainly I have only three functions. So mainly the function I have is and, or, or negate. They work exactly the same. Like my and operator, or operator, or not operator works. And their implementation is very simple as we saw. It uses this operator from inside. So look, to show this, I will tell you a real life scenario. You imagine we have a student class. So class student. class student and I will take two things int marks and student age and I will make a constructor public student and I will take two things marks and age and I will assign it this.marks is marks and this.age is age something like this Now I will tell you that I have to check the condition. So I have to check that the student should be passed. So pass means that the marks should be greater than or equal to 40k. And student should be adult. And adult means that the age should be greater than or equal to 18. We know this. Here comes age. Okay. So I am saying that I have to check these two conditions again and again for eligibility. Let's say if these two conditions are true, then only our student will be eligible for a particular event. So there will be an event in the college and our student will be eligible to sit in it. So see how I can check. A normal way is that every time I put if else, if marks dot, if student dot marks is greater than 40 and student dot marks is greater than 80. and student.age is greater than 18 and that is a very complicated method you know there will be a lot of if-else and suppose I have to check it again and again so the best thing is to make a predicate of it so let's say predicate of student I will say past equal to so let's say for every student you give me if you give me an s then I will map it such that s. whatever its marks are should be greater than equal to let's say 40 something like this and again I will make a predicate predicate of type student Student. And here I write is adult. Is adult. And here I say give me any student S. I will map it such that S.age should be greater than or equal to 18. Right. So I did it like this. Now I make any predicate. Let's say student. And here I write is eligible. Is eligible. Now how can I check eligibility? Is eligible. Now I just have to check one thing that it should also be passed. So past.and and here I can pass is adult. So it should also be adult and should also be passed. That will be eligible for me. That student will be eligible for me. So if I want to try the eligibility of any student, let's say I print it and see. Let's say I write a new student. What is eligible? Is eligible? Who is eligible? I will test iseligible.test and pass it to a new student. Let's say I pass his marks as 50 and age is 19. So is he eligible? The answer is absolutely eligible. If I run it and see, then I should get true which came. But if I change one thing, let's say his age is 17, then my answer should be false. So, you understood how we use AND. So, this is its real-life use case. Again, it works like this from inside. Instead of AND operator, it works like OR operator and NEGATE operator. Now, see how readable it is. I didn't have to write any IF-ELSE. I can simply pass things like this in my code. If you understood predicate chaining, then there is one last OR, which we call consumer chaining. And this is the simplest chaining. Because you know, the structure of the consumer is such that it takes an input and doesn't give an output on T. Void. So the chaining of this is the simplest. There is only one method of chaining in this, which is called and then. And see how it works. First let's understand why we will chain on the consumer. What is the work of the consumer? That it will take an input and will not return you anything. So why do we have to chain on this? You assume that we have to take an input T and we have to do a lot of things on it, but we don't have to give an output even once. Let's say we have to print something once on the console. We have to print something once Let's say log file. Log file. And we have to print something like this on some other normal file. So you have to perform these three operations on an input. But you didn't give output in any of them. You just printed it. In that case, and then is very useful. Now think, how did the normal ANDTHEN function work? First you did f, then you did g, and this means that you wrote g, right? That first you perform f, then g, and you assumed that this is a container, you will give one input and you will get one output. That is, its input for this That means the output of this will work as input for this. But there is no output in the consumer. So a simple way to change this is that let's say you have made a lot of consumers. Let's say A, B, C, D. And you give an input T. So all these consumers will work with this T input. And all will consume it. They will not give any output. All will eat this input. So let's see how it works. So here we are going to see the end then method of our consumer. So what we will do is that we will print a string. So we will print it twice. Once we will print it normally and once we will print it in uppercase. So these two works are done. So I will make a consumer of type string. Again I will have to import the consumer. So I will import it here. Let's say star. Okay. So the consumer has been imported. So let's say I'll tell you one. PrintName. What will it do? What will printName do? It will take any s and print it on the screen. So system.out.println s. Something like this. If I want, I can write like this. Or I can use my method reference as well. So basically I can write like this. System.out. You can directly refer the println method. Okay. Now I'll make another consumer like this. Let's say consumer of type string. And let's say it does print. uppercase print uppercase and see what it does let's say it takes a string s and prints it using uppercase so s dot to uppercase to uppercase I write a method reference for this? Because I can't. I'm not just printing the string, I'm printing its uppercase. So if such a method doesn't exist, I'll write it in lambda. Now think, what if I make my consumer simple. consumer and let's say I name it pipeline which will do a lot of operations together it will print my name as well printName. and then it will also call my printUpperCase now see how interesting it is now what if I call accept method in my pipeline and pass a string let's say aditya Aditya will be passed on my screen and it will be printed on my screen again in uppercase. So my Aditya will be printed twice. Aditya Aditya. So I did two things using and then. Print name and then print uppercase. So if you want to perform multiple tasks without giving any output, then you can use and then on the consumer. Now if we look at the implementation of and then, then again it will be very simple. A consumer will take it, a consumer will return it. And what will it do? It will call both except. Its except, which is this.except and after.except, which is coming here as input. It doesn't return anything, so there is no sense. And again, we will make a lambda expression of it and return it. Okay? So I hope you understood the consumer too. Now if you understood this, then all the interfaces we had seen, function interfaces, one was ours, Like we saw function, we saw consumer, we saw supplier and we saw predicate. By functions also exist, as we call by function interfaces. Basically, this function works on t, r. This works on t, supplier also works on t, and predicate also works on t. Because you know, function takes t, gives r. Consumer takes t, doesn't give anything. Supplier doesn't take anything, but gives t in output. And predicate takes t and gives it in boolean output. You know all this. What if I have a condition that I need two inputs, let's say u, v, and I give one object r in the output, right? This is also a function, but here I am taking two inputs, but I am giving only one output. In that case, I have one more specific function, which I call by function. By function. And this works on T, U, R. Where T, U is the input and R is the output. Everything else is the same. Now when you see the method inside this is apply. it will take two inputs t, t and u and return r something like this and same we have a byConsumer you can understand how this works byConsumer will work on two inputs t, u and will consume both of them And the same will happen for ByPredicate, which will work on t, u and these two will be the inputs. And it will return a boolean. You can understand how it works from the inside. The test method of this will be, earlier it used to take only a t, t and now it will take u as well. It will return only a boolean. The same way our consumer will work. It has the method accept, which used to take 1T, now it will take UU and the return will be void. There can't be any buy alternative for the supplier. Because the supplier doesn't take even 1 input. So why will it take 2 inputs when it doesn't take even 1 input? It doesn't mean that it can give only 1 output. And the output is always 1. Supplier always works single-handedly. But function, consumer and predicate also exist. I hope you enjoyed it. We learned a lot from this. It was a full lecture. But now you must be understanding one thing. Our object-oriented language was very imperative. What does imperative mean? means that if I tell you to do something, then it will tell you how to do that work. For example, I will tell you to print all the elements of a list. So you will do something like for i equal to 0, i less than n, i plus plus. And you will print something inside. You will say no, we will use for each loop. No problem. Then also you will write like for every integer i in this list. Do this. So this is imperative. It means it is telling how to do the work. Which is absolutely correct. There is no problem in it. But our functional programming brings a new approach. It says that I am declarative. Declarative means that I will tell you what to do. So basically, what you are actually doing, write it as it is. I will not tell you how to do it. How do we write in functional programming? You write like this. Do this dot and Then and then let's say do that Something like this So you just do this and then do that You are calling like this Your work is getting done We don't know how the work is getting done from inside Even the for each loop you can use You just write list.forEach For every element Call println We just pass method reference here Let's say system.out Our method reference operator and println works. So we don't understand how it works from inside. We just know what we have to do and write it in steps. If you go into this and then, you will understand what it is actually doing. But it is very declarative. Do this and then do this. Or the predicate we saw, and operator, negate operator, or operator. Right? A lot of operators. Or how can we chain them in different ways. So this is very declarative. Okay? That's all about lambdas. I hope you understood everything about lambdas. Remember, lambda is a very important topic in Java. You should understand it well because modern programming we use functional programming a lot. In fact, in the upcoming lecture, where we will talk about streams, it also uses lambda a lot. So, lambda is very important. Try to understand it well. If you didn't understand the video once, you can watch it again. You will have a lot of fun learning it. Because it is very useful. It will be useful in interviews as well as in your project. Let's meet in the next lecture. That's it for this lecture. Till then, thank you so much.
