Java 8 Lambda Basics 7 - Lambda Expression Examples

Java 8 Lambda Basics 7 - Lambda Expression Examples

SUBTITLE'S INFO:

Language: English

Type: Robot

Number of phrases: 236

Number of words: 1781

Number of symbols: 7498

DOWNLOAD SUBTITLES:

DOWNLOAD AUDIO AND VIDEO:

SUBTITLES:

Subtitles generated by robot
00:00
let us now write a few more lambda expressions to flex our lambda muscles this is kind of practice to see what it takes to build these lambda expressions you know compared to the kind of functions that we are used to so for instance let's start with the greet method so we had like a greeting function which was a lambda expression which looks something like this this is a empty list of arguments and the actual
00:31
body of the function was system dot out dot print of hello world this is a lambda expression what this does is have a function inline which takes in no arguments and the body of the function just as systems are order print of hello world so this function is assigned to the variable called greening function alright now one thing that you must have
01:02
noticed is I don't have the type here what is the type of this variable feeling function let's hold that thought for a bit I haven't talked about the type of these lambda expressions yet I'll come at it a bit later but for now I want you to focus on just the right hand side here them obviously since this is jaw line you could declare this type of the cleaning function I haven't done that here that's actually why I'm typing this in a different editor and it's not eclipse because I don't want the squiggly lines to show up and distract us so again hold it out about the left hand side for a bit and what the type of
01:33
these variables are the right hand side is the expression itself that we are focusing on at this time now one of the advantages of having this function be assigned to a variable is that this variable can be passed around you can send it to another method as an argument and have that method have access to this function so for instance I can do agreed method I can call a grid method and pass in the deleting function right so you remember we were talking about how you need to write you need to be able to
02:03
write a public void greet which takes in some kind of an action and then it performs that action right so this is kind of enabling that isn't it you're able to take this action and assign it to a greeting function the action being just printing off hello world and now for you to pass this there is a way for our greet method to accept that lambda expression and execute it the syntax is obviously not this we'll talk about how to execute a lambda function in a bit but that's the idea we
02:36
have encapsulated a function in a variable that can be passed around and it can be sent to other methods that's really cool now what's even cooler is you don't really have to assign it to a variable you can have this lambda expression be inline so let's say I were to take this out and instead of assigning it to a variable passing it here I can just pass the lambda expression here like this so what we're doing is we are passing a lambda expression of function to the greet method and that function is in line
03:08
right here so this is a function which doesn't take any arguments and it just prints hello world to the console right this is one way in which we typically use lambda expressions we can have that be inline so we'll write a few more lambda expressions and that will make sense but at this point of time let's look at different varieties in which we can write these expressions so this is a lambda expression which is a function now functions are not always with empty parameters you might need a function
03:38
which takes in arguments now how do you write that as a lambda expression now let's say I have a function which is a double number function which takes in an integer and returns a double of that all right so let's say I have this table number function which is let's write the method at this time does it say this is a public in table all right and then I have an int a and what this does is it
04:08
returns a times two okay so this is my function that I want to assign to this this is not a lambda expression now what do I have to do to convert this to a lambda expression the first thing is remove the modifier it's not attached to a class it's inline so public doesn't make sense so get rid of us next the name here doesn't make sense because we're going to be referring to this function by using this name double number function so we're gonna get rid of this next is the return type again
04:40
like I said the compiler now is smart to detect return types in lambda expressions so it sees that this is a times two so it knows that this is an integer because the input is an integer so integer times two should be another integer so it says I've got this don't send me this so we're gonna remove this so this is our lambda expression but remember there is one other thing we need to do which is the arrow which is the minus and the greater than symbol now this when you add the semicolon here that this is a complete lambda
05:12
expression again since this is just one line I can use the shortcut and get rid of this here now here's another shortcut if your lambda expression is just one line you can skip the return as well so what Java compiler is going to do is what the Java interpreter is going to do is it's going to look at what the body of the function is and it is going to return that so you don't have to specify a return here or so this makes it really really crisp and
05:42
really really terse so this is a lambda expression which takes in an integer and it returns this so what's on the right hand side of this arrow is actually the return so this might seem a bit confusing so let's do one more let's do a function which adds two numbers let's call this the add function which takes in two arguments two integers and then it returns the sum of those two so would this look like this is the structure
06:13
right and then there is first of all there is the input arguments is the arrow and then what is being returned so what are the input arguments for adding two numbers that's in a and then int B now what does a return it is a plus B this provides a lambda expression which adds two numbers up passing these two arguments it returns the switch is a plus B okay now if you look at the you might be asking a question well how does it work over here is the greeting
06:43
function returning the return of system dadada print in away yes because system dot out dot print really returns a wide so it works over here but what if this were returning something else this is a question that you're gonna have to hold on too far a little bit longer let's write a few more lambda expressions and I'll talk about two things that I said I will talk about later one is the type of these expressions and how to call them and the return type is something that will address when we talk about the type of these X these variables alright so
07:15
let's do one more let's call this a divide function suppose there's a safe divide function right I want to write a safe divide which which basically takes in two integers and it returns an integer division and what it also needs to do is make sure that you don't do a divide by zero right if the second argument is a zero just return zero but if the second argument is not zero then to an integer division all right so let's see how that works I can do it in
07:45
a and int P and I can do with this write a slash P now this is going to do a divided by B and it's going to return it but what I want there's a safe division I want to make sure that the second argument is not zero so for this I need to add an if block and since I need to add an if block and this function is grown to more than one line I need to add the curly braces so let me go ahead and do that here the curly braces and here I'm going to say if B is equal to
08:19
zero then I want to return zero else I have done eight divided by P okay so this is my lambda expression again since this is more than one line I have to add this curly braces alright I'm going to do one last expression to really drive the point home and then we can wrap this up so let's say I want to add I want to create a function which does a string length count right so it's a string length count function the name of these really do not matter I'm just putting some
08:52
arbitrary names over here is just a variable name so what is this function going to be like it takes in a string and it returns the count of the number of characters in that string so let's say this is gonna be a string s and then this returns s star length okay this is a lambda expression which finds out what the length of the string is given an input string argument so hopefully by looking at all these things all these different examples you're kind of
09:23
getting familiar with uncomfortable that the writing lambda expressions these in this way so in the next tutorial we'll tackle some of the pending questions first of all what's the type of this of these variables and secondly given a variable like this let's say I'd give you this variable which is the string length count function which holds this lambda expression which holds this function how do I execute this thing let's look at that in the next tutorial

DOWNLOAD SUBTITLES: