Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.1 Lambdas and Method References

Creating instances of anonymous classes that implement SAM (single abstract method) interfaces is unnecessary while using Java 8. Now you can use lambdas and method references instead. You'll learn how to use this much more concise syntax in this lesson.

2.1 Lambdas and Method References

Hello and welcome back. One of the most popular language features of Java 8 if not, the most popular language feature is support for lambdas and method references. In this lesson you are going to learn how to make the most of this feature. So what is a lambda? You might be thinking, well, before I tell you the answer to that question, you need to know what a functional interface is. A functional interface is just an interface that has a single method. Let's create a functional interface now. So as usual, I'll use the interface keyword, give the interface a name, and add a method to it. This is, by default, a functional interface. If you want to explicitly tell the compiler that this is a functional interface, you can also add the add FunctionalInterface annotation to it. So with this annotation, if I add another method to this interface, you'll see Android Studio complain. Okay, let's delete that now. So in Java 7, if you wanted to create an instance of an anonymous class that implements the interface, you would have used the new keyword and chosen this option to generate all of this code. Now this is a lot of code and most of it is unnecessary. Why? Because all we want to do is add a body to this method. By using a lambda, you can do exactly that. Now you can see that Android Studio is already telling us that there is redundant code that can be replaced with a lambda. So you can just click on this bulb, and select the replace with lambda option. And there you go, all the unnecessary code is gone. What you have left is an anonymous function definition. These empty parentheses mean that our interfaces method doesn't take any arguments. If I modify it such that it takes one argument, say a string, then I like to modify this too, to take exactly one argument. We don't have to specify the type of this argument, because the compiler is able to infer it automatically. Similarly, if you were to change the method signature to return a value, you will have to return something here. To do something non-trivial, let's say we just returned the sum of the ASCII values of all the characters in the string. So a simple loop and the charAt method should suffice. Just so you know, you are free to change the name of the method in the interface without affecting the lambda at all. You might now be wondering how to run this function body you just created. And the answer to that is, the same way you run any objects method. That means you can simply call mli.getSum here and pass a string to it. To be sure that it is working correctly, let's log the result. So I'll quickly create a tag for this class, and then call the log.d method. If we run this activity now, we'll get an ASCII sum. Let's talk about method references now. It is another feature that's new to Java 8 but it is closely related to lambdas. The only difference between the two is a slightly different syntax. A lambda as you can see here doesn't have a name. It is anonymous. So if we want to reuse this code, say for another interface, we will have to write it all over again. That's not a very good thing is it? So what we could do is move this code to a real method which has a name. I'll call it mySumCalculator. Note that the signature of this method matches the signature of the interface's method. And now we can delete all this and simply say this ::mySumCalculator. This is nothing but a method reference. We can now easily reuse the function for multiple instances of the interface. In my opinion this is really a very useful feature. That's all we are gonna do in this lesson. You now know the basics of lambdas and method references. In the next lesson, you're going to see how to use lambdas to simplify event handling in Android apps

Back to the top