so in this lecture we're going to look at the Java.util.function
package which was added in Java 8 now that package contains functional
interfaces that are meant to be used with lambda expressions now the
interfaces aren't meant to do anything specific
they represent the structure of lambda expressions that are commonly used in
java and you'll see what I mean as we look at some of the available interfaces
going to start by creating a new project i'm going to call this one employees
gonna be a regular java project create the template which we normally do so
employees is what i'm going to call it finish and open up in a new window and
that's because i'm going to copy some code from the current or the last
project we worked on into here just to save a bit of typing so the first one
is i'm going to create a class in here called employee in the new project and
i'm just going to swing over to the other project and this the lambdas
project we worked on in the previous video and i'm just going to grab the
code for that class and i'm just going to grab the entire code because i'm
using exactly the same package name and I can do that
come back over here and select all and paste and we are getting this error
about lambda expressions not being supported so just need to make sure that I'm running it
the right I'll language level open module settings and I'm gonna go to project
and make sure we're on lambdas version 8 for the project language level and
that should fix these errors right so let's fix those and the other thing I
want to copy over from the other project is the code that creates and populates
the list and as well as the code that iterates over the list and print out the
employee's name and age and that was in the main class oops the main class
from the lambdas project and so that's just this code here so i'm going to copy
that and swing back over to the new project and I think when copying I
accidentally copied the wrong file so let me just fix that up again so i'm
going to go back and what i will do first is I'm in the lambda projects i'm just going
to
copy that to the right place to main i'm just going to undo these changes
alright so we've now got the main package i'm going to paste it in first the code that
I copy and pasted and that's gonna have some errors until we copy the
class now we're going to go back to employee again and this time i'm going
to do i'm just going to copy the class you don't need all the other associated
stuff so i'm going to copy just that then go back to the new
project going to create a new class called employee and i'm going to paste
this in over-the-top their like so now just make that a public class that's
what I was trying to do the first time
alright that should fix the other errors now in main as well and we're good to go
alright so just to confirm that it's working let's run this to see that it
does work and we see that we're getting the results that we talked about in the
last video and i'll just move the run window down to the bottom you can see it
easier
alright and the code on screen you can see we use the for each method to iterate
over the employees list so if we put the cursor over the top of the for each and
on windows i believe its control q and on a Mac it's actually f1 so if I press f1
I can see you can see that I've got the documentation that's come up so i press
that or control q depending on which operating system your running but we can see
here that the method is actually looking for a parameter of type Java.util.function
.consumer and a diamond and we've got a super t in the middle
so let's actually have a look at the consumer documentation to see what
that's all about
and you can see on screen it says that this is a functional interface and if we
look at the section of the document above the method summary this part here
we see that the function method is accept down here form this operation on
the given argument the interface has an additional method called and then down
here but it has a default implementation so what that means is consumer has one
method that needs to be implemented and so it's a functional interface and
lambdas can therefore map to it now according to the documentation the accept
method performs this operation on a given argument or on the given argument
but we're not actually calling accept though we're calling for each now if you
take a look at the code for each and going to do an alt tab here to go back to
our project if we have a look at code for each and I'm pressing either command
or control depending on your operating system to have a look at the source code
and click on that and have a look you can see that for each method calls
accept and that it returns void so what does all this actually mean we pass for
each a lambda expression that meets the requirements of the consumer interface
so a consumer accepts one argument and performs an action that doesn't return a
value now it's called a consumer because its object in nothing out now this is
perfect for when we want to do something like iterate a list and print the
contents sure we could write an old-style or an enhanced for loop to do the
work but doing it on one line of code is concise and clear the lambda were using
takes an employee object go back to the code again in the case of the for each
the lambda we're using takes an employee object and it doesn't return anything so
it matches what the for each method expects consumer now when we call for
each it iterates over the list on each iteration it calls the consumers accept
method passing the employee object as the parameter now the accept method
evaluates the lambda expression using the past employee object as
the argument the employee's name and age are printed to the console and nothing is
returned the employee is consumed and the iterator moves to the next employee
in the list now as I said in the last lecture is quite a bit going on under
covers when we use for each but essentially it boils down to the
iterator calling the consumers accept method for each object in the list
accept method evaluates the lambda expression pass to by the for each
method using the pass object as the argument now have just seen an example
of how the consumer interfaces used the Java.util.function package
contains over 30 functional interfaces and don't worry we'll we won't actually be
looking at all of them but we'll touch on a few representative ones though
before we move on making sure that you are still in main java lets come over here
to the lambda sender in the gutter over here if you actually click on that
notice that it takes us to the accept method in the consumer class just as we
expected
ultimately it's the consumer which was passed as a lambda expression that does
the work so how else are the interfaces in the Java.util.function
package used let's suppose we want to print all employees older than 30 to the console
so what we do first is we will add a couple more employees just to make it a
bit more interesting closed down this consumer file
let's just add a few more here.....
....
now lets start by using an enhanced for loop so what we'll do is we'll just delete this
for loop their for now and we'll just create a new one.....
....
.....
....
.....
....so we run that we should see looking at the
list here we've got one two three employees that are 30 or above
I have got 3
I've got Red Riding Hood 35 prince charming 31 and Jack hill 40 so that's correct
we're getting the results back for 3 they're so lets change this code now to use
a lambda expression so I'm just going to comment this part this code out for now
enhanced for loop and put the lambda above so we do something like....
....
.....
....
ok we'll just run this again to confirm that it works and we got the same
results three employees so once again we pass the for each method a lambda
expression that meets the requirements of a consumer interface but let's say we
want to print out all the employees who are 30 or younger than 30 so let's actually
do that as well so I'm going to leave that first one in there then we can also
create a second loop which is for employees 30 and younger so we're going
to start out by printing a message here....
....
...
....
....so lets run that to make
sure that works as well you can see there that we've got both of them
returning the right results so while that does work we are repeating
ourselves
the only difference between the first and second lambda expressions is the if
condition so what we could do is we could move the code into a method and
pass a boolean parameter that tells the method whether we want the younger or older
employees for example we could pass a true if you want greater then and
false if you wanted less than or equal to it would work but it feels like a
hack
perhaps there is a better way and this looks like a great place to use the
predicate interface that's built into the Java.util.function package
now the test method is the functional method in this case and it accepts a
predicate that returns a boolean value we can replace the if conditions were
using in our two for each calls with a predicate going to start by moving the
code into a method called print employees by age and we'll assume we
want to print the older employees so let's do that first going to create
that method....so it's going to be private....
.....
.....so I haven't made any
changes whatsoever to that at the moment and also delete the second code block as well
.....we want to pass our employees over string as a parameter we're going to add
that we also need to somehow passed the condition will test the employees age
against and that's where the predicate will come in now we can't use the for
each loop because it expects a consumer so instead we need to go back to using
an enhanced for loop so let's try that and see what actually happens so i'm
going to change the parameters first the parameters us going to be second
problem is going to be string....
....
...
...ok so our if statement is now using the predicate parameter the for
loop passes each employee to the predicate test method which in turn uses the
employee as a parameter or as the parameter for the lambda expression that
maps to the predicate so hopefully that sounds familiar it's the same way you
have for each it uses consumer now back to predicate if the test method returns
true the employee will be printed so let's now use the method to print out
employees older than 30 and employees 30 or younger will add this code to the
main method just below the definitions where we've added the employees to the
arraylist we can do that by calling the\function.....
....
.....
....
....so you can see now that the predicate arguments are
just lambda expressions that match the predicate interface
alright so I'm going to finish the video here now in the next video will continue
on looking at this code and will actually start running this and
discussing the predicate a little bit further so we'll see you in the next
video