Published on Mar 17, 2014
Save 38% on Java 8 in Action Lambdas, Streams and functional-style programming. Enter vturma01 in the Promotional Code box when you check out at manning.com/urma/.
Java developers are hearing a lot of buzz about functional-style programming, especially with Java 8's addition of lambda expressions. This video introduces method references and lambda expressions, the mechanisms Java 8 provides to pass a behavior as an argument to a method. This video is presented by Raoul-Gabriel Urma, Mario Fusco and Alan Mycroft based on Java 8 in Action: Lambdas, Streams, and functional-style programming, available at manning.com/urma.
0:15 First-class functions in Java
In this first video, you'll see how to write more flexible code by regarding methods as first class-values, and using lambda expressions to concisely represent behaviors. Let's start by looking at the idea of functions as first-class values.
2:00 Methods as first class-citizens
Programs embracing the idea of methods as values are said to be functional-style programs—which essentially just means "programs that pass functions around as first-class values." The designers of Java 8 decided to allow methods to be values.
The first new Java 8 feature we introduce is called method references. One of the most significant additions is lambdas, or anonymous functions.
3:40 Passing code example
Suppose you have a farm inventory application with a class Apple declaring a method getColor, and an inventory that's a list of all Apple objects. It's reasonable to think you that might want to select all the green apples and return them in a list. You'd typically call the code to select just the green apples a filter. In this case, you'd write your filter as a method called something like filterGreenApples: Let's look at an example of what this method might look like. Java 8 makes it possible to pass the code of the whole condition as an argument, thus avoiding code duplication of the filter method. Let's look at how you can write this, and step through some of the important features of this code.To use this you simply call filterApples with parameter inventory and a method reference to either isGreenApple or isHeavyApple. The key idea to take away for now is that you can pass a method around in Java 8!
6:22 Introducing Java 8 Lambdas
While passing code is a way to give new behaviors as an argument to a method, it's currently quite verbose in Java. Lambdas, also known as anonymous functions, help eliminate the verbosity of declaring multiple concrete classes for an interface that's needed only once. In our previous apple-sorting example, we showed you that passing methods is useful. We also reminded you that it's really annoying to write separate definitions for the methods isHeavyApple, isGreenApple, and any other required condition, especially when they're likely to be used only once or twice. Fortunately, Java 8 has solved this too. Enter the lambda expression. The new lambda notation lets you represent your method differently. Lambda expressions follow a straightforward syntax, shown here. Now let's look at a few specific examples based on our earlier farm inventory code. As you can see, using this new approach, you don't need to write a method definition that's used only once; the code is crisper and clearer because you don't need to search to find the code you're passing. A lambda expression can be understood as a kind of anonymous function that can be passed around: it doesn't have a name, but it has a list of parameters, a body, a return type, and also possibly a list of exceptions that can be thrown. Lambdas fix the verbosity problem because they let you pass code in a concise way. Thus, you can cope with changing requirements by using a behavior, represented by a lambda, as a parameter to a method. Once you get used to them, you'll find lambdas to be pretty powerful.