Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Lambdas are anonymous functions and are new to Java 8.
Read more
-
0:00
Okay, so first things first, we're gonns need to set our language level to eight.
-
0:05
By default, probably things are set to seven.
-
0:08
Most editors keep themselves pinned to a current language level just to make sure
-
0:12
that others using your code know what they can and can't use.
-
0:15
So, let's check it out.
-
0:17
So if we go File > Project Structure, and here under Project,
-
0:21
you'll see the project language level.
-
0:25
And just like I thought, it was limited to seven.
-
0:28
So let's go ahead and let's flip that to eight.
-
0:30
And we'll click OK.
-
0:32
You'll notice now that this turned gray here.
-
0:34
Then what this is saying,
-
0:36
is it's saying that the new comparator book can be replaced with a lambda.
-
0:40
Let's open that up just a little bit more.
-
0:42
It says, this inspection imparts all anonymous classes,
-
0:45
which this was, which can be replaced with lambda expressions.
-
0:48
Okay, cool, so let's just do that.
-
0:50
Let's leave this reference here, for what was there, and we will dupe this.
-
1:00
And let's make a new method that will accomplish the same thing.
-
1:05
Like Def Leppard said, let's pour some sugar on it, syntactic sugar, that is.
-
1:10
So I'm gonna show you a long-form class first.
-
1:14
So we'll call this usingLambdasInLongForm.
-
1:20
Okay, so what we're gonna replace is we're gonna get rid of this comparator that we
-
1:23
have going on here.
-
1:24
So I'm gonna get rid of this.
-
1:28
So the first thing that we wanna do here is put a pair of parentheses, and
-
1:32
inside of these parentheses is where we define our parameters.
-
1:36
So the parameters that we know from before that were
-
1:39
being requested was there was this book.
-
1:41
There's a book and it was called b1, it doesn't have to be,
-
1:44
it can be called whatever.
-
1:45
And b2. In order to say that this is a lambda,
-
1:48
what we want to do is we want to make a little arrow.
-
1:52
You do a minus and then a greater-than sign.
-
1:55
See how it looks like an arrow?
-
1:56
And then we can use the curly braces, just like we did before, and
-
1:59
we can do the same code that we had before too as well.
-
2:02
So we'll just copy this, and paste that here.
-
2:07
Cool, so let's go ahead and we'll change this to say, usingLambdasInLongForm.
-
2:12
We'll save it and we'll run it.
-
2:16
Okay, cool, it's still working.
-
2:18
That's definitely some space savings line-wise, right?
-
2:21
We didn't have to declare the new type, the comparator book,
-
2:24
it just kind of figured that out.
-
2:25
And we also didn't have to declare the method name which was compared,
-
2:28
it also figured that out right?
-
2:30
It doesn't say anything about the method name being compare or
-
2:32
that it's the comparator interface.
-
2:34
But we can make this even more concise.
-
2:36
So, if you've come across some of these before you were probably wondering what
-
2:40
the heck was going on, I know I did the first time I saw them.
-
2:42
So let's go ahead and let's dupe this method into a short form.
-
2:48
So, we'll say public static in ShortForm this time.
-
2:56
Okay, so the compiler can actually figure stuff out
-
2:59
well enough that you don't even need to declare types.
-
3:01
So, let's get rid of those, right.
-
3:02
So, it knows that it's expecting two books.
-
3:05
So, let's get rid of that.
-
3:07
Okay, cool.
-
3:08
Now, if you have a single line method, you actually don't need the curly braces.
-
3:12
Let's go ahead and get rid of those.
-
3:17
You put this all up on the next line.
-
3:20
When you only have a one-liner you actually don't even need the return
-
3:23
keyword, it knows what's expected to be returned.
-
3:26
So, let's get rid of the keyword.
-
3:28
Return. And let's bring this up a line, cool.
-
3:31
So that one-liner is called an expression type body.
-
3:34
This creates a function that accepts two books, and
-
3:37
returns the value from this statement here.
-
3:40
Let's make sure that we change this to say usingLambdasInShortForm.
-
3:47
All right, so let's run it.
-
3:49
Cool, it still works.
-
3:51
Pretty succinct, right?
-
3:53
Now, like I mentioned,
-
3:54
these opened up a new style of programming known as functional programming.
-
3:57
We're not gonna delve to deep here, we'll cover that fully later in another course.
-
4:02
But, let's get a quick little sneak peak.
-
4:04
So, the collections framework really benefits from functional programming.
-
4:08
And one super cool thing that got added is the forEach method on collections.
-
4:12
It takes a consumer, which is a newly introduced functional interface,
-
4:15
remember that's the new name for a single abstract methods.
-
4:18
Now, a consumer expects a parameter that takes one of its container's types.
-
4:23
So, let's replace this forEach loop.
-
4:28
So, we'll do books.forEach, which is the new method.
-
4:34
And, it takes a consumer, so the consumer is book.
-
4:38
And for each one of those books, we're just gonna print out.
-
4:43
Cool, so now if I run it, still working.
-
4:46
All right, are you ready for even more succinctness?
-
4:49
When you just have one parameter, you don't even need the parentheses, so
-
4:52
let's get rid of this cuz there's only one.
-
4:54
So it's saying for each book, run this.
-
4:58
So since we're here, I wanna show you one more thing that you might come across that
-
5:02
might look a little bit foreign, and that's called method references.
-
5:06
Let's take a look in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up