- Interface Default and Static Methods
- Lambda Expressions and Functional Interfaces
Lambda Expressions and Functional Interfaces
A lambda expression (lambda) is a short-form replacement for an anonymous class. Lambdas simplify the use of interfaces that declare single abstract methods. Such interfaces are known as functional interfaces.
Lambdas let you treat code as data. You can use lambdas to pass code to methods for subsequent execution. Listing 4 contrasts the traditional (cumbersome) approach to passing code via an anonymous class with the cleaner approach to passing code via a lambda.
Listing 4 LambdaDemo.java (Version 1).
import java.awt.EventQueue; public class LambdaDemo { public static void main(String[] args) { Runnable r = new Runnable() { @Override public void run() { System.out.println("Running"); } }; EventQueue.invokeLater(r); EventQueue.invokeLater(() -> System.out.println("Running")); } }
Listing 4's main() method first creates a Runnable instance the old-fashioned way, by instantiating an anonymous class that implements the Runnable interface. This instance is then passed to java.awt.EventQueue's static void invokeLater(Runnable r) method.
The traditional approach to creating and passing a block of code to execute is cumbersome and results in a loss of clarity. In contrast, all of this code can be replaced by EventQueue.invokeLater(() -> System.out.println("Running"));, which is much more compact.
The () -> System.out.println("Running") expression is an example of a lambda. It describes a functional interface whose single abstract method doesn't have a parameter list (hence the ()), and it executes System.out.println("Running")); when invoked.
A lambda doesn't have an explicit interface type. Instead, the compiler infers from the context which functional interface to instantiate. In Listing 4, it knows that invokeLater() requires a Runnable parameter, and it instantiates an implementation of this functional interface.
Compile Listing 4 as follows:
javac LambdaDemo.java
Run the LambdaDemo application as follows:
java LambdaDemo
You should observe the following output:
Running Running
If you're not convinced that lambdas are an important contribution to the Java language, I recommend that you read Mario Fusco's blog post "Why We Need Lambda Expressions in Java - Part 1." Fusco makes the case for using lambdas in the context of the internal iteration of a collection, optimizing this iteration by processing items in parallel. After all, lambdas were designed to support a multicore processor architecture, which relies on software that provides parallelism, which in turn improves performance and reduces an overall task's completion time.
Lambda Syntax and Target Types
A lambda adheres to the following syntax:
( formal-parameter-list ) -> { expression-or-statements }
formal-parameter-list is a comma-separated list of formal parameters that match the formal parameters of the functional interface's single abstract method. If you don't specify their types, the compiler infers the parameter types from the context. Consider the following examples:
(int x, int y) (x, y)
If formal-parameter-list consists of a single parameter, the parentheses can be omitted. However, if formal-parameter-list is empty (there are no parameters), the empty parentheses must be specified. Here are a couple of examples:
x ()
Following the -> operator is expression-or-statements, which is an expression or a block of statements (the lambda body). If you place the expression between the opening and closing braces ({ }), you must use a return statement to return its value; otherwise, don't use return. Consider these examples:
(int x, int y) -> x+y (x, y) -> { return x+y; } (int x, int y) -> { System.out.println(x+y); return x+y; }
Listing 5 puts the previous theory to practical use by showing how you might use lambdas in an application that converts between different kinds of units.
Listing 5 LambdaDemo.java (Version 2).
@FunctionalInterface interface Converter { double convert(double input); } public class LambdaDemo { public static void main(String[] args) { // Convert Fahrenheit to Celsius System.out.println(convert(input -> (input-32)*5.0/9.0, 98.6)); // Convert Kilometers to Miles System.out.println(convert(input -> input/1.609344, 8)); } static double convert(Converter converter, double input) { return converter.convert(input); } }
Listing 5 introduces a functional interface named Converter that declares a double convert(double input) method for converting some input value to an output value. It also introduces a LambdaDemo class whose main() method demonstrates this functional interface.
The Converter functional interface is demonstrated in the context of a static double convert(Converter converter, double input) method. The lambda makes it easy to pass code as data to this method, which is ultimately received as a Converter instance.
Compile Listing 5 (javac LambdaDemo.java) and run the application (java LambdaDemo). You should observe the following output:
37.0 4.970969537898672
Associated with a lambda is an implicit target type (the type of object to which a lambda is bound). Because the target type must be a functional interface and is inferred from the context, lambdas can appear in only the following contexts:
- Variable declaration
- Assignment
- Return statement
- Array initializer
- Method or constructor arguments
- Lambda body
- Ternary conditional expression
- Cast expression
I've created another version of LambdaDemo that demonstrates these contexts. Check out Listing 6.
Listing 6 LambdaDemo.java (Version 3).
import java.awt.EventQueue; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.concurrent.Callable; public class LambdaDemo { public static void main(String[] args) throws Exception { // Target type: variable declaration Runnable r = () -> { for (int i = 0; i < 5; i++) System.out.println(i); }; r.run(); // Target type: assignment r = () -> System.out.println("running"); r.run(); // Target type: return statement (in getRunnable()) EventQueue.invokeLater(getRunnable("I'm running")); // Target type: array initializer Callable<String>[] callables = new Callable[] { () -> "First callable", () -> "Second callable", () -> "Third callable" }; System.out.println(callables[1].call()); // Target type: method or constructor arguments EventQueue.invokeLater(() -> System.out.println("invoked later")); // Target type: lambda body (a nested lambda) Callable<Runnable> callable = () -> () -> System.out.println("callable says hello"); callable.call().run(); // Target type: ternary conditional expression boolean ascendingSort = false; Comparator<String> cmp; cmp = (ascendingSort) ? (s1, s2) -> s1.compareTo(s2) : (s1, s2) -> s2.compareTo(s1); List<String> planets = Arrays.asList("Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"); Collections.sort(planets, cmp); for (String planet: planets) System.out.println(planet); // Target type: cast expression String user = AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty("user.name")); System.out.println(user); } static Runnable getRunnable(String s) { return () -> System.out.println(s); } }
The final example demonstrates the occasional need for a cast, which happens to be (PrivilegedAction<String>). The cast is present to handle ambiguity, which arises from the java.security.AccessController class declaring the following methods:
static <T> T doPrivileged(PrivilegedAction<T> action) static <T> T doPrivileged(PrivilegedExceptionAction<T> action)
Each of the java.security.PrivilegedAction and java.security.PrivilegedExceptionAction functional interfaces declares the same T run() method, and the compiler isn't sure which interface is the target type. Without the cast, the compiler would report an error.
Compile Listing 6 (javac LambdaDemo.java) and run the application (java LambdaDemo). You should observe the following output (notice that the sorting of planet names is in descending alphabetical order because I assigned false to ascendingSort):
0 1 2 3 4 running Second callable I'm running invoked later callable says hello Venus Uranus Saturn Neptune Mercury Mars Jupiter Earth Owner
Scopes, Local Variables, this, super, and Exceptions
A lambda doesn't define a new scope. Instead, its scope is the same as the enclosing scope. For example, if a lambda body declares a local variable with the same name as a variable in the enclosing scope, the compiler will report an error.
Whether declared in the lambda body or in the enclosing scope, a local variable must be initialized before being used. Otherwise, the compiler reports an error.
A local variable or parameter that is declared outside of a lambda body and referenced from the body is required to be declared final or to be effectively final (so that it hangs around) because the lambda may execute long after the outer code has returned and non-final/non-effectively final variables no longer exist. An effectively final variable isn't modified after being initialized. If a variable is modified, the compiler reports an error.
Listing 7 demonstrates these local variable-related problems.
Listing 7 LambdaDemo.java (Version 4).
@FunctionalInterface interface Justifier { String justify(String text, int width); } public class LambdaDemo { public static void main(String[] args) { String title = "Hamlet"; //StringBuilder sb; Justifier justifier = (text, width) -> { StringBuilder sb = new StringBuilder(); int count = 0; //title = ""; for (int i = 0; i < text.length(); i++) { char ch = text.charAt(i); if (++count == width) { sb.append("\r\n"); count = 0; } sb.append(ch); } return sb.toString(); }; String text = "This above all: to thine own self be true,"+ "And it must follow, as the night the day,"+ "Thou canst not then be false to any man."; System.out.println(justify(justifier, text, 10)); } static String justify(Justifier justifier, String text, int width) { return justifier.justify(text, width); } }
To observe the problem where a lambda body declares a local variable with the same name as a variable in the enclosing scope, uncomment StringBuilder sb; and recompile. You should observe an error message about sb already being defined in main().
To observe the problem where a local variable isn't initialized before being used, uncomment StringBuilder sb; comment out StringBuilder sb = new StringBuilder();, and recompile. You should observe an error message about sb not being initialized.
Finally, to observe the problem of attempting to modify an effectively final variable, uncomment title = ""; and recompile. You should observe an error message telling you that local variables referenced from a lambda must be final or effectively final.
Compile Listing 7 (javac LambdaDemo.java) and (assuming successful compilation) run the LambdaDemo application (java LambdaDemo). You should observe the following output (on a Windows platform):
This abov e all: to thine own self be tr ue,And it must follo w, as the night the day,Thou c anst not t hen be fal se to any man.
There are two more items to consider. First, any this or super reference that appears in a lambda body is the same as in the enclosing scope because a lambda doesn't introduce a new scope, which is not the case with anonymous classes. Consider Listing 8.
Listing 8: LambdaDemo.java (Version 5)
public class LambdaDemo { public static void main(String[] args) { new LambdaDemo().doSomething(); } public void doSomething() { System.out.println(this); Runnable r = new Runnable() { @Override public void run() { System.out.println(this); } }; new Thread(r).start(); new Thread(() -> System.out.println(this)).start(); } }
Listing 8's main() method instantiates LambdaDemo and invokes the instance's doSomething() method. This method outputs the object's this reference, instantiates an anonymous class that implements Runnable, creates a java.lang.Thread object that executes this Runnable when its thread is started, and creates a second Thread object whose thread executes a lambda when started.
Compile Listing 8 (javac LambdaDemo.java) and run the LambdaDemo application (java LambdaDemo). You should observe output that's similar to the following output:
LambdaDemo@15db9742 LambdaDemo$1@293ed848 LambdaDemo@15db9742
The first output line shows the LambdaObject this reference, the second output line shows a different this reference in the new Runnable scope, and the third output line shows the this reference in a lambda context. The third output line matches the first output line because the lambda's scope is nested inside the doSomething() method; this has the same meaning throughout this method.
Second, a lambda body must not throw more exceptions than are specified in the throws clause of the functional interface method. If a lambda body throws an exception, the throws clause of the functional interface method must declare the same exception type or its supertype.
Conclusion
Interface default/static methods and lambda expressions/functional interfaces are not the only new language features offered by Java 8. In Part 2 of this series (coming April 22, 2014), I cover predefined functional interfaces, method references, enhanced generic type inference, and type annotations.