alright so moving on now let's actually update the code to do our sorting to use
a lambda expression i'm going to just come down here and what I'm going to do
is I'm actually just going to comment that out for simplicity and also just
so we got the other code to compare it against so to do that we're
going to do.....
.....
this is where it's going to get a bit different so in brackets now we're going
to put.....
.....
.....that's actually it
so you can see this is a lot more concise now the lambda is being passed
as the second parameter instead of an anonymous comparator and we split the
lambda over 2 lines which we can do and if we run it we should see we
get the same results as we saw in the previous video you can see we still
got the same output and just to clear things up a little bit I'm comment that out
at the top and just run it again and you can see we've got our results
and clearly its sorting and working very well
alright so let's look at this lambda expression that we've just added so the
three parts that the lambda consists of are firstly the argument list so in
this case the argument list is these here the two parameters employee
employee one employee employee 2 the second part of lambda is our arrow token again
which is this one here and the third part of course is the body which is the
code on the next line which is basically that code that I'm
highlighting
so how does this actually work well when the sort method needs to compare two
objects in the list instead of calling the compare method of our comparator and
passing it in the two objects it will instead evaluate the lambda expression
using the two objects it wants to compare as the parameters to the lambda
now one nice thing about lambdas is so that it's easy to see what code will be
run and this is especially true when dealing with shorter methods when one or
two lines of code are buried within an anonymous class as we've seen previously
that can actually be hard to see or spot very quickly but the good news is
we can actually even simplify this lambda expression even further with the
compiler can infer the parameter types and we don't actually need to include the
types in a lambda in our case the compiler can infer from the first
parameter employees that the objects to be compared were both be of type employee
and because of that we can actually leave the types out and just rewrite it
like this and come back here actually delete the type and just wrote it to
confirm that its still working and that's still working as well now if there's
only one parameter we don't actually need the parentheses in the argument
list so let's take a look at a common use of lambdas now for this we're going
to need a java FX application let's go ahead and create a new project so i'm
going to go to file new project as we've done countless times i'm going to
call this one select java fx first will call this one button fx
to keep it simple.....and what we will do is will keep the lambda
expressions project open and we're going to come back to it so click on new
window so that both projects are open just make this a bit larger so we can
see it better
alright so first we want to add a button to the grid pane so going to open up our
source and and go to sample . fxml so we're going to go and open to create a
button or add a button....
....
.....
....ok now we could add an action handler
here in the fxml we've done that before in the javafx section of this
course and I accidentally deleted the closing grid pane so I will fix that up....
.....
ok so we've seen how we add action handlers here let's suppose we want to
do the controller instead or in the fxml file so we are gonna need FX ID which
is why we're added that so we can access that and actually assign the action
handler in the initialize method back in controller and let's go back and do that
so what we're going to do first is well add the version of the code that doesn't use
a lambda and we need firstly a private button......
.....
.....so let's do it
the first way that doesn't use a lambda expression to set an action so we'll do
that with click me button.....
....
.....and using an
anonymous class for the event handler like I did there is very common in ui
programming especially actually in javas old tool kit swing which didn't actually
have anything like fxml
everything actually had to be done using code so when we run this application let's do
that you should see that we got a button on the screen and were able to click it and
see the output on the console down the bottom you can see the run window you can see click
me so clearly that's working so we got our action handler added via code
that's working nicely but now we actually know about lambda expressions
whenever we see an anonymous class definition that only overrides one
method we can consider using a lambda so lets actually change the code to do that now
we're going to do first is delete the anonymous class parameter i'm going
to just delete all that code and set on action will actually be.....
....
....and again because i've
created a new classes its defaulting to the wrong language level i'm just going to
change that
clicking over here and change it back to 8 again the language level project
lambdas click on ok alright so when we run this now and we got the
same result but this time using a lambda and the point here is just closing out
the run window when we only have one parameter we don't have to put that one
parameter inside parentheses and that's why we just type that there without
parentheses this time we only have to use parentheses when their are no parameters
or more than one parameter when we ran the application you saw that it worked
like I did before you've probably seen enough now to know that we map a method
to a lambda expression and as follows
number one the method parameters become the argument list number 2 the error
token is always there
number three the body of the method becomes the body of the lambda
expression but what about interfaces with a single method that returns a
value
now we've only use one in the comparator example but the sort method handled the
return value
let's take a closer look with a different example i'm going to go back
to the lambda expressions project we're going to add a new interface to the main
files let's go ahead and do that go down the bottom here going to add interface
....
.....so this interface contains a method that upper
case two strings and then concatenates them all joins them or pins the second one
to the first one so now that we've added the interface lets add a
static method to the main class that uses an up concat instance to create
new string and come back up here just below the last class there the end of
the class and put....
....
....so the method accepts an object that implements upper concat and
the two strings that we want uppercase and concatenated it calls the upper
concat method in the upper concat interface to do the work now inside the
main method what we need to do is create a new method we're going to pass an
anonymous classes as the first parameter and the first two employees in the list
as the second or three parameters so lets go and do that just up here just after
the for loop it is going to be....
....
.....
....
.....
.....so when we run this we can see
we get jack hill John doe so hopefully this point you might be thinking we're using
an anonymous class that overrides the only method in interface let's use a
lambda expression instead now we still have to define the interface we can't
get away from doing that when using lambdas but we can definitely simplify
the rest of the code by passing a lambda instead of an anonymous class and
remembering the three parts to a lambda parameters arrow taken and body so to
start with lets define the lambda on a separate line and assign it to a variable
of type upper concat so we're going to just comment out the code we just added so we
still got it there to refer to so let's go ahead and define so it's going to be....
.....
.....notice here that we haven't included the return
keyword in fact if we try to add it doing something like
....we get a compile error when the lambda body consists of a
single statement that evaluates to a value return keyword is assumed and the
return value is in third to be the type of the evaluated value which in this
case is string so going to undo those changes but we can go one step further
the compiler can infer the types of the parameters so don't need to
include those either so you can come back here and delete those to make more
concise and likewise for the second string as well so you can see that now is
fairly concise and now that we have a lambda we can use it as the first
parameter in our call to do string stuff so we just type in that code again....
....
.....
....
...so if we run that that also works well as you
can see we got our Jack Hill and John Doe output again
alright I'm going to finish the video here now in the next video will continue
on with our journey with lambda expressions see you in the next video