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