Sneak Peak: Method References3:35 with Craig Dennis
Java 8 also introduces method references and allows them to be used anywhere a Functional Interface AKA Single Abstract Method is expected.
Up until now we've been using anonymous functions. 0:00 But it's also possible to use a thing called method references. 0:03 And we can use those in place of where lambdas are expected. 0:06 The compiler does some crazy smart stuff for 0:10 you, which we'll explore in depth in a different course. 0:12 But I wanted to just give you this version of the code, 0:15 as it's probably the correct way to solve this using functions. 0:17 It may also be a little hard to read if it's new to you. 0:20 So let's dupe this, using lambdas in short form, 0:23 and let's replace this with using method references. 0:29 The comparator actually added a new static method called comparing, 0:35 which expects a function. 0:41 So let's go ahead, let's do this. 0:43 So, Comparator,comparing the static method and it expects a function. 0:44 We could put a lambda here right? 0:51 So we could do this we could say b and b.getTitle. 0:53 So what that does is it says for each thing in here compare using the title. 0:59 So, that's a lot cleaner already using that lambda method. 1:04 But since that style is so common, you can actually do something like this. 1:07 So, instead of writing out the lambda there, 1:12 you can actually use a method reference. 1:15 So, what we're gonna say is the class book 1:17 has a method called getTitle on it, and it should use that. 1:20 Use the getTitle method on each instance to get the value that will be used for 1:25 the comparing command. 1:31 This takes a little bit to get use to, but it's super powerful. 1:32 Now it also works with parameters. 1:36 Check this one out. 1:37 So if we say books for each and we want to print out each one of these, 1:39 we're gonna say system.out. 1:42 And the method that we want to do, cuz it takes a parameter, is println. 1:44 That returns a reference to the println method, and 1:50 for each applies the entry as the parameter to the function. 1:52 Pretty cool, right? 1:57 But it’s definitely gonna take some time to get used to. 1:58 But don’t let that boggle your mind too much. 2:00 We’ll go over that fully in a functional programming course. 2:01 But I just wanted you to see that double colon style of method reference. 2:04 Okay, so let’s take a quick look at the evolution of our code. 2:08 So, here is the SAM method, right? 2:12 It’s very long. 2:14 Okay, so let's expand this. 2:16 And again, so this creates a new anonymous class. 2:18 It's typed. 2:22 We use the name. 2:23 We had to put the override with the method name, and we defined the method. 2:24 And we put in there return type, pretty noisy. 2:29 And then it got cleaner. 2:34 We still had the types. 2:35 And then it got a lot cleaner, right. 2:37 So we removed all the return lines, and now it's onto three lines. 2:39 And if we look down here we're hardly writing any code at all when we get down 2:43 to the method references. 2:46 See how much cleaner things can become? 2:48 So, there you go, I hope you're happy. 2:50 Oh no, no wait, I hope you are merry that you had a little into to lambdas. 2:53 You see what I did there? 2:59 Mary had a little lambda? 3:00 Oh God, you should edit that joke out, it's so bad. 3:02 >> So there we go. 3:07 I'm glad you got a chance to meet and get cozy with lambda expressions. 3:09 You'll definitely encounter them as they're becoming increasingly popular in 3:12 new java code and frame works. 3:16 Now if you want to delve deeper into functional programing, 3:18 please be sure to check out the teachers notes for more resources. 3:21 We are always looking for feedback on this format, and 3:24 how we can make it even better. 3:26 Please let us know what you thought, and also, 3:28 make sure your vote is heard on upcoming content. 3:30 Thanks for hanging out and we'll see you very soon. 3:33
You need to sign up for Treehouse in order to download course files.Sign up