Java lambda ฮป

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how's it going everybody it's you bro here hope you're doing well and in this video I'm going to teach you guys about Elan de expressions in Java and at the end of this video we're going to work on a project where we can use a lambda expression to have a button perform some sort of action like display a message and we only need to write a single line of code cool right let's get into it if you find this video helpful please remember to Like comment and subscribe your support will help keep this channel running hey everybody we're gonna be discussing lambda expressions and when I say lambda I think of lambo short for Lamborghini for some reason but anyways a lambda expression is a feature for Java 8 and above it's also known as an anonymous method and basically it's a shorter way to write anonymous classes with only one method so in order for us to use a of lambda expression they need to use a functional interface or a predefined functional interface which are basically interfaces that contain only one abstract method and a few examples would be action listeners which we have a little bit of experience with runnable we haven't discussed this yet or our own user-defined functional interface so a lambda expression can be used in any place where a functional interface would normally be required and it has its own syntax we pass in some arguments we use the lambda operator and then we can perform some statements or some block of code so we're going to begin by creating our own user-defined functional interface that won't really do much and then for our second example we're going to apply the same logic to an action listener and then you'll really understand to see why Lomb do expressions are fairly useful so let's begin by creating our own user-defined functional interface so within your project folder we're going to go to file new and then interface not class interface and then let's call this my interface and click finish so now what we have is an interface called my interface but we're going to turn this into a functional interface so the first step and this is just considered good practice before this duck duration of this interface we're going to add a note that says at functional interface and you might have to watch the capitalization here and you can see now that it's yelling at us this is underlined red and it says my interface is not a functional interface so that's what we're gonna have to do so functional interfaces contain only one abstract method so that's what we're going to add next so let's create a method that just displays a message something simple like that so let's make this public void and won't return anything and let's call this method just message and then add the parameters and we'll just have no parameter set up so we now have only one abstract method and this is no longer yelling at us because we added that method in just now so we now have a functional interface and now we can use a lambda expression so let's create an instance of my interface and this is just going to be similar to creating an instance of a class like an object so we type in the name of the interface my interface and let's call this instance my interface again but with a lowercase M so normally with creating and anonymous inner class we would normally type in something like equals new my interface then add a set of parentheses and then we add a code block afterwards right and then we could have this method within here now right so normally we would type in like public void message and then we would actually like do something right so we don't actually have to do that we can use a lambda expression if we want to use this message method so we're going to get rid of all this and then what we're going to type here is equals and then we're going to follow this formula here we're going to list the arguments that we want to receive and we actually don't need to receive any arguments because these parameters are blank so we can just add in a blank set of parentheses and then use the lambda operator which is the arrow and then we can perform some sort of function or method so let's just display like hello world there's something lame like that so hello actually I'll keep it in misspelt like that world alright and then if we were to call this function here it would perform this action and let's try it so we'll list the instance of the interface my interface dot and we'll use the message method that we made and let's run this and it says hello world so now if you want to execute more than just one line of code for your lambda expression you're going to surround this portion with a set of curly braces so let's do that now so after the lambda operator we're just going to enclose this within a set of curly braces and then maybe let's print a second line such as it is a nice day no and let's run this again so with lambda expressions you can perform more than just one line of code so now let's take this a step further we can actually pass in some arguments let's pass in a name so let's create a string variable called name and let's set this equal to whatever your name is for me I'll just type in bro all right and then we're going to change the method within our functional interface and we're going to set up one parameter to begin with so let's set up a string and let's just name it name then within this method call we need to pass in an argument so we're just going to pass in whatever we want it has to be a string so let's just send in our name and now with this formula for a lambda expression anything within parenthesis are the arguments that we want to send and so we need a name we can't reuse name again but let's call it something else like maybe X all right so X is going to be our name when we receive it because remember you can name things when they are received so let's place our name perhaps right here it is a nice day plus X per name and let's try this again hello world it is a nice day bro and let's pass in another argument so how about a character like a symbol so char symbol and I'm thinking maybe like an exclamation point or something so let's create a char value and let's call this symbol equals maybe a an exclamation point and then we need to set up the arguments that we're sending when we use the message method here so we're going to add comma and then symbol and then we need to set up the lambda expression so comma we can't just type in symbol because it's gonna yell at us you can see here that lambda expressions parameter symbol cannot read Eclair another local variable blah blah blah so let's just rename this as Y okay so you can see that it's no longer yelling at us and we can now use this Y variable that we receive so maybe I'll just add it here plus y so then when we run this it says hello world it is a nice day bro exclamation point our X variable which is name and our symbol variable which is y so let's create another instance of my interface and we can actually do something completely different by utilizing this abstract method message so let's create my interface and let's call this my interface to equals and we can follow the same procedure so we need to set up the arguments so with this abstract method this takes a name and a symbol a string and a character so we can set that up here and then use the lambda opera and then we can do something completely different like we could just display system dot out dot print line hello X plus y it doesn't necessarily have to be the same thing okay and then we're going to use my interface two's message function and let's run this and it says hello bro the string literal plus our variable X and our variable Y so you can see that's how functional interfaces are pretty useful since they're abstract you can kind of make these up on the go for each individual interface see both of these interfaces do something completely different so that's our first example for applying a lambda expression for a user-defined functional interface but this is even more useful if you apply Londe expressions to other predefined functional interfaces such as an action listener so what I'm thinking for a second example is that we're going to create a simple jframe with a single button and instead of the complicated process of creating an anonymous inner class for an action listener we're simply just going to use a lambda expression instead so what I'm going to do is maybe just turn all of this into a comment and don't worry if you lose this I will post all of this code and the comments down below and let's create a new class and we don't really need to use this my interface class anymore so going back to our main class so make sure you're within the same project folder go to file new class and let's call this my frame and this is going to extend jframe so extends J frame and then we'll need an import and we'll create a constructor for my frame and then before the constructor we're just going to create a simple button so J button and let's call this my button equals new J button and then let's add some text such as my button all right and then we'll need another import so make sure you have at least these two okay let's work on our frame so we can type in this dot set default close operation then within the parentheses jframe dot exit on close then for the second line let's set a size so this dot set size you can pick any size you want I usually pick four 20 by 4 20 then we need this dot set layout then I usually put null because I don't like layout managers and then lastly this dot set visible and set this to true so let's see what we have so far it should be just a simple jframe oh but we need to create an instance of this class though my bad so within your main class type in my frame and let's call this my frame equals new my frame okay here's our frame let's add a button so going back to my frame we're going to take my button and let's set the balance so set bounds I'll place this where maybe X is 100 y is 100 I'll make the width 100 and the height 100 I'm not too concerned about the aesthetics of this and then we just need to add the button to this frame so this dot add my button okay so it should be on the frame right now but it doesn't quite do anything quite yet all right actually let me extend this to 200 okay that's better so now with this button if we want this button to perform some sort of action we can utilize the action listener interface and we'll add that to this button so my button dot add action listener and do remember that lesson on anonymous inner classes well before with that what we typed in here was new action listener parentheses curly braces and then we had an anonymous inner class here we have this method action performed and then we had a print line such as you clicked a button alright so let's take a look at this action listener interface so with this interface this has one abstract method therefore we can actually place a lambda expression here instead of all of this complex code so we need to follow this formula we need to have arguments within a set of parenthesis the alum des operator and then whatever statements we want to execute within a block of code if there's more than one line we need these curly braces if there's just one line of code we don't need this so going back to my frame let's follow this formula here so we can actually get rid of most of this stuff so let's get rid of the name public void we can get rid of override here and we can get rid of the data type where we just have Eve so E is our arguments action event if then we need to use the lambda operator which is the arrow and then we have these within a set of curly braces since we have just one line of code here we don't necessarily need these so I'm just gonna get rid of these and we can get rid of these extra curly braces as well as well as everything within here and this semicolon okay and here is our lambda expression we just have he is an argument the lambda operator and then whatever code we want to perform and this would work just fine then and you can see that this is a lot less work than what we had before so let's run this and try it out so here's our button my button and when we click on it it says you clicked a button so now how about we add a second button and we'll use another lambda expression that will do something different it's kind of like what we did with the first exam where we created my interface to but this time let's create a second button instead so let's just copy what we have here for my button and change the second line to my button too and let's change it here as well so we'll have my button 1 and my button 2 we can copy all of this code here and simply just paste it but we'll change a few things will change my button to my button too and let's change with the Y position to maybe 200 so go from 100 to 200 and let's display a second message such as this is the second button and for the first button I changed it to this is the first button and then we just need to add this button to our frame so this dot add my button to let's run this now and what we get here is two buttons when you click the first button it says this is the first button and when you click the second button it says this is the second button so you can see that using a lambda expression is extremely useful for functional interfaces or predefined functional interfaces such as actionlistener where you simply just have to write one line of code basically the arguments the alarmed operator and then whatever code you want to perform so that's the basics of lambda expressions this video was meant to serve as more of an introduction to lambda expressions because there's still a lot more you can do but basically it's an anonymous method it's a shorter way to write anonymous classes that utilize a single method and you can use them with a functional interface that contain only one abstract method such as action listener or you can create your own user-defined functional interface so if you would like a copy of all this code I'll post this in the comments down below and well yeah that's how lambda expressions work in Java hey you yeah I'm talking to you if you learn something new then you can help me help you in 3 easy steps by smashing that like button drop a comment down below and subscribe if you'd like to become a fellow bro [Music]
Info
Channel: Bro Code
Views: 36,678
Rating: undefined out of 5
Keywords: Java lambda, Java lambda tutorial, Java lambda expressions Java lambda expressions tutorial, Java lambda functions, Java lambda tutorial for beginners, Java lambda expressions explained
Id: LEJ1kGHSXdA
Channel Id: undefined
Length: 18min 0sec (1080 seconds)
Published: Thu Jun 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.