1 00:00:00,270 --> 00:00:05,170 Okay, so first things first, we're gonna need to set our language level to eight. 2 00:00:05,170 --> 00:00:08,220 By default, probably things are set to seven. 3 00:00:08,220 --> 00:00:12,010 Most editors keep themselves pinned to a current language level just to make sure 4 00:00:12,010 --> 00:00:15,670 that others using your code know what they can and can't use. 5 00:00:15,670 --> 00:00:17,020 So, let's check it out. 6 00:00:17,020 --> 00:00:21,670 So if we go File > Project Structure, and here under Project, 7 00:00:21,670 --> 00:00:24,180 you'll see the project language level. 8 00:00:25,380 --> 00:00:28,150 And just like I thought, it was limited to seven. 9 00:00:28,150 --> 00:00:30,290 So let's go ahead and let's flip that to eight. 10 00:00:30,290 --> 00:00:31,300 And we'll click OK. 11 00:00:32,580 --> 00:00:34,765 You'll notice now that this turned gray here. 12 00:00:34,765 --> 00:00:36,170 Then what this is saying, 13 00:00:36,170 --> 00:00:40,290 is it's saying that the new comparator book can be replaced with a lambda. 14 00:00:40,290 --> 00:00:42,570 Let's open that up just a little bit more. 15 00:00:42,570 --> 00:00:45,160 It says, this inspection reports all anonymous classes, 16 00:00:45,160 --> 00:00:48,500 which this was, which can be replaced with lambda expressions. 17 00:00:48,500 --> 00:00:50,620 Okay, cool, so let's just do that. 18 00:00:50,620 --> 00:00:56,860 Let's leave this reference here, for what was there, and we will dupe this. 19 00:01:00,631 --> 00:01:05,160 And let's make a new method that will accomplish the same thing. 20 00:01:05,160 --> 00:01:08,750 Like Def Leppard said, let's pour some sugar on it, syntactic sugar, that is. 21 00:01:10,330 --> 00:01:14,560 So I'm gonna show you a long-form class first. 22 00:01:14,560 --> 00:01:20,724 So we'll call this usingLambdasInLongForm. 23 00:01:20,724 --> 00:01:23,972 Okay, so what we're gonna replace is we're gonna get rid of this comparator that we 24 00:01:23,972 --> 00:01:24,760 have going on here. 25 00:01:24,760 --> 00:01:26,030 So I'm gonna get rid of this. 26 00:01:28,260 --> 00:01:32,810 So the first thing that we wanna do here is put a pair of parentheses, and 27 00:01:32,810 --> 00:01:36,560 inside of these parentheses is where we define our parameters. 28 00:01:36,560 --> 00:01:39,270 So the parameters that we know from before that were 29 00:01:39,270 --> 00:01:40,680 being requested was there was this book. 30 00:01:41,830 --> 00:01:44,220 There's a book and it was called b1, it doesn't have to be, 31 00:01:44,220 --> 00:01:45,670 it can be called whatever. 32 00:01:45,670 --> 00:01:48,760 And b2. In order to say that this is a lambda, 33 00:01:48,760 --> 00:01:52,550 what we want to do is we want to make a little arrow. 34 00:01:52,550 --> 00:01:55,760 You do a minus and then a greater-than sign. 35 00:01:55,760 --> 00:01:56,880 See how it looks like an arrow? 36 00:01:56,880 --> 00:01:59,920 And then we can use the curly braces, just like we did before, and 37 00:01:59,920 --> 00:02:02,440 we can do the same code that we had before too as well. 38 00:02:02,440 --> 00:02:05,240 So we'll just copy this, and paste that here. 39 00:02:07,140 --> 00:02:12,560 Cool, so let's go ahead and we'll change this to say, usingLambdasInLongForm. 40 00:02:12,560 --> 00:02:14,330 We'll save it and we'll run it. 41 00:02:16,750 --> 00:02:18,250 Okay, cool, it's still working. 42 00:02:18,250 --> 00:02:21,140 That's definitely some space savings line-wise, right? 43 00:02:21,140 --> 00:02:24,430 We didn't have to declare the new type, the comparator book, 44 00:02:24,430 --> 00:02:25,480 it just kind of figured that out. 45 00:02:25,480 --> 00:02:28,660 And we also didn't have to declare the method name which was compared, 46 00:02:28,660 --> 00:02:30,130 it also figured that out right? 47 00:02:30,130 --> 00:02:32,610 It doesn't say anything about the method name being compare or 48 00:02:32,610 --> 00:02:34,350 that it's the comparator interface. 49 00:02:34,350 --> 00:02:36,340 But we can make this even more concise. 50 00:02:36,340 --> 00:02:40,240 So, if you've come across some of these before you were probably wondering what 51 00:02:40,240 --> 00:02:42,840 the heck was going on, I know I did the first time I saw them. 52 00:02:42,840 --> 00:02:45,747 So let's go ahead and let's dupe this method into a short form. 53 00:02:48,630 --> 00:02:54,460 So, we'll say public static in ShortForm this time. 54 00:02:56,430 --> 00:02:59,150 Okay, so the compiler can actually figure stuff out 55 00:02:59,150 --> 00:03:01,350 well enough that you don't even need to declare types. 56 00:03:01,350 --> 00:03:02,710 So, let's get rid of those, right. 57 00:03:02,710 --> 00:03:05,750 So, it knows that it's expecting two books. 58 00:03:05,750 --> 00:03:06,430 So, let's get rid of that. 59 00:03:07,640 --> 00:03:08,550 Okay, cool. 60 00:03:08,550 --> 00:03:12,890 Now, if you have a single line method, you actually don't need the curly braces. 61 00:03:12,890 --> 00:03:14,189 Let's go ahead and get rid of those. 62 00:03:17,732 --> 00:03:20,170 You put this all up on the next line. 63 00:03:20,170 --> 00:03:23,260 When you only have a one-liner you actually don't even need the return 64 00:03:23,260 --> 00:03:26,180 keyword, it knows what's expected to be returned. 65 00:03:26,180 --> 00:03:27,120 So, let's get rid of the keyword. 66 00:03:28,200 --> 00:03:31,200 Return. And let's bring this up a line, cool. 67 00:03:31,200 --> 00:03:34,080 So that one-liner is called an expression type body. 68 00:03:34,080 --> 00:03:37,160 This creates a function that accepts two books, and 69 00:03:37,160 --> 00:03:40,010 returns the value from this statement here. 70 00:03:40,010 --> 00:03:45,390 Let's make sure that we change this to say usingLambdasInShortForm. 71 00:03:47,891 --> 00:03:48,840 All right, so let's run it. 72 00:03:49,860 --> 00:03:51,320 Cool, it still works. 73 00:03:51,320 --> 00:03:53,310 Pretty succinct, right? 74 00:03:53,310 --> 00:03:54,340 Now, like I mentioned, 75 00:03:54,340 --> 00:03:57,930 these opened up a new style of programming known as functional programming. 76 00:03:57,930 --> 00:04:02,150 We're not gonna delve to deep here, we'll cover that fully later in another course. 77 00:04:02,150 --> 00:04:04,330 But, let's get a quick little sneak peak. 78 00:04:04,330 --> 00:04:08,240 So, the collections framework really benefits from functional programming. 79 00:04:08,240 --> 00:04:12,460 And one super cool thing that got added is the forEach method on collections. 80 00:04:12,460 --> 00:04:15,670 It takes a consumer, which is a newly introduced functional interface, 81 00:04:15,670 --> 00:04:18,280 remember that's the new name for a single abstract methods. 82 00:04:18,280 --> 00:04:23,440 Now, a consumer expects a parameter that takes one of its container's types. 83 00:04:23,440 --> 00:04:24,920 So, let's replace this forEach loop. 84 00:04:28,630 --> 00:04:34,740 So, we'll do books.forEach, which is the new method. 85 00:04:34,740 --> 00:04:37,010 And, it takes a consumer, so the consumer is book. 86 00:04:38,050 --> 00:04:43,577 And for each one of those books, we're just gonna print out. 87 00:04:43,577 --> 00:04:46,006 Cool, so now if I run it, still working. 88 00:04:46,006 --> 00:04:49,350 All right, are you ready for even more succinctness? 89 00:04:49,350 --> 00:04:52,497 When you just have one parameter, you don't even need the parentheses, so 90 00:04:52,497 --> 00:04:54,480 let's get rid of this cuz there's only one. 91 00:04:54,480 --> 00:04:57,200 So it's saying for each book, run this. 92 00:04:58,480 --> 00:05:02,600 So since we're here, I wanna show you one more thing that you might come across that 93 00:05:02,600 --> 00:05:06,150 might look a little bit foreign, and that's called method references. 94 00:05:06,150 --> 00:05:07,200 Let's take a look in the next video.