alright so let's go through and start talking about lambda expressions so I've
created as you can see a new project so make sure you do that so
you can follow along with what I'm doing so lambda expressions often called
lambdas are a new feature introduced with java 8 and what they do is
they provide us with an easier way to work with interfaces that have only got
one method and a typical well often used they are often used in places where we
use anonymous classes and you'll see what I mean as we progress through the
videos in this section of the course let's start by reviewing how you use a
runnable object to create and run a thread and then we will see how
this relates to lambdas so i'm going to create a new class and this file in this
main.java that we can just kick off as a thread or kick off as a
runnable object so class code to run implements its going to be runnable then
we just want to implement the method which will be the run method we're just
going to print a simple message in their.....okay and
we'll get back up to the main method and I will kick off that runnable and the
code will be a new.....
....so we run this
ok printing from the runnables that obviously works and that's one way of doing it and
we could obviously also use an anonymous class so i could just comment
out or
leave it in there for now but we could change this to....
....
.....
....and obviously give us
the same result but in both cases what we have to do is write several lines
of code to accomplish what we wanted to when all we really care about is the system
. out . print line statement everything else was sort of filler code
to get it to be able to be executed in other words the rest of the code is
there to instantiate an object and implement the single method in the
runnable interface that would be nice if we could just pass the system out print
line statement to the thread constructor and not have to write all the extra code
that isn't really doing anything now using a lambda expression we can actually
do that and here is how we would do that so I delete this again the anonymous class
method and the lambda we would type.....and this i would change it
parentheses....and then we just type sout again and
.....and we are getting this error here because i
haven't set my language level to java 8 and you can see it says lambda
expressions are not supported at this language level and I mentioned at the
start of this video it's a java 8 features so what we need to do is go
into our module settings we can do that by just right clicking up here module
settings or open module settings to be more complete and we want to make sure
to click the project that our project language level is set to 8 and you can
see here it says the lambdas I click on that click on OK the error should disappear and
we should now be able to just right click and run that and
we get the same result so that's a lambda expression now every lambda expressions
got three parts first one is the argument list
the second one is an arrow token and thirdly the body
let's take a closer look at what we wrote here the lambda expression we used
and specifically what we're talking about is this code here and I've got
highlighted so this expression has an empty argument list the arrow token is
just the - character followed by the greater than character so you can refer
to that as the arrow token the body is the code that we want to run which in
this case is the system . out . print ln statement now when the
compiler sees a lambda expression how does it know what to do
well it knows that one of the thread classes constructors accepts a runnable
parameter and in addition it also knows that the runnable interface only has one
method in this case run which doesn't take any parameters so it's able to
match the lambda expressions argument list no parameters with the run method
because the compiler needs to match the lambda expression to a method lambda
expressions can only be used with interfaces that contain only one method
that has to be implemented so these interfaces are also referred to as
functional interfaces now by using a lambda expression instead of a
creating a class that implements runnable or using an anonymous class
actually able to reduce the lines of code we have to write and now we could
focus on what we care about this case the code we want to run now before we
move on notice that the IntelliJ recognizes lambdas and marks them with the
lamba symbols in the editors gutter this little bit over here and if
you actually click that you can see what it did was it takes us to the method
that IntelliJ believed the lambda expression maps to so in this case the
open runnable . java then come down and have a look at the run method that's
because of course with the way we coded this were actually mapping to
the run method so this is that
handy way what I've just done again closing it down again this little button here by
clicking on that takes us straight there so that's a really handy way to quickly
check that the expression matches the method that i would have to implement if
we weren't using a lambda obviously the our lambda code is very simple at the
moment it's literally only one line of code whatever we wanted our thread to do
more
so what if the run method we implement would contain multiple lines
of code not a problem we can do that as well
all we have to do is surround the lambdas body with curly braces just as
we would do for any code block so let's have the thread print several lines of
output to the console and just to avoid confusion I will just delete this extra class
now that we don't need at the moment we already got printing one class if we
want those multiple lines or multiple statements then
what we do is just here before the system out print line this time we're
going to put left curly brace go to the next line and because we got multiple
lines now we need to finish the format the lines with a semicolon so that's
the first line then we can go sout maybe make this line 2 and sout again
and let's try that....
.....so if we just gonna run this to make sure it's going to work so
you can see now we've got all three lines printed to the console and you
notice that i had to type a semicolon after each statement and we didn't have
to do so when the body only had one statement in actual fact you'd get a
compile error if we add the semi colon when their is only one statement so
lambda expressions are meant to be concise the compiler doesn't need a
semicolon when the single statement in fact you can't use one when it's just a
single statement
alright so let's add a class called employee to the main file and the
classes go will start by having two fields name and age
let's go ahead and do that
we'll see how this works with a lambda expression so....
....
....okay so the main method lets just create some employees
and add them to an array list so we're going to do is leave that first line and
down here we're going to do.....
....
.....
....
ok so let's sort the employees by name in ascending order and print results
or print the result to the console so let's look at the code first that
doesn't use a lambda expression so we're going to do something like
....
.....
....
.....
....
.....and let's do a loop to
a for each to actually check the values for employee...
.....so lets actually run this just to
make sure that it is actually working alright so you can see that obviously
that method is working and we have talked about comparator before so we don't
do really need to talk too much more about that now but the collections .
sort method is using that obviously to figure out how to go about sorting and
by passing this and calling this direct its actually sorted that arraylist for us
but what I do want to do is show
you the documentation because it is relevant to lambda expressions I'm gonna open it up
and we're just going to go there
so if we go down and have a look will see that it contains two methods that don't have
default implementations compare and equals so compare and also equals these
ones here now we said that we can only use the lambdas for interfaces that
require the implementation of only one method but the comparator documentation
states at the top that the comparator implementation is a functional interface
and you probably saw this anyways as well functional interface and actually says
just up here this is a functional interface and can therefore be used as
these assignment target for lambda expression or method reference to see if you
can come up with a reason for why that might be why while we continue with our
example let's go back to our code now inside if I close this run window so inside the
collections . sort we implemented the compare method which
uses the string . compareto method to compare the employee names now the
return value of compare to matches what they compare method what it also wants to
return a negative number when the first value is less than a second value a
positive integer when the first value is greater than the second value and 0 when
the two values are equal and again we've seen that in previous
lectures so in this case it's ok to return the compared to result as the
result of our compare method and you saw when i run the application we got the
employees correctly printing out in ascending order so you see the
regular way of doing it
let's actually change the code to use a lambda expression now we can use a
lambda even though there are two methods we can implement because the equals
method will always have a default implementation now remember that all
classes descend from object and object contains an equal method and that means
that every instance that implements comparator will already have an
implementation of the equals method therefor the comparator interface really
only has a single method
that always has to be implemented by classes that implement it and for that
reason it's a functional interface and we can use lambdas instead of an
anonymous class tricky
yes and you don't have to know or memorize the stuff interfaces
documentation will sometimes tell you if it's a functional interface if it
doesn't the IDE your using might now we can see that the new comparative part of the
sort statement is grayed out in the editor and if we actually hover over this you
see that IntelliJ will pop up a message and says that we can actually
replace the anonymous class with a lambda so it's good to know in theory
but in practice if you want to know if you can use a lambda expression check
the interface documentation or course you can just try it going to finish the
video here now in the next video we'll go ahead and update this code to use a
lambda expression see you in the next video