alright so moving onto a second video
with challenges challenge number 8 alright so moving onto a second video
so challenge number 8 so theirs many with challenges challenge number 8
interfaces in the java software so challenge number 8 so theirs many
development kit and sometimes we can use interfaces in the java software
a lambda expression instead of creating development kit and sometimes we can use
an instance that implements the a lambda expression instead of creating
interface that we want to use so given a an instance that implements the
specific interface interface that we want to use so given a
how can we tell whether we can map a specific interface
lambda expression to in other words how can we tell whether we can map a
what's the criteria that has to be met lambda expression to in other words
so pause the video have a think about what's the criteria that has to be met
that and see if you can figure out what so pause the video have a think about
that is and i'll see you when you get that and see if you can figure out what
back alright so let's have a look at that is and i'll see you when you get
that the answer well the interface has to back alright so let's have a look at
be a functional interface can have only that the answer well the interface has to
a single method that must be implemented be a functional interface can have only
now functional interface can contain a single method that must be implemented
more than one method but all the methods now functional interface can contain
but one must have default more than one method but all the methods
implementations now most of the time but one must have default
the documentation for an interface will implementations now most of the time
state whether it's a functional the documentation for an interface will
interface state whether it's a functional
alright so moving on interface
next question here is what we just said alright so moving on
in mind can we then use a lambda next question here is what we just said
expression to represent an instance of in mind can we then use a lambda
the java.util.concurrent.callable expression to represent an instance of
interface and as a hint here you have the java.util.concurrent.callable
to check the documentation and as a interface and as a hint here you have
java developer you have to be really to check the documentation and as a
comfortable looking up a reading java developer you have to be really
documentation it's a really important comfortable looking up a reading
part of the process of being a developer documentation it's a really important
and theirs a link on the screen you can part of the process of being a developer
see there to the call of documentation and theirs a link on the screen you can
links also in the resources section so see there to the call of documentation
check that out and see whether you links also in the resources section so
can answer that question and i'll see check that out and see whether you
or pause the video now and i'll see you can answer that question and i'll see
when you get back or pause the video now and i'll see you
alright the answer the callable when you get back
interfaces only one method that has to alright the answer the callable
be implemented the call method so in interfaces only one method that has to
other words we can use a lambda for it be implemented the call method so in
and if you went to the documentation and other words we can use a lambda for it
have a look you could see that you would and if you went to the documentation and
see that the documentation states that have a look you could see that you would
it's a functional interface see that the documentation states that
alright next question is that it's a functional interface
java.util.comperator or alright next question is that
comparator i should say interface a java.util.comperator or
functional interface see if you can comparator i should say interface a
figure that out and pause the video and functional interface see if you can
I'll see you when you get back with the figure that out and pause the video and
answer the answer to that is yes it is I'll see you when you get back with the
so despite containing over ten methods answer the answer to that is yes it is
only one method has to be implemented so despite containing over ten methods
and that's the compare method because of only one method has to be implemented
that it's a functional interface and and that's the compare method because of
there's a link on the screen to the that it's a functional interface and
documentation which you may have there's a link on the screen to the
already found done a bit of documentation which you may have
research on links also the resources already found done a bit of
section research on links also the resources
alright so that is challenge eight section
completed let's move on now to the next alright so that is challenge eight
one challenge 9 and we're going to completed let's move on now to the next
get back into a bit of coding one challenge 9 and we're going to
alright challenge 9 get back into a bit of coding
so let's suppose we got the following alright challenge 9
list of the top five male and female so let's suppose we got the following
names for 2015 list of the top five male and female
so with that list you need to write some names for 2015
code to print the items in the list in so with that list you need to write some
sorted order and with the first letter code to print the items in the list in
in each name upper cased so the name sorted order and with the first letter
Harry in lowercase should be printed as in each name upper cased so the name
Harry with a capital H and should be Harry in lowercase should be printed as
printed after Emily and before Isla Harry with a capital H and should be
now you want to use lambda expressions printed after Emily and before Isla
obviously wherever possible because this now you want to use lambda expressions
is a lambda challenge so go ahead and obviously wherever possible because this
try that and pause the video now and is a lambda challenge so go ahead and
i'll see you when you get back with the try that and pause the video now and
answer just before i do that the list i'll see you when you get back with the
you can see on screen there it is answer just before i do that the list
available as a resource in this video so you can see on screen there it is
you can just copy and paste that available as a resource in this video so
directly into save you some typing you can just copy and paste that
ok let's have a look at the answer for directly into save you some typing
that ok let's have a look at the answer for
so I'm just going to start by pasting in that
the list of baby names so I'm just going to start by pasting in
ok the list of baby names
so want to write the code to print the items ok
in the list in sorted order so how do we so want to write the code to print the items
do that we're going to start by defining in the list in sorted order so how do we
a new array list so its going to be a list of string do that we're going to start by defining
.... a new array list so its going to be a list of string
..... ....
.....
.... ....
..... ....
.....
.... ....
..... ....
..... .....
.... .....
....
.....
.... ....
alright so now that we've done that we ....
then want to sort it so i'm going to do alright so now that we've done that we
a first uppercase.... then want to sort it so i'm going to do
..... a first uppercase....
.... .....
....
.....
....
.....
.... ....
so their are many ways to complete this but my ....
solution on the screens is just one so their are many ways to complete this but my
answer so we create a new list that solution on the screens is just one
holds all the names with first letter answer so we create a new list that
uppercase and I used to for each method holds all the names with first letter
to iterate over the list we use uppercase and I used to for each method
sub string to get the first letter for to iterate over the list we use
each name then we upper case that sub string to get the first letter for
letter then use sub string again to each name then we upper case that
concatenate it with the rest of the name letter then use sub string again to
and we added the result of the lambda concatenate it with the rest of the name
expression to the first uppercase list list and we added the result of the lambda
now when for each completes the first expression to the first uppercase list list
uppercase list contains all the names now when for each completes the first
with the first letters uppercase and uppercase list contains all the names
then used the sort method to pass it a with the first letters uppercase and
comparator in our comparator we're calling then used the sort method to pass it a
the string . compareto method and now comparator in our comparator we're calling
we've been print each item in a sorted the string . compareto method and now
list to the console so I've used lambdas we've been print each item in a sorted
in three places with the for each method list to the console so I've used lambdas
with the sort method and with the system in three places with the for each method
. out . print line method with the sort method and with the system
as i said this is only one way to do it . out . print line method
there are other ways including using as i said this is only one way to do it
third-party libraries that manipulate there are other ways including using
strings but this works and we'll use third-party libraries that manipulate
this version of the code going forward strings but this works and we'll use
alright so let's move on now to this version of the code going forward
challenge 10 alright so let's move on now to
alright challenge 10 challenge 10
so we now want to change the code so that it alright challenge 10
uses method references remember that a so we now want to change the code so that it
method reference looks like class :: uses method references remember that a
method name so based on the method reference looks like class ::
code that I provided in my solution see if method name so based on the
you can convert that to use method code that I provided in my solution see if
references so pause the video and I'll you can convert that to use method
see you when you get back alright so how references so pause the video and I'll
do we convert that to those two lines to see you when you get back alright so how
use method references the lines that do we convert that to those two lines to
we're working to change are lines 67 and use method references the lines that
68 using the . sort and the for each so we're working to change are lines 67 and
first thing I'm going to comment those out so 68 using the . sort and the for each so
we've got the original code in there you first thing I'm going to comment those out so
need to download the source code so in we've got the original code in there you
terms of making that sort line and need to download the source code so in
method reference to use a method reference terms of making that sort line and
.... method reference to use a method reference
.... ....
..... ....
.....
.....and that's going to be.... ....
....so that's .....and that's going to be....
it and actually have been running this ....so that's
have I so I just run this to see that it's it and actually have been running this
actually working probably should have have I so I just run this to see that it's
done with the previous challenge and you actually working probably should have
can see we get everything in uppercase done with the previous challenge and you
there and sorted correctly so that's can see we get everything in uppercase
working nicely there and sorted correctly so that's
alright time for challenge 11 working nicely
alright so now i want to do the same alright time for challenge 11
thing upper casing the first letter and alright so now i want to do the same
sorting and printing the list using a thing upper casing the first letter and
stream and a chain of stream operations sorting and printing the list using a
so pause the video now go and do that stream and a chain of stream operations
and I'll see you when you get back so pause the video now go and do that
alright so how we go about that and I'll see you when you get back
converting this or using stream to alright so how we go about that
sort the baby names converting this or using stream to
what I'm going to do is I'm just going sort the baby names
to click comment that out and we're going to what I'm going to do is I'm just going
come in here and i'm going to type.... to click comment that out and we're going to
actually what we can do is well comment come in here and i'm going to type....
those out as well because the stream is actually what we can do is well comment
gonna handle the sorting stuff as well those out as well because the stream is
so we're going to do.... gonna handle the sorting stuff as well
..... so we're going to do....
.... .....
.... ....
couple of things to note here ....
first we didn't have to create a new couple of things to note here
list to hold the names with uppercase first we didn't have to create a new
first letters the map method returns a list to hold the names with uppercase
stream containing all the transform first letters the map method returns a
transformed names now secondly this stream containing all the transform
code is a little more concise than the transformed names now secondly this
previous versions and just as easy to code is a little more concise than the
read in this case so we went from java previous versions and just as easy to
code that used lambda expressions to read in this case so we went from java
more concise code that used method code that used lambda expressions to
references to streams and when more concise code that used method
you're first learning how to code references to streams and when
using lambdas or streams this is a good you're first learning how to code
way to do it using lambdas or streams this is a good
we start by writing the more verbose way to do it
version that doesn't use lambdas but we start by writing the more verbose
uses anonymous classes you can replace version that doesn't use lambdas but
the anonymous class with lambdas or use uses anonymous classes you can replace
method references where applicable or the anonymous class with lambdas or use
when applicable and then finally we can method references where applicable or
rewrite the code to use streams but of when applicable and then finally we can
course only when it makes sense to use rewrite the code to use streams but of
them course only when it makes sense to use
just run this to make sure that it does them
work we still get the same output this just run this to make sure that it does
that's working nicely work we still get the same output this
alright let's move on to challenge 12 that's working nicely
alright the challenge 12 alright let's move on to challenge 12
so instead of printing out the sorted alright the challenge 12
names print out how many name start with so instead of printing out the sorted
the letter a in uppercase instead t hints here names print out how many name start with
you have to modify the stream chain and the letter a in uppercase instead t hints here
secondly you'll have to add another you have to modify the stream chain and
statement to print the number of items secondly you'll have to add another
so that's the challenge statement to print the number of items
pause the video and see if you can so that's the challenge
figure out that challenge and i'll see pause the video and see if you can
you when you get back alright so let's figure out that challenge and i'll see
have a go at doing that you when you get back alright so let's
counting the names beginning with the a have a go at doing that
would be a long.... counting the names beginning with the a
.... would be a long....
.... ....
let's just run that then we'll just talk ....
about the code solution you can see we got let's just run that then we'll just talk
names beginning with a too and we've about the code solution you can see we got
got a merely an a over their so that's names beginning with a too and we've
correct got a merely an a over their so that's
so here we use the filter method to correct
remove all items from the stream that so here we use the filter method to
don't begin with a and then we use the remove all items from the stream that
count method to get the number of items don't begin with a and then we use the
in the resulting stream which again as I count method to get the number of items
just pointed out was 2 alright we got in the resulting stream which again as I
two challenges to go lets now look just pointed out was 2 alright we got
at challenge number 13 two challenges to go lets now look
alright so challenge number 13 at challenge number 13
let's go back to the previous version of alright so challenge number 13
the code when we're printing the sorted let's go back to the previous version of
names the code when we're printing the sorted
let's suppose that we want debug what's names
going on when the chains executed let's suppose that we want debug what's
instead of printing out the names at the going on when the chains executed
end of the chain you know maybe we're instead of printing out the names at the
not sure the code that upper cases the end of the chain you know maybe we're
first letter is working correctly so not sure the code that upper cases the
let's use peek to print out the names first letter is working correctly so
after the map method is executed so what let's use peek to print out the names
will the following code on the next after the map method is executed so what
screen print to the console there's the will the following code on the next
code on the screen so what will be the screen print to the console there's the
result of that code when we executed so code on the screen so what will be the
in other words what can be printed out result of that code when we executed so
on the console so pause the video now in other words what can be printed out
and see if you can figure out the answer on the console so pause the video now
and i'll see you when you get back and see if you can figure out the answer
so the answer bit of a trick question I am and i'll see you when you get back
afraid the answer is nothing the chain so the answer bit of a trick question I am
doesn't contain a terminal operation so afraid the answer is nothing the chain
nothing is printed out remember that doesn't contain a terminal operation so
stream chains are evaluated lazily so in nothing is printed out remember that
other words nothing happens until the stream chains are evaluated lazily so in
terminal operations added to the chain other words nothing happens until the
at that point the entire chain is then terminal operations added to the chain
executed at that point the entire chain is then
alright so let's now move on to the last executed
challenge challenge number 14 alright alright so let's now move on to the last
challenge 14 last one challenge challenge number 14 alright
your challenge is to add a terminal challenge 14 last one
operation to this chain the code that i your challenge is to add a terminal
just showed you so the peek call will operation to this chain the code that i
execute and you can grab that code in just showed you so the peek call will
the resources section of this video to execute and you can grab that code in
save you having to type it in if you need the resources section of this video to
to check the documentation for save you having to type it in if you need
stream at the link here on the screen to check the documentation for
and again that's in also the link is in stream at the link here on the screen
the resources section of the video now and again that's in also the link is in
since the peek call is printed every the resources section of the video now
item try to do something else with the since the peek call is printed every
terminal operation other words don't item try to do something else with the
just print out the items again and see terminal operation other words don't
if you can attempt that pause the video just print out the items again and see
now and i'll see you when you get back if you can attempt that pause the video
alright so let's start off with the code now and i'll see you when you get back
that we know doesn't do anything at alright so let's start off with the code
this stage I'm just going to print it that we know doesn't do anything at
out or paste it in I should say this stage I'm just going to print it
that's our basic code if we run that we out or paste it in I should say
should find it doesn't do anything that's our basic code if we run that we
we only got the code the output from should find it doesn't do anything
line 84 and again we haven't added the we only got the code the output from
terminal operation that's why it's line 84 and again we haven't added the
not actually working so let's add a terminal operation that's why it's
terminal operation so going to add not actually working so let's add a
one and we'll just use . count terminal operation so going to add
so now that even though were throwing one and we'll just use . count
away the result you should find the peek so now that even though were throwing
call runs and prints out the names at away the result you should find the peek
this stage which aren't sorted so we don't call runs and prints out the names at
actually have to use the result of this stage which aren't sorted so we don't
terminal operations let's just run this actually have to use the result of
to confirm that it does work you can see terminal operations let's just run this
we are now getting output put again and to confirm that it does work you can see
again it's printing the names at the stage we are now getting output put again and
which they aren't sorted in case you're again it's printing the names at the stage
wondering because of course the stream which they aren't sorted in case you're
for sorting it is after the peek another wondering because of course the stream
possible result would be changing for sorting it is after the peek another
count..... possible result would be changing
.... count.....
run that again like so and what ....
I did there was used . collect which is run that again like so and what
a terminal operation to store the result I did there was used . collect which is
of a chain to a list we may want to pass a terminal operation to store the result
the results to a method or do something of a chain to a list we may want to pass
else with the list at that point the results to a method or do something
alright so that's it for the lambda else with the list at that point
expressions challenge and actually for alright so that's it for the lambda
this entire section expressions challenge and actually for
my advice is to get used to working with this entire section
lambdas and streams by practicing using them my advice is to get used to working with
at some point you'll think of a lambda lambdas and streams by practicing using them
expression first rather than using at some point you'll think of a lambda
anonymous class so i hope you enjoyed expression first rather than using
this section of code lambdas are really anonymous class so i hope you enjoyed
quite interesting and useful i'll see this section of code lambdas are really
you in the next section of the course quite interesting and useful i'll see