Old School7:34 with Craig Dennis
In this video we will review Single Abstract Methods or SAMs and how things were done prior to Java 8.
[SOUND] Hello, I’m Craig and I’m a developer. 0:00 In this workshop, we’ll take a look at a relatively new language construct for 0:07 Java, lambdas. 0:12 Now in general, lambdas are not a new concept and 0:13 they’re available in many other programming languages. 0:15 You may have heard them referred to as anonymous functions. 0:18 They provide the building blocks to a more declarative style of coding 0:22 known as functional programming. 0:25 Java 8, which was released in March of 2014, 0:27 introduced several new changes to the Java syntax. 0:30 The introduction of lambdas on the surface is basically just syntactic sugar or 0:33 a better way to say, the same thing in code. 0:38 Its sugar is replacing what was already available to us through creating inline 0:40 anonymous classes, but aside from being much more legible and 0:45 concise the introduction means more to the language. 0:49 It promotes the concept of functions as a first class citizen and 0:52 enters Java into he functional programming arena. 0:56 In this workshop, we'll reacquaint ourselves with the old way of doing 0:59 things and then explore the shiny new more succinct declarative function way. 1:03 Lambdas may look a little strange when you run across them for the first time, so 1:08 I wanted to make sure that you were able to read them when you bump into them. 1:12 Now more and more, as code bases upgrade to Java 8, 1:15 lambda expressions are quickly becoming the de facto way to accomplish tasks. 1:18 Let's go get cozy with lambdas. 1:23 So first, let's get refreshed. 1:26 Now I've gone ahead and 1:28 I built an IntelliJ project that you can download, it's in the teacher's notes. 1:29 So let's open it up and take a peek, then we click open. 1:33 And in the download folder here, I unzip this lambda and 1:36 there's this thing here that's called Lambda. 1:39 So, I'm gonna click Choose on that and it's gonna open things up. 1:41 A common question that I get on the forum is asking what Java books I recommend? 1:46 So, I thought we'd kill two birds with one stone. 1:50 This tiny little project here is gonna print out some Java books that I've 1:52 been reading. 1:55 So first thing's first, I've created the book class for us already, 1:57 you can get to it over here. 2:00 It's a pretty basic class and it could definitely be improved on, but 2:02 it'll give us what we need right now. 2:05 So the way that you do it is when you create a book to the constructor, 2:06 you pass the title, the author and a publication date of an integer. 2:09 So like the year date there and then I also made a little two-string method here, 2:12 so we could kinda take a look at that. 2:16 And then I have another class here called Books and books has a static method on it 2:17 that returns a list of books and it's called all. 2:22 Now static methods are nice, because they don't require us to create an instance. 2:25 We can just kind of access that method right off the class. 2:29 So basically, what this is doing is giving us a list of book objects to play with. 2:32 We're gonna use them to loop and sort through some things. 2:37 So let's review the anonymous inline class version of how we solve this 2:40 problem before Java 8. 2:45 How did we sort things before Java 8? 2:46 So we explored the style just previously in the Java data structures course. 2:49 So let's come over here in main and let's make a new static method here. 2:54 It's not gonna return anything, we're just gonna print out to the screen and 2:58 we're gonna call it usingAnonymousInlineClass. 3:05 So first things first, let's get a list of our books and 3:09 we're gonna use the Books.all. 3:13 Now it's saying, it doesn't know what list. 3:15 See how it's red there and it's suggesting that we import it and it says, 3:17 do you mean this? 3:20 So, I'm gonna press what it suggests there. 3:21 It says, option and then Enter and choose Java.util.Llist. 3:23 There we go and I don't have the name of it, so 3:26 we're gonna call it books as the name. 3:28 There's a static method off of the collections object called sort. 3:31 Let's go ahead and take care of the collections that we were talking about. 3:36 And it takes two parameters, it takes books and 3:39 then it takes a comparator class and 3:44 a comparator is an interface and it's a generic interface. 3:47 Let's go ahead and import that too. 3:54 And you'll see that this is telling me that I've got something wrong with it. 3:57 So let's mouse over here and see what it says. 4:02 It says, the anonymous derived from comparator must either be declared 4:03 abstract or implement the abstract method compare of type to type. 4:08 This is exactly what we had done before, 4:12 I just wanted to show you this over here in IntelliJ. 4:14 Let's go ahead and let this, we'll say, implement methods. 4:16 So we'll write out the method that we did. 4:19 Now before we did this by hand, but 4:21 we're gonna do this compare method here is the one that it actually needs. 4:22 Now we're making a brand new anonymous class from this interface and 4:27 we're overwriting the one abstract method that needed to be completed and 4:32 that's this compare method. 4:36 And let's name these things o1 o2. 4:38 Let's call it b2, so we know that they're books. 4:41 And so we're gonna return the title and we're gonna compare it to, 4:44 cuz remember, these comparators were is that returns negative 4:50 one if it's less, zero if it's equal, one one if it isn't. 4:56 And strings have a method on them called compareTo. 5:01 And since we're gonna just sort by the title of the book, 5:05 what we're gonna do is here is we're gonna get the title and 5:08 we're gonna use the compareTo on string to compare it to the other string. 5:11 So that's how the sorting works through the process here. 5:14 So now we've pushed the books in here. 5:19 The books should now be sorted, so let's go ahead and let's just print out and 5:21 make sure that the books are sorted. 5:24 We’ll look through all the books for each book in books and 5:29 we’ll print out the book. 5:33 Cool. 5:34 Let’s go ahead and call our new function here. 5:36 We’ll say, usingAnonymousInLineClass and now I’m gonna run that. 5:38 Awesome. So here it is and they are sorted and 5:46 you'll remember from before, this books, they're not sorted here. 5:48 But because we ran it through that sort function here, they are clean code, 5:52 effective Java, functional Java. 5:55 Prior to Java 8, this is how you did things. 5:56 Now let's take a look at that again, this is how you did things. 5:59 It’s pretty ugly. 6:03 I mean, that’s a lot of lines of code just to get this one liner here out. 6:05 Now the main reason for all this code is because until very recently, 6:10 there was no way to have a method outside of a class. 6:14 So developers dealt with this hindrance by creating an interface, like this one here. 6:18 This comparator, this is an interface or an abstract class and 6:22 it just had a single method. 6:26 Like this one here. 6:28 Compare that was required to be implemented. 6:29 Now this pattern became so 6:32 heavily used that it is known as a single abstract method or SAM for short. 6:34 S-A-M. 6:39 Now while the acronym is short, it's implementation is not. 6:41 It kinda reminds me of that George Carlin joke, 6:44 how come abbreviate is such a long word? 6:46 A lot of event-driven code use SAMs to handle the event 6:49 without creating a separate class. 6:52 So you'll see this solution all over the place and 6:55 it's definitely one of the reasons that Java gets a lot of flack. 6:57 So Java developers asked for a quicker,and more succinct way to add dynamic code and 7:01 the Java community process members listened to the complaints and 7:05 they made a change. 7:09 If you're interested in how this process works, please check the teachers notes. 7:10 So, a method outside of a class is called a function and they're now part of Java. 7:14 When you have an unnamed, anonymous function, it's called a lamba. 7:20 Lamba's can be used anywhere that SAMs were used before. 7:25 In fact, their now called functional interfaces. 7:27 Let's write one up, right after this quick break. 7:31
You need to sign up for Treehouse in order to download course files.Sign up