Java Functional Programming | Full Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
yes it's okay [Music] [Applause] [Music] what's goin on guys welcome to amigos code my name is Nelson and in this course I'm gonna teach you everything you need to know about functional programming with Java so when I first saw functional programming I was like you know what is this I saw functions by function predicates being used all over the place and to be honest I only realized the power of it when I learnt about the clarity of programming so you see most of us write imperative programming which means that you define every single implementation detail of your code whereas declarative simply tell it what you want instead of defining you know the details for example you might have a variable or actually you might have a loop right so you define you know the initial value how to increment it and how to stop it right so with declarative programming you have none of that you seem to tell it what you want and that's what I'm gonna teach you in this course so in this course I'm gonna teach you exactly you know what the differences what the differences are between imperative and declarative programming and why you want to move away from imperative programming then I'm going to teach you about the functional interfaces that Java has to provide so functions by functions predicates consumers suppliers and all that good stuff right so once you learn that you can then start to use streams and optionals to write code in a declarative way and then towards the end I'm gonna teach you one of my favorite design patterns when it comes to functional programming and that is the Combinator design pattern which is really really awesome it's really good for validation where you simply define all of your validations in line interface and then you exactly step-by-step what you want your validation to be it's really really awesome and finally I'm also going to teach you exactly about lambdas Co Banks and the properties of functional programming so that you fully grasp exactly the whole concept so if you haven't subscribed to my YouTube channel go ahead and subscribe because that way I can you know provide more awesome content like this without further ado let's go ahead and learn everything you need to know about functional programming with Java all right so before you start this course I want to make sure that you really learn from this course because I want to add value so what I want from you is to go ahead and practice as I teach because that's the best way for you to learn any new technology also go ahead and join the private Facebook group so if you have any questions you can go ahead and engage with other students and the group is full of cool people just like you so this is all I wanted to say for now and enough of me talking let's go ahead and learn about functional programming alright let's go ahead and bootstrap our application where I'm gonna show you all the examples throughout this course so I've caught IntelliJ right here and if you've never used IntelliJ then I highly suggest you to enroll to my free course where I teach you pretty much how to use this awesome IDE and to be honest as a software engineer this is probably the IDE of your choice when it comes to Java back-end development so let me go back to IntelliJ and the first thing I'm gonna do is to create project and right here I'm going to pick maven now the project SDK it doesn't really matter whether you use Java 1112 or any other version as long as is above 1.7 so I'm gonna stick with 11 right here and as I said the version does not matter next let me go ahead and click Next and then right here for the group ID I'm gonna say com don't and then and migos and then code so right here this could be your own organization and right here let me simply name this as Java and then functional and then I'm gonna press Next and then I'm gonna save this and the code and then Java functional and then simply press finish there we go let me actually collapse this maven tab right here and also limit collapse this and right here go ahead and simply enable order import to import dependencies and this is the actual pom file right so if I open the functional or actually Java functional folder inside you see that we have only the pom file and this contains the group ID artifact ID and then the actual version so let me just remove that line and also remove this pop-up and finally I'm gonna put this full screen and this is all and actually let me just close this because we're not gonna need this pom file and this is pretty much everything you need in order to bootstrap the application to follow along so obviously I'm gonna give you the source code at the end of this course but I highly suggest you to practice as I teach because that's the best way of you learning any new topic this is all for now if you have any questions about IntelliJ as I said go ahead and roll to my course right here where I teach you everything you should be aware of this awesome IDE let me go ahead and give you an example of imperative programming so imperative programming involves you as a developer defining every single detail about your implementation so right here inside of the source folder inside of main and in Java let's go ahead and create a new package so you can right click on this folder or I'm gonna use keyboard shortcuts and also you can see down here so right here you can see all the keyboard shortcuts that I'm going to use throughout this course so right here I'm going to use the error so I'm gonna select package and then right here my name this has impaired teeth and then okay now inside go ahead and create class and this will be called as main now let me collapse this and inside I'm gonna type P s and then V M for public static main there we go and now we have a main method that we can run so let's go ahead and define a list and the type of this list will be of type and in person so right here let me go ahead and simply name this as people equals two and then list and then dot and then off so right here we're gonna do is simply create filled people or actually a few people of trained person so right here let's go ahead and simply create a class so let's go ahead and say static and in class and then person and this person will have a name so private and then final string and a name and we also will define a gender so enum and this will be in gender and the gender will be male or female and then right here what we're gonna do or actually underneath is simply say private final and then gender and that will be the gender and let me add this to constructor and there we go so now we have this class right here called person and let me also define a two-string method right here so two and then string and there we go so now right here we can simply say list off and then kneel and then person and right here let's give it a name so John and John is a male and let me have a static import for this and let's go ahead and duplicate this so I'm gonna press command D couple of times and there we go so let's have him rear and she's a female let's I've Ayesha she's also a female just like that let's have Alex is a male and let's finally have Alice just like that and Alice she's of course a female so now that we have this list right here let's go ahead and find out how many females that we have so the way that we will do it with the imperative approach is let me add a common right here so in an imperative approach is simply by doing the following so first we need an empty list and then of type and then person and then let me simply say females right here equals to list of and then or actually not even list of best co-editing say new and then array list so this is an empty list now what we need to do is to loop through people so let's go ahead and we say 4 and then person and then person inside of people and right here we're going to say if and a person actually let me go anything to say female and then dot equals to person dot and then gender just like that so if this is the case I'm going to say females don't and then add and then person just like that and that would say my column and then right here at the very end what I'm going to do is simply print each people so again let me simply copy this just like that and then right here this instead of people this will be female females and this will also be female and then South just like that so system dot out dot print line oops just like that and then female now if I go ahead and pretty much run this class right here so let's go ahead and run this class okay and there we go so right here you can see that we have only females in our output and basically this is pretty much what imperative programming is so basically we are defining every single detail so right here you can see that first we have an empty list and then we loop through that and then we have an if statement and finally we have a final for loop looping through females and you can see that there is a lot of logic not you know you know a substantial amount of logic but you see that for example this if statement is some logic and also you can see that this is a lot of code for something very simple and this is what I wanted to show you about the imperative approach next let's go ahead and implement this using the declarative approach and you will see that it's much neater and we pretty much just tell it what we want to do instead of defining every single implementation detail right here you so to use the clarity of approach with Java ie functional style programming we simply have to do the following let me let me collapse this so right here so what I'm gonna do here is instead of us creating a list and then having to fold ups what I'm gonna do is the following first let me go ahead and pretty much have a comment and right here this will be declarative and then approach now the way that we use declarative approach is by using streams with collections so don't worry about so much about what if stream is because I'm going to show you exactly later on but for now just follow along so go ahead and simply say people so people is the original list right here so this list of people and then dot and then stream right here so now we have a stream and a stream allows us to go into an abstract mode where we simply tell it what we want so right here let's go ahead and simply say dot and then filter and what we want to filter is basically this so right here we have that person so this takes a person and then I'm gonna say the exact same thing right here so I want to take this if statement whoops let me press come on W couple of times come on C and then put it here there you go so you can see that I'm filtering and then this takes a person and then I'm using this same if statements basically female dot equals and then percentage enter right and then finally I'm going to say don't and then collect and then collectors and then two lists and now it's a my column and right here what I'm gonna do is put this on a new line and the same way to collect just like that and right here we can simply now go ahead and simply say dot and then for each and then we can use system dot and then out and then column column and then print line so now let me actually add right here so let me simply add this as a South so I'm gonna print this just like that there we go and let me do the same here so I'm gonna copy that and then paste that take this comment put it here so you can see exactly in the console what I'm doing so now you can see that this is much concise and right here you can see that I'm not creating a list and then adding to that list and then doing the if statement so on and so forth so if you see that is very straightforward and I'm just telling what I want so from the people array list or a list basically this is a list I'm gonna stream so I'm gonna go into this abstract mode then I'm going to filter all the females and then I'm gonna collect to a list and finally I'm gonna print everything so technically I don't even need this collect to list because I'm simply printing out things so let me simply run this for now so you can see exactly what's happening there we go so basically you see that we have the exact same output right so if I put this whole screen if you see that we have the exact same output but the declarative approach is much neater so obviously if you were to have this into a list right here so this is where collect comes into play and then I can remove this line and they switch Sam I call and then extract this to a variable and then say females and then to write and you can see that now I've got a list right here and I could again simply say females to dot and then for each system dot out called column print line so if I run again you can see that this is the exact same output so there we go now you should have a full understanding of the differences between declarative and imperative approach so obviously I've just shown you you know the difference between both but there are few things that we need to understand in order to write code like this right so I'm going to teach you about functions by functions predicates by predicates consumers and you know all the good stuff that Java has to offer when it comes to functional programming if you have any questions go ahead and join me a message otherwise let's move on alright let's go ahead in the section learn about the Java dot util dot function package so basically this package contains functional interfaces that provides target times for lambda expressions and method references so each functional interface as a single abstract method basically you can have one interface with multiple default methods but you must have only one single abstract method to be considered as a functional interface so basically you can see some examples right here so the assignment the assignment context so this is how you for example define a predicate and this is how for example you use it within the actual you know stream so right here you've seen that we can get stream from a list right so stream and then filter and there's another example where you can do some casting so this package right here contains a bunch of functional interfaces right here you see by consumer by function binary operator by predicate boolean supplier consumer and the list goes on so they are pretty much identical apart from the type so the ones that you will most likely be using our functions by functions predicates by predators and consumers and also there is a by consumer but I'm like but I'm gonna explain exactly how everything fits together because once you learn those main ones then the rest will be very simple so right here I want to show you that so we have a predicate so where it's a predicate so let me find predicate so right here so a predicate let me quickly show you a predicate and show you that we actually have used you know a predicate before so a predicate represents a predicate basically this product that represents a predicate boolean valued function of one argument so let me go back to IntelliJ and right here remember where we had this declarative approach so right here so this filter basically if I pretty much extract this into a variable you can see is that this is a predicate and look at this this predicate looks like this so the predicate accepts a person and then returns true or false based of that person ie whether the person is of type female right but what we've done right here we simply inlined like that that's why you don't see the private key word but indeed this is a predicate and a predicate looks like this so don't worry about this if it doesn't make sense but I'm gonna try my best to explain you exactly how you go from you know normal Java functions to the functional interface world join me in the next one all right so I've gone ahead and created a new package right here called functional interface and inside of this package I'm gonna go ahead and create a class and first I want to name this class as underscore and then function and I'm gonna explain why I'm saying underscore right here but for now go ahead and simply say underscore and fun and this called function or you can name this anything other than function without underscore right so right here what I'm gonna do is simply say public static void main you have a main method and right here what I'm going to do is simply do the following so let's go ahead and create a function so static and then function so this function will take a number right so take a number and then increment that number by one so let's go ahead and simply say int and then increment and then this will take an integer number and then it will return number and then +1 so now if I go ahead and simply say increment and then zero and then end up with semi column and if I pretty much extract this to a variable and for now let's go ahead and simply sout this increment and let me put this fullscreen let me run this now oops I'm running the wrong one so I need to run this function there we go so you can see that the result is one very simple so if I put one right here this will be two so what I want to show you is how we go from this so this is what you are used to to this right here so let's go ahead and learn about the funk an interface so where is a function so this one right here so this says that represents a function that take that accepts one argument and then produces a result so this is pretty much what we have here right so this function that we have so this is a normal function in Java so this is what you are used to so this takes an input and then returns one single apple right here and this is pretty much the equivalent right here so represents a function that accepts one argument and produces a result so the way that we convert this into a function right here is the following so let's go ahead and simply say function and if actually delete this so I want to show you something so function you can see that this function takes a tea right so tea and then our so basically these are the data types of the input and the output so let's go ahead right here and simply say that this will be of type integer so the input is integer and the output is also an integer right here so now let me go ahead and simply say increment and then function actually increment by and then one function right here and then this will be equal to right now this is where things become a bit tricky so here equals two and then this will be the actual number and then right here we pass a lambda and then I'm gonna say number and then plus plus there we go and which semi column so you can see that this and this are the exact same thing so basically this integer right here corresponds to the actual input so this one right here right this data type this integer corresponds to the return type and then this number corresponds to the parameter to this function right here and then I'm saying number plus plus watch let me simply make them the same so number plus one and let me also rename this function to increment by and then one so you can see that this is pretty much the same but now we are talking in functional style world so now the way that we use this is as follows so go ahead and simply say increment by one and the reason why we not seen this dysfunction is because we can actually put it inside of this method so inside of this main method we can paste that function there or if you want this can also be static like that right so let me actually put this on a new line so you see exactly how it is so right here I can say increment by one function and what I'm gonna do is simply say dot and then apply so the apply pretty much takes the actual argument so let me go edit you say one and then right here I'm gonna say increment and then two and I can change this to the time to integer and then simply say South and then increment two and then let me simply run this again you can see that we have the exact same output so this is how we use a function so basically it's the exact same thing as this function right here but in a functional style way so if you have any questions about this go ahead and drop me a message but I'm going to show you exactly how all of this fits together right because now you might be asking you know why should I use you know this function right here instead of a normal regular function right and don't worry by the end of this course everything will be clarified so there are few methods that I want to teach you so right here if I premature say increment function actually increment by one function don't you can see that we have apply and then this method right here and then so I want to teach you what this method is because you might use it later on so let me simply move that and this is all for now join me in the next video see ya alright let's say that you want to combine multiple functions together to achieve a desired state or a desired output so let's say that you have this function right here so increment by one function but let's also create another function actually let me know copy paste but simply say static and then function and this function will take an integer and this function will also return an integer and then I'm gonna say x and then 10 right here so this will take the number oops number and then this will be lambda just like that and then number x and then 10 just like that so we have this function right here and what we want to do is after incrementing so after incrementing by one right here we want to apply that or the function immediately so you could do something like this so you could say int increment or actually multiply and then equals two and then multiply by 10 and actually rename this to a function just like that and then right here you could say equals 2 multiplied by 10 function don't apply and then increment and then 2 right so this is not quite right because this is a name so right there and then remove that and this should be multiplied by 10 let me grab that name there they there and there we go so now if I do a salt and then multiply by ten function actually I'm getting all confused right here so this should be multiplied there we go so this is the actual output so now if I run this you can see that we take 2 multiplied by 10 is 20 so obviously this is you know quite normal for us to do but we can do something better so what we can do is simply combine the two functions together so right here what I'm going to do is simply say increment by 1 function dot and then and then multiply by 10 function and right here what I'm gonna do is simply extract this to a variable so this will give us yet another function and let me simply say add by 1 and then multiply by 10 there you go so if I put this on a new line and you can see exactly what it's doing so now let's go ahead and simply say add by 1 and multiply by 10 and then simply say apply and you can see that you can keep on you know chaining these functions as you go along but for now let's simply change two functions so now I'm gonna say apply and the number that I want is 1 right so this should give us the exact same result so 1 + 1 - 2 + 10 you guessed it right that'll be 20 so obviously I need to put this around system go out print line just like that run this again you can see that we have 20 right here so if I change this to let's change this number right here so instead of 1 let's go ahead and change it to 4 4 plus 1 5 times 10 50 right here and this is pretty much how you chain functions together so if you have any questions go ahead and drop me a message next let me go ahead and teach you about by functions all right so what a Buy function is is the same as a function but instead of taking one input it takes two inputs and produces one output and by function for two things right so right here we have this function right here so this one add by one and multiply by ten so let's say that the first number that we give it is the one that it should increment and then the second one is the one that it should multiply a by so right here let me go ahead and define this as a normal function so you can see exactly the equivalent so increment and actually increment by one and multiply just like that so now right here we have a second argument so int num2 multiplied by just like that and now I'm gonna wrap everything within parentheses and then say times and then two x now the way that we define the exact same thing but using a bi function is as follow so let's go ahead and simply say bye and then function so the first argument is the first input which is the actual number that we want to add 1 by so let me go ahead and simply say this is an integer the second one is also an integer the third one is also an integer so bear in mind that you can have different data types depending of your circumstances so right here let me simply say that this will have the same name but by function so usually you wouldn't include the PI function for the name but I'm just doing it so that you understand exactly and reason about these things so right here this now will take two things so because it takes two arguments we need to wrap this within parentheses so this will be number two increment by one and then number two ma t fly and then PI and this will be now a lambda and what we do is simply say number two increment by 1 plus and then 1 right so this is the exact same thing that we did here and then this will be within parenthesis and then finally times and a number to multiply by and that would say my column and let me just put this on a new line yet again so you see exactly everything so I've got bigger font because I want you to see exactly properly but you know you shouldn't in then things like this but basically this right here is the exact same thing as this so now let's go ahead and use it so right here remember we had this function so add by 1 and then multiply by 10 so let's go ahead and say this now is a bi function and this was the function example so let me actually add a comment so takes one argument and produces one result so now the buy function is almost the same but it takes two arguments right here so let's go ahead and use our function so right here so increment by one and then multiply let's go ahead and give it four and then let's multiply four by one hundred so basically this will be 4 plus 1 5 5 times 100 500 so let's go ahead and simply I'm not going to even put this into a variable I'm simply going to say South and then just like that so let me put like that and there we go so now if I run this you see that we have 500 right here now let me go ahead and use the buy function so if I escape out of this and let's actually have the same system right here so system to 0.9 and right here let me simply use the actual function I think we have to have here static so this guy right here so this has to be static there we go now it should appear by function and this should be dot and then apply and apply takes 4 and 100 now if I run this you can see that we have the exact same result so this one right here is using normal Java function and this one right here is using the PI function let me go ahead and show you the actual docs you'll see that we have lots of by so let me say let me search for by so you can see that we have you know double by function - int by function - long by function and all of these by functions are the exact same thing as the function but they take two arguments as you can see here right so the same with predicates so it takes two arguments and returns true or false so this is the difference between a function and a by function if you have any questions go ahead and join me message otherwise let's move on alright next let's go ahead and about consumer so a consumer so let me search for consumer right here so consumer so a consumer represents an operation that accepts a single input argument and returns no result so let me show you exactly first the non-functional way and then the functional way so let's say that right here again let's have a public static void main and right here let's go ahead and define a static class and then a customer and then let's go ahead and simply say private final and let me put this full screen so private final and then string and then customer name private final string customer phone number just like that so next let me add that to a constructor and now what I'm gonna do is have a function right here so let me go and simply say static and then right here let's go ahead and say great and then customer so this will take a customer and right here let's go ahead and simply say salt and then hello and then customer and then daunt customer name and then right here plus and then comma thanks for registering and then phone number and then plus customer dot and end customer phone number so let me actually put this on in your line so you can see everything so now let's go ahead and simply create new customer so new customer this will be let's say that Maria Anna Maria phone number will be nine nine nine nine nine right so let me go ahead and cut this and then simply say create customer pass new customer just like that and end with semicolon just like that so now if I run this class you can see that we have a low Maria thanks for registering from number nine nine nine nine so the way that we achieved the same but using the functional interface so the consumer functional interface is by doing the following so let me go ahead and simply say static and then consumer so consumer takes one argument so this will be of type of customer and then right here let me go ahead and seem to say to read customer consumer equals two and then we'll take the customer and what we're gonna do is pretty much the exact same thing right here so let me grab all of this and then paste it there and basically now I can go ahead and simply say great customer consumer dot and then right here instead of apply we simply accept now let me go ahead and extract this customer so Maria and then also past Maria right here so there we go so this is using functional interface so this is actually consumer and then normal Java function right so if I run this you can see that we have the exact same thing right so basically a consumer takes one argument and pretty much returned nothing it's like a void function right so this customer right here is the data type of the input so is this one this customer is the actual name of the input so this customer right here and void you could technically say that void is a consumer so if you have any questions on this go ahead and join me message otherwise let's learn about by consumers all right so let's go ahead and learn about the by consumer so the same way that we've done with functions and by functions we have the same for consumer so pretty much every one of these functional interfaces they have the by version so in front of it they have the by version so the by simply means that instead of taking one argument it takes two arguments now let's go ahead and change this function so let me simply grab that and let's go ahead and beside v2 for example so this will be the version two so what we're gonna have is a by consumer right here and let me import this so first we will take the customer and then we will have the boolean so right here so this boolean so this boolean will represent whether we should hide the customers phone number show and then phone and then number so now we have an error because we have two arguments and because we have two arguments we need to enclose these within parentheses just like that now right here what I'm gonna do is instead of printing the actual phone number like that I'm going to say the following so I'm gonna put this within parentheses just like that and right here I'm going to say show phone number so if show phone number then we can pretty much print that otherwise I'm simply going to have some stars just like that so now let's go ahead and test this out so let's go ahead and simply say great customers v2 and let's pass Maria and then right here let's simply say true for now so this is actually complaining because we need to say dot accept and if I run this you can see that this is the version 2 of our function now let's go ahead and simply say false run that if you see that now we are hiding the number right here so basically if you were to convert this into a normal regular Java function this would be as follow so let me just duplicate all of this just like that call this v2 and then right here we simply take the exact same variable so show phone number but right here boolean show phone number and let me grab everything from here and then put it here there we go and that would set my column and I can simply now go ahead and say get or actually create customer v2 passed Maria and then true run there we go and you can see that we have the exact same information so there we go this is how you use a by consumer if you have any questions go ahead and drop me a message otherwise let's move on you alright let's go ahead and about predicates so I've gone ahead inside of the functional interface package created this and this called predicate class and inside let's go ahead and simply type public static void main and let's go ahead and learn about predicates so let's say that you want to do the following so let's say that you have a function so a normal Java function so static and then this will return a boolean and right here let's simply go ahead and say is phone number valid so let's go ahead and simply take a phone number so string and then phone number and for now let's coherence and keep it very simple but this validation is not real but it's just to illustrate the actual example and purpose of predicates so right here let's simply say that this node this phone number in order to be valid it has to meet the following criteria so let's return phone number dot and then starts with and let's say that the phone number has to start with zero seven and let's also say that the phone number and phone number don't and in length is equal to eleven so this is a simple phone validation method but this is not real it's just to illustrate the purpose now let's go ahead and simply do a South here and let's go ahead and have this phone number so zero seven and then zero zero zero zero zero zero zero zero zero and let's have another one zero and then nine and then we can had you know a couple digits like so right so the validation is not real but this will illustrate the purpose so now if I run this so let me run this main method you can see that I'm printing the number but what I meant to do was to actually let me cut this so what I mean to print was is phone number valid and then passed the actual phone number so same with that one cut it is phone on a valid and then paste it running again you see that the first phone number is valid so it meets the actual criterias let's also have another one so the same but this will be 10 characters long and if I run this again you can see that the first one is valid the second one the second one the second one isn't and the third one isn't as well so right here we're simply doing a simple phone validation check now in order for us to use the functional style we can use the predicate so predicate right here you see represents a boolean valid function of one argument right so right here the way that we use a predicate is as follows so this exact same function as predicate will be something like this static and then right here predicate and then this will take a string so this will be the phone number and then let's copy this exact same name and then let's simply say predicate and right here this will be equal to and then phone number so let's actually grab this and then this lambda and on a new line I'm simply going to grab this and then paste it and then end with semicolon now let's go ahead and use it so to use it is phone number valid predicate dot and then test so this is the actual method that we need to use so now we're going to test the following let's test the first number so we know that the first number is valid so basically this will be the exact same thing so we need to simply salt this just like that there we go and let me duplicate this and basically we should have the exact same output there we go and then place that in let me do South without predicate and then right here weight predicate and if I run this you can see that we have the exact same result so true false false and without predicate true false false so basically this is the equivalent so the predicate is the equivalent as this normal Java function and the cool thing about predicate is that you can have a bunch of predicates and then combine them together so the same way that we've done with function using and then we can you know we can also use it with predicates so let's say that we have another predicate so let's have another project so static and then predict it and this predicate will take a string and then let's say contains and then number and then let's simply say number three so if the phone number contains number three so phone number and right here I'm simply gonna say phone number don't and then contains and then three just like that now the way that you change predicates is as follow so right here let's simply go ahead and say is phone number valid so we're gonna pass this phone number so this one right here there we go and then we can say dot and then is phone number valid predicate dot and then and another predicate so the other predicate is contains number three dot and then test let's actually pass this number right here so the last one there we go and let's wrap everything without a system topper print line let me put this on a new line and just like this there we go so if I say right here is phone number valid and contains number three equals and then plus the actual result let's do the same for a different number so let's actually let's actually do for this one very first one so right here and let's actually put a three right here so this should be false because the phone number is invalid but it contains number three right here but this one the phone number is valid so it starts with zero seven eleven characters long and contains number three at the end so let's go ahead and run this method and there we go so you see that the second one is true as I explained why and the first one is false now you can use ant but you can also use or so or means either one of them is true so if I was to say right here so instead of and I can use or right and if I remove three here and put a zero run this this will still be true it's true because we only need one condition to be true if you use an and so if you chain multiple predicates together using an and all of them have to be true so the same way that we have predicates we have by predicates by and then predicates right here and basically they are the same thing but instead of taking one argument they take two arguments so I'm not gonna give you a full example on by predicates because you should be familiar now with by predators by functions by consumers on so forth so if you have any questions on predicates go ahead and drop me a message otherwise let's go ahead and learn about the final functional interface that I want to show you and then we can start putting all the puzzle together so that all of this starts to make sense and how you actually end up using it join me in the next one see ya all right go ahead and inside of the functional interface package create class called underscore supplier with a public static void main method so let's go ahead and learn about what a supplier is so if I go to the docks right here you can see that they say supplier T represents a supplier of results so basically sometimes you might have a method right here that simply let's say that we have a method like that so like a method like this so static and then get largely string get connection URL why should i simply say get DB connection URL right so this will take no arguments and let's simply return and then for example JDBC Coulomb force transport / localhost and then five four three two four slash let's say users right so this is not real but you get the idea so the way that we can use this function now is if I simply say South and then get DB connection URL and then if I run this you see that we get the actual connection your route for some kind of database so the purpose of this function right here is simply to return a value right here so this is the value ty it returns so we can do so we can use a supplier to represent the exact same thing so I'm gonna copy this URL and to have this job of function as functional we simply have to say supplier wisely static and then supplier and then this will supply a strain and then get let's grab this and then supplier and right here equals two and then what we need to do is simply have parenthesis and then we simply pass or actually return this just like that so if I put this on in your line so you can see exactly what I'm doing and you can see that now this is the exact same thing but if I go ahead and simply say system dot out dot print line source out and then right here to use this I can say get DB connection URL supplier dot and then get so now if I run this I do get the exact same result so this is pretty much how you use a supplier so a supplier simply returns any kind of value that you want right so this data type doesn't have to be a string it could be you know your customer classes your custom objects you know an array pretty much anything that you want so if for example if you wanted to return a list right here you could simply say list and then off type and then string right here and then here I would say list dot off and then just like that and I would say I call them right so now if I run this you see that this now is a list so if I was to return something else so right here let me simply add comma new line and then this like so so it's much neater just like that oops just like that and right here let's simply say customer for example right so get connection URLs and then supplier and then here URLs run this and there you go so now we are returning a list with two URLs so if you have any questions on suppliers go ahead and drop me a message otherwise let's move on you all right so by now you should see exactly the differences between writing a normal Java function and functional interfaces so the benefit of you know knowing the functional interfaces is when you start to use optionals and streams but more specific streams right so once you start using streams with all of these functional interfaces you will start to write clean code and that's what I teach you next okey-dokey now that you've learned about consumers functions by functions predicates by predicates and suppliers and also I think by consumers now let's go ahead and actually learn how this is useful to us when writing code so go ahead and create a new package under Java so package and call this as streams now go ahead and create a new class and simply call it underscore and then string just like that enter public static void main enter now remember inside of the imperative package go ahead and open that up and let's actually steal some code so right here let's steal all of this so the enum gender as well as the person class so let me close this and then right here let me paste that in and I'm a council because I don't want to import from imperative domain so cancel and let me go back again and let's actually steal some code again so let's take this list right here so we're going to take this list and then we can close this once more and then right here inside let's go ahead and paste it and you see right here is trying to import from imperative main Council out of it and I simply import the actual list ourselves soft just press option enter and right here let's import male and gender just like that and finally let's call it another a static import for those so I'm just pressing option and then enter add static import now let me go ahead and close this and let's actually use what we've learned in the previous section with streams so this is pretty much where everything starts to makes sense so let's go ahead and use a couple of those functional interfaces that we've learned so if I collapse this partially open this so consumer function predicates and suppliers so go ahead and simply say people dot and then stream right so stream pretty much takes us to an abstraction mode and if you want to learn about all of this of course this course right here where I teach everything about the Java streams API because once you've because once you learn this the way that you write your Java code will change quite significantly so stuff that you used to take twenty lines of code you can simply collapse it into perhaps five lines of code and to be honest knowing how to use the Java streams API will make you a much better developer so I also have a free video on YouTube where I teach about filters or group by all match basically this is a an introduction of this awesome API but for now let's go ahead and learn some of these methods so go ahead and say people dot stream and now go ahead and simply say dot so what I wanted to do is to actually look carefully so right here so look at the methods that we have we have map collect filter find first four reach all match but what I wanted to see is exactly look that map function takes a function right takes a function of some type that extends person and then you can see the other one so the other arguments or question mark extends are matter so this is exactly what we've learned right so a function takes an input and produces an output so the same would collect so collect is a supplier and you can see a u by consumer so this is awesome right this is everything that we've learned the same with filter takes a predicate find first so this sometimes simply an optional for rich takes a consumer right let's look another one so for example drop Wow this takes a predicate find any flat map so this is a very useful function takes a function inside and right here flat map to int map to double so this takes a true double function right and you can see right here non match a therefore you have a very useful function as well reduce binary operator and then right here you have another reduce and then look at this sort it takes a comparator it's not relevant to us take wow you see that this takes a predicate and to array int function basically this is pretty much everything that we have learned right so functions by function consumers predicates and the other good stuff so basically this is now where everything comes into action so let me go ahead and show you for example how to use this map function so go ahead and simply select the map function in the mouth and the mouth and and the map function simply does transformations so right here this takes a person right and from here what we want to return so I want to return person dot and then the actual gender now let's go ahead and simply say dot and then collect and then collect us to set right so and also let me extract this to a variable genders and then South and then the genders or actually no even south so let me let me simply remove that because I want you to see exactly what we're doing so right here so instead of collect let's go ahead and simply say dot and then look at this for each takes a consumer and we've learned about consumers right a consumer simply takes an argument and produces nothing so void right so right here we will take a gender and what we want to do with this gender so we want to simply do a system dot out dot print line and then in gender and now we can use method reference with this like so and beautiful code now let me go ahead and run this method so you can see what we're doing tada there we go male and female right so you can see that we mapped everything so right here we stream the people list so oops so right here we've dreamed the people list right contains a bunch of people right here so John Maria Ayesha Alex and Alice so we map so we perform a transformation on that list so we convert every single one of them into a gender so now we simply have a list full of genders and then we collect to a set removing duplicates and then we simply print that's why you see that we have only male and female right here so if I was to have another gender right here cannot actually prefer not to say basically this is not gender I'm just giving you an example so right here let's add another one another person right here there we go and then prefer not to say this will be Bob and you could tell that this is a gender but so if I run this you see that now we have preferred not to say as well right here so if I was to now map this into the actual name right so let's say name and then run this we simply now get the actual names instead of genders so this is really really really awesome so let me actually let me also show you that we can simply get the length of each name so let's also perform another map operation right or in or in this scenario let's say map to int this takes the actual name so this time is name and then simply say name the dot and then length and right here we're not going to collect to set so let me delete that and then we can even change this to method reference just like that if I run this you can see now the length of each name so this is the power of streams so you might be asking you know what is this right or what is this all what is this so let me just show you right now so what I'm gonna do here is simply extract this to a variable and also this to a variable and also this to a variable and now check this out so this is what you've been learning in the past videos so look at this the function that takes a person returns a string and then the same here true int function we haven't learned to int function but basically this is the same thing so this takes a string and returns an integer that's all it does and you can see right here in the consumer so an int consumer simply takes an integer and it prints out the results right here so let me actually replace this with a lambda so you can see exactly what is this you see that this is what we've been learning throughout this course but when you use it with streams you don't have to have all of this syntax basically you can ditch the left-hand side and just keep these right so if I now in line everything so command shift end command shift and there come on shift and there and look at this right and here even method reference and there we go so if you want to learn more about streams go ahead and check my course on Java streams API where you learn everything about writing code in a declarative way this is all for now join me in the next one all right let me show you some other methods right here so still right here so let's go ahead and for now I'm gonna pretty much just comment everything right here so I'm gonna comment this and right here let's go we're dancin to say people dot n n stream and then right here let's go ahead and simply say all and then match basically we want to know that in our list we only have females so right here so this takes a predicate and remember the product that looks like this so this will take a person and then in this list we want to make sure that we only have females so female dot and then equals person dot and then watch it not people person dot and then gender there we go and now and now in this with semi column and if I try this to a variable you can see that this is a boolean now let me go ahead and simply say contains only females there we go and if I do a South on this South and then contains only females and let me actually go ahead and this structure all of this so I'm gonna extract this to a variable and check this out so right here so we've learned about predicates right so this is what we've learned previously so now let me go ahead and run this again and you can see that it's false right because in our list so right here in our list we contain both male and females so if you want for example to say ohm is say the opposite you could say for example fig match let's look at for let's look at match so any match right here right so any match so any match means any much taking our predicate so if we have at least one female then this will return true run this tear it up true right so if I for example remove all the females here just like that run it again and it's false now right so let me undo all of that and finally we have I think it's non match right so let's say that now we change this to prefer not say and let's move Bob from here so basically I want to make sure that in our list we do not have someone we'd prefer and not say gender if I run this this is true if I had Bob back this will be false there we go so you see the power of dreams right so obviously we have other methods really really useful methods so if I do a person actually people dot an extreme dot and look at those methods right so mean peek reduce sorted take Wow so right here drop Wow non match you've seen find first map and map to int so basically once you learn all of this the way you write java code will change and you will become a much better java developer so this is all for this video next let me also show you that you can use you know all of the goodness of java functional interfaces with optionals because you know again optionals changes the way that you deal with null pointer exceptions in java join me in the next one see ya alright let me go ahead and show you how to use Java optionals with functional interfaces so if you've never heard about Java optionals or if you've never used Java optionals go ahead and check my youtube channel where I've got this crash course on Java optionals and I highly suggest you to learn how to use optionals because they will help you a lot in terms of dealing with null pointer exceptions so let me go back to IntelliJ and let me create a new package so right here optionals and inside let's go ahead and grab main class and right here public static void main and there we go so now let's go ahead and simply say optional don't and then of nullable so right here let's simply say no right and if I now press dot check this out you can see that right here you have a bunch of methods but if you look closely again so these methods they do take for example the first one or else to throw takes a supplier right so this supplies an exception you see or else or so right here this takes a supplier mat again function if present consumer filter and flat map so right here if present or else right is present or else get so this is a supplier so and actually use this one or else and get and then remember suppliers so right here oops there we go so let me simply say default value just like that extract is to a variable and let me simply name this as value now if I go ahead and simply say south on value there we go if I run this you see that we get default value but now check this out if I simply add a value here so hello run this again you can see that we get a low right so let's try something else so instead of or else get let's go ahead and simply say dot or so right here so or else throw so this again we have to supply an exception oops just like that new illegal set exception and you can have for example a message right there exception for example right so you can see that if I shrunk this to a variable this is a supplier and we've learned about suppliers remember so right here so this is a supplier right it takes no arguments but it supplies a value so there we go now let me again show you something else so if I delete this so instead of saying or else throw let's go ahead and use for example so let's use something that has a consumer so if present there we go if present will receive the actual value here and then we do something with the value so right here let me simply remove that and let me move this above and then this there we go now because I only have one single value but basically you could do like some logic right here right if you had like you know something that you wanted to do like a business logic right so now because I only have one value and delete all of this I can delete that and I can even go one step further use London method reference just like that and look at this looks beautiful so now let me go ahead and simply say John at gmail.com and in fact let's go ahead and pretty much have a custom message so right here so email and what we're gonna do is simply say South and then sending email to and then email there we go now if I run this you can see that right here we do get sending email to John but now as soon as I pass I know here right if I run this we get nothing but now I can go ahead and pretty much change this weed so there is another method right here so if present or else takes a consumer and then the other empty action is a runnable so that is our consumer so let me actually grab this so you can see what am i doing so this is the first one so this email and then right here we have to pass comma right comma and then a runnable so a runnable goes like that and there we go so let me pretty much just put this on a new line and this on a new line just like that and then if I close that in then things there we go and then here what I'm gonna do is simply say South cannot send email there you go now if I run this you see that we have cannot send email as a default value so again I can remove that and have method reference and look at how this now starts to flow guys so this is the power of functional programming streams optionals and you know once you start writing code like this you can achieve a lot more instead of using imperative approach so as I said go ahead and roll to this course right here where I teach everything you need to know about Java streams API also if we learn about optionals go ahead and check this video where I show you how to use optionals and how to be safe with no values this is all for this section if you have any questions go ahead and drop me a message otherwise let's move on let's go ahead and learn about this awesome design pattern called Combinator so let me go ahead and first create a new package and right here I'm going to simply named this as Combinator and then pattern so right here what I want to do is to create a class so on a great class and let's say that we will start with the user so the user actually a customer so we have a customer and this customer has few properties so let me collapse this so private final string name let's also give it an email phone number and let's also give it a local date for date of birth now let me go ahead and pretty much just add those to a constructor and let me generate some Geddes right here so getters just like that so now let's say that you have a system right where customers do register to your system and you and you need to perform some kind of validation right so what you would do for example would be something like this so inside of your package you would create a class and then customer validator and then service right and in this class right here you would have few methods to validate few properties of a customer right so you could have a method so right here you could say public and then boolean is email valid right and this takes a customer or better takes a string email right and right here you'd simply say return and then email dot contains you know this is a rubbish validation but this is just for illustration purposes right so contains that or actually contains the add symbol for now let's keep it simple then you might want to have order validation for the actual phone number so right here so is and then phone number of valid so this would be phone number and then right here phone number right here contains so instead of contains let's go ahead and simply have a simple validation so starts with and then plus and then zero right and then what did we have so if I go back so we have a date of birth so let's go ahead and simply say that the date of birth so right here is adult so is adult so right here we take a local date and then D OB and then what we're gonna do here is simply say period dot and then between so we're going to calculate the number of years between two dates so the start date will be inclusive so this will be the DOP so customers and their of birth and then local date don't and now and then right here dot and then get years and let's say that for someone to be an adult they have to be at over sixteen just like that right so if I put the screen so you see so this is then your customer validator service right so now the way that you would use it is let's go ahead and create a new class so main and then public static void main like that customer and then customer equals to new and then customer and then right here let's go ahead and simply say that this is Alice and then she has an email of Alice at gmail.com and then the actual phone number is plus and then zero so oops plus zero and then any random number and finally let's go ahead and simply say that she was born so local date of and then 2,000 just like that and then one and then one and there we go so if I collapse this and you can see that now we have a customer that we can perform some validation now obviously you know this validator service right so this would even be private so private and then private and then right here so let's go ahead and combine the both let's go ahead and combine everything so is actually let's go ahead and simply say public so this is the actual interface that we provide to clients so public and then boolean and then is valid just like that and this now we'll take a customer and now what we're gonna do is simply say return and then is email valid customer don't get email and then and and then is phone number valid customer dot get phone number and finally and is an adult customer don't and then get and then DOB just like that so this is actually you know normal code but so what I want to teach really is how are you going to be able to use a functions to change this in a much better way and ain't need to way right so here if you have extra validation you have to have another method like that and then simply append here and then basically right here you don't have visibility you know whether the phone number was invalid or it was an adult or or anything right because this simply returns a boolean right is valid so if I go ahead and show you what I mean so right here let's go ahead and simply say customer validate the service service equals to Y should let me rename this to validation validate the service new and then custom validator service now let's go engine to say validate a service dot is valid and in past customer there we go so what I'm going to do actually is inline this oops not that so basically I wanna inline this guy right here so like that and then I'm going to do ass out just like that and then end that like that so now if I go ahead and run this there we go so you can see that this customer is valid and from now on let's say that we can store customer store customer in and then DB right so now actually them say if and then valid right so now if I go ahead and pretty much just for example remove the add sign when this you can see that now it's false so it's not valid and also let me go ahead and remove the plus sign right here there we go still not valid but now you don't know exactly you know what is wrong with our validation so the Combinator pattern it's an awesome design pattern that allows you to change phones together so basically a Combinator is a function that might take or the function as humans and returns new functions so this is an awesome pattern that I'm pretty sure that you will love and that's what I'm going to teach you next alright let's go ahead and use the Combinator pattern to improve this validation service right here that we have so validator service actually so this one is a she said so so this class right here is okay but we can do way better than this right because right here for example if you want to have you know a different method that validates for example just email and the phone number then you would duplicate this so right here and then you would say it's valid phone number and email and basically just remove for example is a load from here and we don't want to do this right so we want to have a very beautiful interface that we can simply chain all of these validations together so let's go ahead first and expand the project tab and right here let's go ahead and simply create a class and this will be called customer and then let's actually named this as registration valid data there we go and instead of a class let's change it to an interface now what I'm gonna do here is the following so right here I'm going to say that this will extend a function so remember we've used functions right here and the first argument that we want for this interface is a customer so the type of a is a customer and then right here let's go ahead and simply say validation and then result so let me end up just like that and they put this on a new line so you can see exactly what we do it now this validation result this will be an enum so enum and then validation or let me actually just copy and paste this so validation result just like that and now I can import this actually I did the right thing and let me actually do a started import right here there we go now here what I'm gonna do is have you know few results that I want my validated service or actually my my customer registration validator to have so one I want this to be success right so this is where all the validation is successful then we have few errors so right here we have phone number not and then valid we also have email not and then valid and we also have is not and adult just like that so the way that we're going to use this with this interface is as follows so this is where Combinator pattern kicks off so right here we're simply gonna have the validations that we want right so here you can have as many validations as you want and then you chain them together at the end so right here let's go ahead and simply say static and then right here what we're gonna return is this guy right here so the same interface so right here we will return this function right here right that takes a customer and returns a validation result so right here I'm gonna say is and then email valid and then I'm not taking anything there but right here what I want to say is return and then customer and right here I'm gonna say customer so basically the exact same validation that we had before so customer dot and then contains and then add so if this is the case what I'm going to do is simply return you guessed it success otherwise phone number or actually it had not phone number email is not valid and let me have a solid import for this and the same force yes there we go now let's go ahead and do the same for phone number right here you can actually steal some code so from here what did we have we had this copy that go back and then is phone number valid and then I'm going to change that with this and then customer dot get phone number starts with that so if that is the case then it's valid otherwise so right here phone number is not valid let's go ahead and do the same for is adult so right here static and then customer registration validator is an adult there we go let me steal some code from here so this is what I want just like that go back and then customer wash lis return customer and then I'm gonna paste that and then right here customer dot and then get DOB so if this is the case right so if the customer age is bigger than 16 what I'm gonna do so let me put this on a new line so if it's bigger than 16 what I'm gonna do is simply return success otherwise is not an adult there we go in then this properly and so one final thing that now we have to do is right here we have all of these functions of these three functions and the Combinator pattern allows us to chain all of these functions together so the same way that you saw with a function where we can use and then and the same with predict is by using I think it was and right or or we can have the same logic with the comm we need to patent so right here what I'm gonna do here is the following so I'm gonna have a default method and right here I'm going to return a customer valid data so the same thing that we did here and then I'm gonna say and right so right here we're going to take a customer valid data and I'm gonna call order so basically this is where the magic happens right so I'm going to say order and inside I'm going to return and then customer and then inside what I'm gonna do is simply say this don't and then apply so we are going to apply this customer and I'm going to run these two available so I'm gonna say result and now check this out I'm going to say return and then result dot equals and then success so if it is equal to success I'm going to apply the order validator dot and then apply and then customer otherwise I'm going to simply pass the actual result not super result there we go so if we get to this point we know that we have an error so now this is complaining because I need to pass semicolon right here so let's go ahead now and use this awesome interface so let's go back to the main method right here and now let me simply comment this out and let me simply add an ADD sign right here actually let me simply leave it as it is and then I'm gonna show you exactly all the errors so now let's go ahead and simply say customer actually not here so using Combinator pattern so right here let's go ahead and use a customer registration validator and then dot and check this out so we have all of these methods right is an adult is email valid and is phone number valid so let me go ahead and simply start with is an email valid right but now what I want to do is the following so right here I'm going to say dot and then check this out we have this method so and right here so this is the one that we defined so I'm going to say and and then let's use the rest so if I do if I press control-shift you can see that we have the other methods so is phone number of valid dot and then and and then is an adult there we go so if I now add a started import you can see that now this becomes much much cleaner so now we have these three methods that we have chained together the way that we use it is simply by saying dot and then apply and then pass customer there we go now if extract is so variable so resolved and let's go ahead and simply say South and then result so now what I'm gonna do here is say if and then result not equals to success so success we're going to throw so let's throw so throw new in Eagles that exception you could have your own exception but for now let's go ahead and throw on a legal state exception and then the actual result dot and then name there we go and basically this will actually print out the same thing right here but I want you to see the actual result so let's go ahead and scroll up and let's start with a valid customer so at gmail and then the phone number has to start with plus and then zero so if I go ahead and run this there we go we have success right here and if I scroll up let's now check all the validation that we have so let me remove the app sign and then run there go you see that email is not valid and we've thrown an exception right so email or valid so if I add that back and if I remove it plus here run this there we go phone number not valid and if I had a plus there and let's make sure that this guy was born you know 2015 or this guy Alice was born 2015 so which means that you know she is not an adult so if I run this you see that is not an adult right and you can even customize your exception that we throw by including the name or you know an ID or anything like that but basically now you can see the power of the Combinator pattern so if you have more stuff that you need to change to this validation so let's say that the requirements change you can simply duplicate this and then have your custom implementation inside of the interface that we've defined so this one right here so customer registration validator and then just chain all the validation logic and scenarios that you have and this is the power of the Combinator pattern so one last thing that I want to show you is that so if I command Z couple of times there so basically so right here you see that we call don't apply so if I comment this and then right here let me simply remove the apply there so the cool thing about you know these functions so let me change the variable right here to a validator so the cool thing about this right is that this is lazy right none of this stuff is run until we invoke the dot apply so if I say result right here you can see that we have all of these methods right we can compose applies on and so forth but if I run this you can see that nothing gets printed out and even if I for example right here so where we return here so the email valid so right here so is email valid some let me grab that and then let me simply South and then running email validation and then right here simply return that and end up with semi column there so even if I run this you can see that we still get nothing but as soon as I referred all of this just like that and then call apply now check this out if I run you can see that we have the running email validation and I think I need to undo all of this and then run it again and there we go so running email validation right and then we have is now an adult and the actual exception so I hope that you see the power of the Combinator pattern I use it whenever I can to perform validations like this and you should also do it if you have any questions go ahead and drop me a message otherwise let's move on you alright so in this final section let me go ahead and explain you few things that will help you to fully understand the functional interfaces and functional programming with Java so so go ahead and go in your package and in this package we're going to call it as vinyl and then section right here so inside create a main method and then so let me put this full screen like this and then PS and then the m4 public static void main and right here what I'm gonna do is I'm gonna write some JavaScript and there we go so wanna touch on this video is callbacks so maybe you've done some JavaScript and JavaScript they have this notion of callbacks so a function in JavaScript looks like this so this won't work in Java but it looks like this so you have function and then you give it a name so then hello and then right here you might have for example a variable so let's say and then first name last name and then callback something like that right so right here what we're gonna do is let's say for example the last name is not provided then we simply have like console dot log so let's simply say console the log and then right here first and then name and then I can say if if we have last name then we're going to console dot log and then last name otherwise we will have this callback so we're going to invoke the callback right so you might have seen code like this in JavaScript basically this is an extra function so if I take this command C and then go to Chrome and then inspect and then right here go to console paste my function there and then enter you can see that we have undefined now if I say hello and then say first name John and then pass now there and then right here I can pass a function so this is a callback right so console dot and then log and then no last name provided and then end this with parentheses here if I now run this oops we have a error there and they shouldn't have they call them there so enter and you can see that now we have John and then no last name provided so basically you can see that this callback right here is a function and you can have like you know a lot of logic in it right but you know this is very simple so we're just logging but if you want to have for example an extra logic that goes through the database and does you know some other stuff you could definitely do it I'm just showing you the callbacks in JavaScript so right here if I now pass so right here if I know say Alex and then Montana and then run this you can see that now we not printing the actual callback right so now we have John and then Montana the last name so we can achieve the same in Java using functional programming and the functional interfaces so the way we're going to do it is as follow so let me simply comment this right here so you have it for reference so now what I'm gonna do is actually have a static and then void and right here what I'm gonna do is actually combine both worlds so we can have a regular method so a normal Java method and also inside as a parameter we can have a function right so this is you know programming because now we are free to do whatever we want so right here you see static void and let's go ahead and simply say hello and then right here string and then first and then name string last in the name and then right here what do you think we need right so remember we've learned about functions by function consumers suppliers predicates so we kind of just want to have a function that might take for example last name if it's there printed otherwise half for example a default message so right here what we're gonna do is simply say consumer and then off type and then string and then let's simply name this house call back just like that there we go and then right here let me simply say South and then the first name and then I want to say if and then last name equals to no right we can even say is blank right but equals to know so if it's equal to no or actually lets me read this so it's the same as the function that we have right here as JavaScript so if it's not equal to now let's go and let me say South and then a last name otherwise else we're going to say callback dot and then except and then the string that we want is first name for example right so let's take this first name right here and then do something with it so now the way we use it is as follow so let's go ahead and say hello and then John right here and then Montana and right here let's have our callback so let's go ahead and simply pass no for now right so if I run this there we go you see that we have John Montana but now let's go ahead and define this go back so right here we'll have a callback that takes a value and the value will be the actual first name it doesn't matter and then we will do something with it so right here South and then what did we have so right here we said last name not provided so let's do the same here and then four and then plus and then John and then value right so you could even you could even change this to first name if you want but I'm just gonna leave it as value now if I run this you see that we have John Montana and now watch this if I pass no right here and then run there we go no last name provided for John so basically I just want to show you that you can combine you know a normal function with these functional interfaces and do stuff like this and achieve a similar concept as JavaScript that they call holdbacks so there are other languages that have the similar concept but JavaScript you see a lot of these kopacz being thrown and you can pretty much do the same with functions another way that you might see callback is so right here so because right here you might not want to pass a default value right so you might have something like this so right here let me just duplicate this and then call it two and then right here so I'm gonna simply remove this and then runnable and this will be the callback and then right here call back at the dot and then run all right so this is a bit different now so right here the way we'd use it simply say hello like here and then - and then right here you simply pass this runnable right here and you have value there basically this now is the exact same thing as this concept right here right because we're not taking any value and because I only have one line there I can simply do this and then check this out just like that and beautiful code so now if I run this you see that it's exact same thing but right here we're not taking any arguments so there we go so this is how you combine both worlds where you can mix and match these functional interfaces with normal functions and have callbacks if you have any questions go ahead and drop me a message otherwise let's move on you let me go ahead and teach you about lambdas so we've been using lambdas throughout this course we'd you know all these functions but basically let me show you exactly how they're used and for example if you only have one return statement if you have two arguments what you should do when using lambdas so let's go ahead and expand this so come and then one right here for this main method let me go ahead and simply named this as callbacks just like that now let's go ahead and go in your class and then simply call your hands alumnus there we go oops no lambis but lambda and then this just like that now let me collapse that and then public static void main now inside let's go ahead and have a function there we go that takes a string and returns a string there we go so let's simply say print and then name equals two so this guy will take the name and then right here so this part right here it's a lambda right so this is a lambda where this is the actual argument right so this name right here is what comes from this data type so right here then you have to pretty much just do the following so if you have only one line so if you simply return immediately you can simply do this so salt and then actually no even it's not my bad so you can simply and this is not a good example so not print name but let's simply say upper and in case name for example something like this right so now right here if you simply return immediately you can simply do this so name and then don't and then upper case right so you can immediate they do this so this is only when you have one return statement right after this error right here so because you only have one with a return statement and you can even see that IntelliJ is telling you that you can use a method preference so this is because right here this is the same as doing this right it's the exact same thing so this is more understandable but you know once you get too comfortable with this you'll start to use code like this now another thing is right here so if you have to do some extra logic and not return immediately you need to have these curly braces right here so now you can have a logic right so for example say if and the name dot actually is blank right here so if it's blank then you can throw a new illegal in an argument exception something like this right and then right here otherwise now because I need to return I need to say return and then name to uppercase but also I need to add semicolon right here right so you can see that now this is the actual method body for this lambda right so this lambda so this is the entire thing right here so another thing that you could do with them this is you see right here where you have name so if this is a by function so by function just like that and then right here they simply have let's have an integer right integer and this will be H right so now you see that I need to put H right here because this data type is for this one and this data type is for the second one right and string is what this bio function returns now because I have two arguments I need to do this and it will close them within these parentheses so let me actually go back for a second so right here so where we have a function so you see this is a function I can even you know and close them with parentheses but because I only have one argument right into this function right here I can simply ditch this right but if you have a by function and right here let's simply say integer again and then age you know you need to enclose like that right and you could you know do something like another logic right here so South for example and then H something like that right and you know the way you use it is as follows so upper case name dot apply and then Alex s have Alex here and then Alex C is 20 right there we go and this returns a string right here upper chased name and then South and then upper case name I can even inline this like so if I run this there we go so 20 and then Alex so basically this is this logic doesn't really make sense but I just want to show you exactly different ways that you can use these lambdas right here so if you have any questions on lambdas go ahead and drop me a message otherwise let's move on all right so just to let you know that I think I didn't mention this at the beginning of this course but when you use these functional interfaces you see right here where you define the actual inputs and outputs of functions right so string and an integer and then string so these right here they have to be the object type and not the primitive so for example integer right so you could have an integer like this so integer and then number equals to no right and then you can he'll also have integer and then I say counter and right here I can't say this so this is invalid right because this is the primitive version of this object type right here right so right here you either have to say a number so it could be negative number or positive number so if I do a zero right here you'd see that this goes away so for that reason you are not allowed to have primitives in here right because if you have to pass nulls then then you can sew with primitives you cannot so right here you see that we have this integer right here so I can go ahead and simply say no right here and you see that works but if I was to have this integer so the primitive type that does not work and also one thing that I've been doing a lot wrong this course is having static right here so static so the reason why I've been doing static is because I'm outside so right here I'm outside of the main function and the main function is static right and you can only use static methods inside of this main function so right here you see that this still works but if I remove the static keyword you see that it does not work right so this is something that to bear in mind your functions and by functions they don't have to be static right in fact having static throughout your code base is not practical so ideally what you would do is simply have for example so let's say that you have a quiet let me actually put this inside so just like that so let's say that you have a class and then call this as service for example right what you should really be doing is like that having you know Publix and then your function for example or predicate or consumers right so consumer of string and then blah [Music] equals two and then s right just like that and basically this is your public interface you can also change this to private or no access modifier at all so this is package protected right so just to bear in mind that you know they don't have to be static and you should avoid static but the reason why I've been using static is because I've been doing everything inside of this public static void main method so there we go if you have any questions on primitives and the use of solid keywords go ahead and drop my message otherwise catch me on the next one all right let me go ahead and quickly show you the rules that come with functional programming and everything that we've learned throughout this course so the first thing I should know is that there should be no state around your functions right so the functions they have to be pure functions so they shouldn't depend on any other state so for example if you have a global state like okay or a global variable your function should not depend on that global state right so that's the difference between pure and impure functions so pure functions don't depend on global state impure functions do depend on global state and that comes with the side effects right so your functions should have no side effects other than everything inside of the body of your function right it shouldn't have a side effect outside of the function and also higher-order functions so a function is considered to be higher-order function if one of these two conditions are met the function takes one or more functions as parameters so we saw how to so we saw how to pass functions inside a normal functions right or if the function returns another function as results so with the Combinator pattern that's exactly what we've done so I didn't actually show you so right here so with callbacks so right here so right here so you see that right here we are passing a function right here so this is a function right and also within our Combinator pattern so remember so customer and then registration validator check this out so right here we are returning a function so this interface extends customer who actually extends function that takes a customer and sends a validation result but if you check the return type of all of these methods right here so if I do a command shift and then - you can see that they all return the function right here so this means that we are satisfying with this property higher-order functions so make sure that for example let me actually show you you know the know state and pure functions inside of things so if I go back to let's say lambdas right here so you see this function right here so into PI function right here so I should never depend on for example counter right so let's say that I do for example South and then the counter or if I change the state of counter right I should never do this because it will break all of this right so you see that there might be some side effects on the outside of this function right here right so outside of this function there might be the some side effects also you see that you are now mutating state and it's no longer a pure function it's an impure function because it depends on global state so you never want to do this or for example increment the number so number and then you know increment you know plus plus something like that you should never do it right so your functions right here they should be self-contained right so they should only depend on the state that you define inside so right here I would say in int and an encounter equals to zero so this is absolutely fine right because this counter so this counter right here it's only visible inside of this function and not the outside were so if you have any questions on these properties go ahead and drop me a message and I can give you more examples but this is pretty much everything you need to know about functional interfaces and functional programming with Java if you have any questions go ahead and drop me a message next let's go ahead and wrap up this course okey-dokey amigo I want to congratulate you for completing this course and practicing throughout so as you saw for you to write functional programming you just have to shift the way that you think you no longer think about regular Java functions but you start to use functions by functions predicates consumers suppliers so on and so forth now the next step for you is to enroll to my course on Java streams API because once you combine everything that you've learned in this course with Java streams you will start to write code that really really shines instead of you writing code that takes for example 20 lines of code you simply take five lines of code with streams and functional interfaces so this is all for now go ahead and subscribe to my channel to get more videos like this and it really means a lot to me to see a lot of you you know sending me messages suggesting for new courses and I'm trying my best to give you the awesome content that will allow you to become a software engineer also if you haven't joined the private Facebook group go ahead and join because that way you can see what's up you can connect with all the students and if you have any questions just drop them and I'm pretty sure one of us will be able to help you this is off now and I'll catch you on the next one see ya [Music]
Info
Channel: Amigoscode
Views: 246,354
Rating: undefined out of 5
Keywords: java functional programming best practices, java functional programming in hindi, java functional programming tutorial, java functional programming example, java functional programming exercises, java functional programming library, java functional programming interview questions, java 8 functional programming tutorial, functional programming patterns with java 8, functional programming with java, java 8 functional programming
Id: VRpHdSFWGPs
Channel Id: undefined
Length: 142min 15sec (8535 seconds)
Published: Tue Feb 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.