1 00:00:00,025 --> 00:00:07,730 [SOUND] Hello, I’m Craig and I’m a developer. 2 00:00:07,730 --> 00:00:12,116 In this workshop, we’ll take a look at a relatively new language construct for 3 00:00:12,116 --> 00:00:13,105 Java, lambdas. 4 00:00:13,105 --> 00:00:15,497 Now in general, lambdas are not a new concept and 5 00:00:15,497 --> 00:00:18,424 they’re available in many other programming languages. 6 00:00:18,424 --> 00:00:22,170 You may have heard them referred to as anonymous functions. 7 00:00:22,170 --> 00:00:25,440 They provide the building blocks to a more declarative style of coding 8 00:00:25,440 --> 00:00:26,620 known as functional programming. 9 00:00:27,900 --> 00:00:30,270 Java 8, which was released in March of 2014, 10 00:00:30,270 --> 00:00:33,670 introduced several new changes to the Java syntax. 11 00:00:33,670 --> 00:00:38,040 The introduction of lambdas on the surface is basically just syntactic sugar or 12 00:00:38,040 --> 00:00:40,550 a better way to say, the same thing in code. 13 00:00:40,550 --> 00:00:45,392 Its sugar is replacing what was already available to us through creating inline 14 00:00:45,392 --> 00:00:49,222 anonymous classes, but aside from being much more legible and 15 00:00:49,222 --> 00:00:52,559 concise the introduction means more to the language. 16 00:00:52,559 --> 00:00:56,635 It promotes the concept of functions as a first class citizen and 17 00:00:56,635 --> 00:00:59,966 enters Java into the functional programming arena. 18 00:00:59,966 --> 00:01:03,654 In this workshop, we'll reacquaint ourselves with the old way of doing 19 00:01:03,654 --> 00:01:08,680 things and then explore the shiny new more succinct declarative function way. 20 00:01:08,680 --> 00:01:12,164 Lambdas may look a little strange when you run across them for the first time, so 21 00:01:12,164 --> 00:01:15,442 I wanted to make sure that you were able to read them when you bump into them. 22 00:01:15,442 --> 00:01:18,244 Now more and more, as code bases upgrade to Java 8, 23 00:01:18,244 --> 00:01:22,530 lambda expressions are quickly becoming the de facto way to accomplish tasks. 24 00:01:23,730 --> 00:01:24,810 Let's go get cozy with lambdas. 25 00:01:26,420 --> 00:01:28,240 So first, let's get refreshed. 26 00:01:28,240 --> 00:01:29,230 Now I've gone ahead and 27 00:01:29,230 --> 00:01:33,390 I built an IntelliJ project that you can download, it's in the teacher's notes. 28 00:01:33,390 --> 00:01:36,925 So let's open it up and take a peek, then we click open. 29 00:01:36,925 --> 00:01:39,578 And in the download folder here, I unzip this lambda and 30 00:01:39,578 --> 00:01:41,962 there's this thing here that's called Lambda. 31 00:01:41,962 --> 00:01:44,850 So, I'm gonna click Choose on that and it's gonna open things up. 32 00:01:46,010 --> 00:01:50,280 A common question that I get on the forum is asking what Java books I recommend? 33 00:01:50,280 --> 00:01:52,566 So, I thought we'd kill two birds with one stone. 34 00:01:52,566 --> 00:01:55,728 This tiny little project here is gonna print out some Java books that I've 35 00:01:55,728 --> 00:01:57,000 been reading. 36 00:01:57,000 --> 00:02:00,380 So first things first, I've created the book class for us already, 37 00:02:00,380 --> 00:02:01,400 you can get to it over here. 38 00:02:02,540 --> 00:02:05,050 It's a pretty basic class and it could definitely be improved on, but 39 00:02:05,050 --> 00:02:06,419 it'll give us what we need right now. 40 00:02:06,419 --> 00:02:09,341 So the way that you do it is when you create a book to the constructor, 41 00:02:09,341 --> 00:02:12,340 you pass the title, the author and a publication date of an integer. 42 00:02:12,340 --> 00:02:16,149 So like the year date there and then I also made a little toString method here, 43 00:02:16,149 --> 00:02:17,896 so we could kinda take a look at that. 44 00:02:17,896 --> 00:02:22,452 And then I have another class here called Books and books has a static method on it 45 00:02:22,452 --> 00:02:25,420 that returns a list of books and it's called all. 46 00:02:25,420 --> 00:02:29,783 Now static methods are nice, because they don't require us to create an instance. 47 00:02:29,783 --> 00:02:32,990 We can just kind of access that method right off the class. 48 00:02:32,990 --> 00:02:37,898 So basically, what this is doing is giving us a list of book objects to play with. 49 00:02:37,898 --> 00:02:40,870 We're gonna use them to loop and sort through some things. 50 00:02:40,870 --> 00:02:45,401 So let's review the anonymous inline class version of how we solve this 51 00:02:45,401 --> 00:02:46,994 problem before Java 8. 52 00:02:46,994 --> 00:02:49,494 How did we sort things before Java 8? 53 00:02:49,494 --> 00:02:54,290 So we explored the style just previously in the Java data structures course. 54 00:02:54,290 --> 00:02:58,690 So let's come over here in main and let's make a new static method here. 55 00:02:58,690 --> 00:03:05,199 It's not gonna return anything, we're just gonna print out to the screen and 56 00:03:05,199 --> 00:03:09,560 we're gonna call it usingAnonymousInlineClass. 57 00:03:09,560 --> 00:03:13,461 So first things first, let's get a list of our books and 58 00:03:13,461 --> 00:03:15,869 we're gonna use the Books.all. 59 00:03:15,869 --> 00:03:17,280 Now it's saying, it doesn't know what list. 60 00:03:17,280 --> 00:03:20,399 See how it's red there and it's suggesting that we import it and it says, 61 00:03:20,399 --> 00:03:21,161 do you mean this? 62 00:03:21,161 --> 00:03:23,260 So, I'm gonna press what it suggests there. 63 00:03:23,260 --> 00:03:26,243 It says, option and then Enter and choose Java.util.List. 64 00:03:26,243 --> 00:03:28,371 There we go and I don't have the name of it, so 65 00:03:28,371 --> 00:03:31,170 we're gonna call it books as the name. 66 00:03:31,170 --> 00:03:34,460 There's a static method off of the collections object called sort. 67 00:03:36,440 --> 00:03:39,334 Let's go ahead and take care of the collections that we were talking about. 68 00:03:39,334 --> 00:03:44,107 And it takes two parameters, it takes books and 69 00:03:44,107 --> 00:03:47,687 then it takes a comparator class and 70 00:03:47,687 --> 00:03:54,400 a comparator is an interface and it's a generic interface. 71 00:03:54,400 --> 00:03:57,181 Let's go ahead and import that too. 72 00:03:57,181 --> 00:04:02,040 And you'll see that this is telling me that I've got something wrong with it. 73 00:04:02,040 --> 00:04:03,725 So let's mouse over here and see what it says. 74 00:04:03,725 --> 00:04:08,159 It says, the anonymous derived from comparator must either be declared 75 00:04:08,159 --> 00:04:12,900 abstract or implement the abstract method compare of type to type. 76 00:04:12,900 --> 00:04:14,454 This is exactly what we had done before, 77 00:04:14,454 --> 00:04:16,476 I just wanted to show you this over here in IntelliJ. 78 00:04:16,476 --> 00:04:19,844 Let's go ahead and let this, we'll say, implement methods. 79 00:04:19,844 --> 00:04:21,461 So we'll write out the method that we did. 80 00:04:21,461 --> 00:04:22,698 Now before we did this by hand, but 81 00:04:22,698 --> 00:04:25,400 we're gonna do this compare method here is the one that it actually needs. 82 00:04:27,080 --> 00:04:32,390 Now we're making a brand new anonymous class from this interface and 83 00:04:32,390 --> 00:04:36,650 we're overwriting the one abstract method that needed to be completed and 84 00:04:36,650 --> 00:04:38,650 that's this compare method. 85 00:04:38,650 --> 00:04:41,941 And let's name these things o1 o2. 86 00:04:41,941 --> 00:04:44,990 Let's call it b2, so we know that they're books. 87 00:04:44,990 --> 00:04:53,861 And so we're gonna return the title and we're gonna compare it to, 88 00:04:53,861 --> 00:04:58,423 cuz remember, these comparators were is that returns negative 89 00:04:58,423 --> 00:05:01,779 one if it's less, zero if it's equal, one one if it isn't. 90 00:05:01,779 --> 00:05:05,780 And strings have a method on them called compareTo. 91 00:05:05,780 --> 00:05:08,359 And since we're gonna just sort by the title of the book, 92 00:05:08,359 --> 00:05:11,046 what we're gonna do is here is we're gonna get the title and 93 00:05:11,046 --> 00:05:14,465 we're gonna use the compareTo on string to compare it to the other string. 94 00:05:14,465 --> 00:05:19,580 So that's how the sorting works through the process here. 95 00:05:19,580 --> 00:05:21,698 So now we've pushed the books in here. 96 00:05:21,698 --> 00:05:24,622 The books should now be sorted, so let's go ahead and let's just print out and 97 00:05:24,622 --> 00:05:26,010 make sure that the books are sorted. 98 00:05:29,303 --> 00:05:33,064 We’ll look through all the books for each book in books and 99 00:05:33,064 --> 00:05:34,845 we’ll print out the book. 100 00:05:34,845 --> 00:05:36,832 Cool. 101 00:05:36,832 --> 00:05:38,828 Let’s go ahead and call our new function here. 102 00:05:38,828 --> 00:05:43,465 We’ll say, usingAnonymousInLineClass and now I’m gonna run that. 103 00:05:46,490 --> 00:05:48,468 Awesome. So here it is and they are sorted and 104 00:05:48,468 --> 00:05:52,230 you'll remember from before, this books, they're not sorted here. 105 00:05:52,230 --> 00:05:55,206 But because we ran it through that sort function here, they are clean code, 106 00:05:55,206 --> 00:05:56,657 effective Java, functional Java. 107 00:05:56,657 --> 00:05:59,665 Prior to Java 8, this is how you did things. 108 00:05:59,665 --> 00:06:03,924 Now let's take a look at that again, this is how you did things. 109 00:06:03,924 --> 00:06:05,590 It’s pretty ugly. 110 00:06:05,590 --> 00:06:10,535 I mean, that’s a lot of lines of code just to get this one liner here out. 111 00:06:10,535 --> 00:06:14,464 Now the main reason for all this code is because until very recently, 112 00:06:14,464 --> 00:06:18,160 there was no way to have a method outside of a class. 113 00:06:18,160 --> 00:06:22,643 So developers dealt with this hindrance by creating an interface, like this one here. 114 00:06:22,643 --> 00:06:26,553 This comparator, this is an interface or an abstract class and 115 00:06:26,553 --> 00:06:28,420 it just had a single method. 116 00:06:28,420 --> 00:06:29,097 Like this one here. 117 00:06:29,097 --> 00:06:31,660 Compare that was required to be implemented. 118 00:06:32,920 --> 00:06:34,770 Now this pattern became so 119 00:06:34,770 --> 00:06:39,995 heavily used that it is known as a single abstract method or SAM for short. 120 00:06:39,995 --> 00:06:41,240 S-A-M. 121 00:06:41,240 --> 00:06:44,144 Now while the acronym is short, its implementation is not. 122 00:06:44,144 --> 00:06:46,830 It kinda reminds me of that George Carlin joke, 123 00:06:46,830 --> 00:06:49,122 how come abbreviate is such a long word? 124 00:06:49,122 --> 00:06:52,448 A lot of event-driven code use SAMs to handle the event 125 00:06:52,448 --> 00:06:55,040 without creating a separate class. 126 00:06:55,040 --> 00:06:57,728 So you'll see this solution all over the place and 127 00:06:57,728 --> 00:07:01,256 it's definitely one of the reasons that Java gets a lot of flack. 128 00:07:01,256 --> 00:07:05,883 So Java developers asked for a quicker and more succinct way to add dynamic code, and 129 00:07:05,883 --> 00:07:09,532 the Java community process members listened to the complaints and 130 00:07:09,532 --> 00:07:10,652 they made a change. 131 00:07:10,652 --> 00:07:14,640 If you're interested in how this process works, please check the teacher's notes. 132 00:07:14,640 --> 00:07:20,580 So, a method outside of a class is called a function and they're now part of Java. 133 00:07:20,580 --> 00:07:25,090 When you have an unnamed, anonymous function, it's called a lamba. 134 00:07:25,090 --> 00:07:27,826 Lambas can be used anywhere that SAMs were used before. 135 00:07:27,826 --> 00:07:31,480 In fact, they're now called functional interfaces. 136 00:07:31,480 --> 00:07:33,460 Let's write one up, right after this quick break.