- 6.1. Interfaces
- 6.2. Lambda Expressions
- 6.3. Inner Classes
- 6.4. Service Loaders
- 6.5. Proxies
6.2. Lambda Expressions
In the following sections, you will learn how to use lambda expressions for defining blocks of code with a concise syntax, and how to write code that consumes lambda expressions.
6.2.1. Why Lambdas?
A lambda expression is a block of code that you can pass around so it can be executed later, once or multiple times. Before getting into the syntax (or even the curious name), let’s step back and observe where we have used such code blocks in Java.
In Section 6.1.7, you saw how to do work in timed intervals. Put the work into the actionPerformed method of an ActionListener:
class Worker implements ActionListener { public void actionPerformed(ActionEvent event) { // do some work } }
Then, when you want to repeatedly execute this code, you construct an instance of the Worker class. You then submit the instance to a Timer object.
The key point is that the actionPerformed method contains code that you want to execute later.
Or consider sorting with a custom comparator. If you want to sort strings by length instead of the default dictionary order, you can pass a Comparator object to the sort method:
class LengthComparator implements Comparator<String> { public int compare(String first, String second) { return first.length() - second.length(); } } . . . Arrays.sort(strings, new LengthComparator());
The compare method isn't called right away. Instead, the sort method keeps calling the compare method, rearranging the elements if they are out of order, until the array is sorted. You give the sort method a snippet of code needed to compare elements, and that code is integrated into the rest of the sorting logic, which you’d probably not care to reimplement.
Both examples have something in common. A block of code was passed to someone—a timer, or a sort method. That code block was called at some later time.
In early versions of Java, giving someone a block of code was not easy. You couldn’t just pass code blocks around. Java is an object-oriented language, so you had to construct an object belonging to a class that has a method with the desired code.
In other languages, it is possible to work with blocks of code directly. The Java designers have resisted adding this feature for a long time. After all, a great strength of Java is its simplicity and consistency. A language can become an unmaintainable mess if it includes every feature that yields marginally more concise code. However, in those other languages it isn’t just easier to spawn a thread or to register a button click handler; large swaths of their APIs are simpler, more consistent, and more powerful. In Java, one could have written similar APIs taking objects of classes that implement a particular interface, but such APIs would be unpleasant to use.
For some time, the question was not whether to augment Java for functional programming, but how to do it. It took several years of experimentation before a design emerged that is a good fit for Java. In the next section, you will see how you can work with blocks of code in Java.
6.2.2. The Syntax of Lambda Expressions
Consider again the sorting example from the preceding section. We pass code that checks whether one string is shorter than another. We compute
first.length() - second.length()
What are first and second? They are both strings. Java is a strongly typed language, and we must specify that as well:
(String first, String second) -> first.length() - second.length()
You have just seen your first lambda expression. Such an expression is simply a block of code, together with the specification of any variables that must be passed to the code.
Why the name? Many years ago, before there were any computers, the logician Alonzo Church wanted to formalize what it means for a mathematical function to be effectively computable. (Curiously, there are functions that are known to exist, but nobody knows how to compute their values.) He used the Greek letter lambda (?) to mark parameters. Had he known about the Java API, he would have written
?first.?second.first.length() - second.length()
What you have just seen is a simple form of lambda expressions in Java: parameters, the -> arrow, and an expression. If the code carries out a computation that doesn’t fit in a single expression, write it exactly like you would have written a method: enclosed in {} and with explicit return statements. For example,
(String first, String second) -> { if (first.length() < second.length()) return -1; else if (first.length() > second.length()) return 1; else return 0; }
If a lambda expression has no parameters, you still supply empty parentheses, just as with a parameterless method:
() -> { return 1 + (int)(Math.random() * 6); }
If the parameter types of a lambda expression can be inferred, you can omit them. For example,
Comparator<String> comp = (first, second) // same as (String first, String second) -> first.length() - second.length();
Here, the compiler can deduce that first and second must be strings because the lambda expression is assigned to a string comparator. (We will have a closer look at this assignment in the next section.)
If a method has a single parameter with inferred type, you can even omit the parentheses:
ActionListener listener = event -> System.out.println("The time is " + Instant.ofEpochMilli(event.getWhen())); // instead of (event) -> . . . or (ActionEvent event) -> . . .
You never specify the result type of a lambda expression. It is always inferred from context. For example, the expression
(String first, String second) -> first.length() - second.length()
can be used in a context where a result of type int is expected.
Finally, you can use var to denote an inferred type. This isn't common. The syntax was invented for attaching annotations (see Chapter 11):
(@NonNull var first, @NonNull var second) -> first.length() - second.length()
The program in Listing 6.6 shows how to use lambda expressions for a comparator and an action listener.
Listing 6.6 lambda/LambdaTest.java
1 package lambda; 2 3 import java.util.*; 4 5 import javax.swing.*; 6 import javax.swing.Timer; 7 8 /** 9 * This program demonstrates the use of lambda expressions. 10 * @version 1.0 2015-05-12 11 * @author Cay Horstmann 12 */ 13 public class LambdaTest 14 { 15 public static void main(String[] args) 16 { 17 var planets = new String[] { "Mercury", "Venus", "Earth", "Mars", 18 "Jupiter", "Saturn", "Uranus", "Neptune" }; 19 System.out.println(Arrays.toString(planets)); 20 System.out.println("Sorted in dictionary order:"); 21 Arrays.sort(planets); 22 System.out.println(Arrays.toString(planets)); 23 System.out.println("Sorted by length:"); 24 Arrays.sort(planets, (first, second) -> first.length() - second.length()); 25 System.out.println(Arrays.toString(planets)); 26 27 var timer = new Timer(1000, event -> 28 System.out.println("The time is " + new Date())); 29 timer.start(); 30 31 // keep program running until user selects "OK" 32 JOptionPane.showMessageDialog(null, "Quit program?"); 33 System.exit(0); 34 } 35 }
6.2.3. Functional Interfaces
As we discussed, there are many existing interfaces in Java that encapsulate blocks of code, such as ActionListener or Comparator. Lambdas are compatible with these interfaces.
You can supply a lambda expression whenever an object of an interface with a single abstract method is expected. Such an interface is called a functional interface.
To demonstrate the conversion to a functional interface, consider the Arrays.sort method. Its second parameter requires an instance of Comparator, an interface with a single method. Simply supply a lambda:
Arrays.sort(words, (first, second) -> first.length() - second.length());
Behind the scenes, the Arrays.sort method receives an object of some class that implements Comparator<String>. Invoking the compare method on that object executes the body of the lambda expression. The management of these objects and classes is completely implementation-dependent, and it can be much more efficient than using traditional inner classes. It is best to think of a lambda expression as a function, not an object, and to accept that it can be passed to a functional interface.
This conversion to interfaces is what makes lambda expressions so compelling. The syntax is short and simple. Here is another example:
var timer = new Timer(1000, event -> { System.out.println("At the tone, the time is " + Instant.ofEpochMilli(event.getWhen())); Toolkit.getDefaultToolkit().beep(); });
That’s a lot easier to read than the alternative with a class that implements the ActionListener interface.
In fact, conversion to a functional interface is the only thing that you can do with a lambda expression in Java. In other programming languages that support function literals, you can declare function types such as (String, String) -> int, declare variables of those types, and use the variables to save function expressions. However, the Java designers decided to stick with the familiar concept of interfaces instead of adding function types to the language.
6.2.4. Function Types
The Java API defines a number of very generic functional interfaces in the java.util.function package. One of the interfaces, BiFunction<T, U, R>, describes functions with parameter types T and U and return type R. You can save your string comparison lambda in a variable of that type:
BiFunction<String, String, Integer> comp = (first, second) -> first.length() - second.length();
Note that this interface does not help you with sorting. There is no Arrays.sort method that wants a BiFunction. If you have used a functional programming language before, you may find this curious. But for Java programmers, it’s pretty natural. An interface such as Comparator has a specific purpose, not just a method with given parameter and return types. When you want to do something with lambda expressions, you still want to keep the purpose of the expression in mind, and have a specific functional interface for it.
A particularly useful interface in the java.util.function package is Predicate:
public interface Predicate<T> { boolean test(T t); // additional default and static methods }
The ArrayList class has a removeIf method whose parameter is a Predicate. It is specifically designed to pass a lambda expression. For example, the following statement removes all null values from an array list:
list.removeIf(e -> e == null);
Another useful functional interface is Supplier<T>:
public interface Supplier<T> { T get(); }
A supplier has no parameters and yields a value of type T when the get method is called:
Supplier<Integer> die = () -> (int)(Math.random() * 6) + 1; int outcome = die.get();
Suppliers are used for lazy evaluation. For example, consider the call
LocalDate hireDay = Objects.requireNonNullElse(day, LocalDate.of(1970, 1, 1));
This is not optimal. We expect that day is rarely null, so we only want to construct the default LocalDate when necessary. By using the supplier, we can defer the computation:
LocalDate hireDay = Objects.requireNonNullElseGet(day, () -> LocalDate.of(1970, 1, 1));
The requireNonNullElseGet method only calls the supplier when the value is needed.
Functional interfaces that involve primitive types are a little cumbersone. Consider a function consuming an int and yielding an object of type T. You could use a Function<Integer, T>, but then the argument must be boxed in each call. Instead, there is a functional interface IntFunction<T>. Conversely, if a function has a return value of type int, the ToIntFunction<T> interface is more efficient than Function<T, Integer>. Finally, if both argument and return value are int, there is an IntUnaryOperator interface.
As the user of an API, you don't usually care about this subtlety. Consider the Arrays.setAll method. It sets all values of an array to the result of a function whose argument is the array index. Here, we set all elements to the square of the index:
var values = new int[100]; Arrays.setAll(values, i -> i * i); // [0, 1, 4, 9, 16, . . ., 9801]
There are overloaded versions of setAll for arrays of type int[], long[], double[], and a generic array T[]. Here, the int[] overload has as second parameter an IntUnaryOperator. But as the user of the method, you don't care. You just supply the lambda expression, which you can do without worrying about the difference between primitive types and their wrapper classes.
6.2.5. Method References
Sometimes, a lambda expression involves a single method. For example, suppose you simply want to print the event object whenever a timer event occurs. Of course, you could call
var timer = new Timer(1000, event -> System.out.println(event));
It would be nicer if you could just pass the println method to the Timer constructor. Here is how you do that:
var timer = new Timer(1000, System.out::println);
The expression System.out::println is a method reference. It directs the compiler to produce an instance of a functional interface, overriding the single abstract method of the interface to call the given method. In this example, an ActionListener is produced whose actionPerformed(ActionEvent e) method calls System.out.println(e).
As another example, suppose you want to sort strings regardless of letter case. You can pass this method expression:
Arrays.sort(strings, String::compareToIgnoreCase)
As you can see from these examples, the :: operator separates the method name from the name of an object or class. There are three variants:
object::instanceMethod
Class::instanceMethod
Class::staticMethod
In the first variant, the method reference is equivalent to a lambda expression whose parameters are passed to the method. In the case of System.out::println, the object is System.out, and the method expression is equivalent to x -> System.out.println(x).
In the second variant, the first parameter becomes the implicit parameter of the method. For example, String::compareToIgnoreCase is the same as (x, y) -> x.compareToIgnoreCase(y).
In the third variant, all parameters are passed to the static method: Math::pow is equivalent to (x, y) -> Math.pow(x, y).
Table 6.1 walks you through additional examples.
Note that a lambda expression can only be rewritten as a method reference if the body of the lambda expression calls a single method and doesn't do anything else. Consider the lambda expression
s -> s.length() == 0
There is a single method call. But there is also a comparison, so you can't use a method reference here.
Table 6.1: Method Reference Examples
Method Reference |
Equivalent Lambda Expression |
Notes |
---|---|---|
separator::equals |
x -> separator.equals(x) |
This is a method expression with an object and an instance method. The lambda parameter is passed as the explicit parameter of the method. |
String::strip |
x -> x.strip() |
This is a method expression with a class and an instance method. The lambda parameter becomes the implicit parameter. |
String::concat |
(x, y) -> x.concat(y) |
Again, we have an instance method, but this time, with an explicit parameter. As before, the first lambda parameter becomes the implicit parameter, and the remaining ones are passed to the method. |
Integer.valueOf |
x -> Integer.valueOf(x) |
This is a method expression with a static method. The lambda parameter is passed to the static method. |
Integer.sum |
(x, y) -> Integer.sum(x, y) |
This is another static method, but this time with two parameters. Both lambda parameters are passed to the static method. The Integer.sum method was specifically created to be used as a method reference. As a lambda, you could just write (x, y) -> x + y. |
String::new |
x -> new String(x) |
This is a constructor reference—see Section 6.2.6. The lambda parameters are passed to the constructor. |
String[]::new |
n -> new String[n] |
This is an array constructor reference—see Section 6.2.6. The lambda parameter is the array length. |
You can capture the this parameter in a method reference. For example, this::equals is the same as x -> this.equals(x). It is also valid to use super. The method expression
super::instanceMethod
uses this as the target and invokes the superclass version of the given method. Here is an artificial example that shows the mechanics:
class Greeter { public void greet(ActionEvent event) { System.out.println("Hello, the time is " + Instant.ofEpochMilli(event.getWhen())); } } class RepeatedGreeter extends Greeter { public void greet(ActionEvent event) { var timer = new Timer(1000, super::greet); timer.start(); } }
When the RepeatedGreeter.greet method starts, a Timer is constructed that executes the super::greet method on every timer tick.
6.2.6. Constructor References
Constructor references are just like method references, except that the name of the method is new. For example, Person::new is a reference to a Person constructor. Which constructor? It depends on the context. Suppose you have a list of strings. Then you can turn it into an array of Person objects, by calling the constructor on each of the strings, with the following invocation:
ArrayList<String> names = . . .; Stream<Person> stream = names.stream().map(Person::new); List<Person> people = stream.toList();
We will discuss the details of the stream, map, and toList methods in Chapter 1 of Volume II. For now, what’s important is that the map method calls the Person(String) constructor for each list element. If there are multiple Person constructors, the compiler picks the one with a String parameter because it infers from the context that the constructor is called with a string.
You can form constructor references with array types. For example, int[]::new is a constructor reference with one parameter: the length of the array. It is equivalent to the lambda expression n -> new int[n].
Array constructor references are useful to overcome a limitation of Java. As you will see in Chapter 8, it is not possible to construct an array of a generic type T. (The expression new T[n] is an error since it would be “erased” to new Object[n]). That is a problem for library authors. For example, suppose we want to have an array of Person objects. The Stream interface has a toArray method that returns an Object array:
Object[] people = stream.toArray();
But that is unsatisfactory. The user wants an array of references to Person, not references to Object. The stream library solves that problem with constructor references. Pass Person[]::new to the toArray method:
Person[] people = stream.toArray(Person[]::new);
The toArray method invokes this constructor to obtain an array of the correct type. Then it fills and returns the array.
6.2.7. Variable Scope
Often, you want to be able to access variables from an enclosing method or class in a lambda expression. Consider this example:
public static void repeatMessage(String text, int delay) { ActionListener listener = event -> { System.out.println(text); Toolkit.getDefaultToolkit().beep(); }; new Timer(delay, listener).start(); }
Consider a call
repeatMessage("Hello", 1000); // prints Hello every 1,000 milliseconds
Now look at the variable text inside the lambda expression. Note that this variable is not defined in the lambda expression. Instead, it is a parameter variable of the repeatMessage method.
If you think about it, something nonobvious is going on here. The code of the lambda expression may run long after the call to repeatMessage has returned and the parameter variables are gone. How does the text variable stay around?
To understand what is happening, we need to refine our understanding of a lambda expression. A lambda expression has three ingredients:
A block of code
Parameters
Values for the free variables—that is, the variables that are not parameters and not defined inside the code
In our example, the lambda expression has one free variable, text. The data structure representing the lambda expression must store the values for the free variables—in our case, the string "Hello". We say that such values have been captured by the lambda expression. (It's an implementation detail how that is done. For example, one can translate a lambda expression into an object with a single method, so that the values of the free variables are copied into instance variables of that object.)
As you have seen, a lambda expression can capture the value of a variable in the enclosing scope. In Java, to ensure that the captured value is well-defined, there is an important restriction. In a lambda expression, you can only reference variables whose value doesn't change. For example, the following is illegal:
public static void countDown(int start, int delay) { ActionListener listener = event -> { start--; // ERROR: Can't mutate captured variable System.out.println(start); }; new Timer(delay, listener).start(); }
There is a reason for this restriction. Mutating variables in a lambda expression is not safe when multiple actions are executed concurrently. This won't happen for the kinds of actions that we have seen so far, but in general, it is a serious problem. See Chapter 10 for more information on this important issue.
It is also illegal to refer, in a lambda expression, to a variable that is mutated outside. For example, the following is illegal:
public static void repeat(String text, int count) { for (int i = 1; i <= count; i++) { ActionListener listener = event -> { System.out.println(i + ": " + text); // ERROR: Cannot refer to changing i }; new Timer(1000, listener).start(); } }
The rule is that any captured variable in a lambda expression must be effectively final. An effectively final variable is a variable that is never assigned a new value after it has been initialized. In our case, text always refers to the same String object, and it is OK to capture it. However, the value of i is mutated, and therefore i cannot be captured.
The body of a lambda expression has the same scope as a nested block. The same rules for name conflicts and shadowing apply. It is illegal to declare a parameter or a local variable in the lambda that has the same name as a local variable.
Path first = Path.of("/usr/bin"); Comparator<String> comp = (first, second) -> first.length() - second.length(); // ERROR: Variable first already defined
Inside a method, you can’t have two local variables with the same name, and therefore, you can’t introduce such variables in a lambda expression either.
When you use the this keyword in a lambda expression, you refer to the this parameter of the method that creates the lambda. For example, consider
public class Application { public void init() { ActionListener listener = event -> { System.out.println(this.toString()); . . . } . . . } }
The expression this.toString() calls the toString method of the Application object, not the ActionListener instance. There is nothing special about the use of this in a lambda expression. The scope of the lambda expression is nested inside the init method, and this has the same meaning anywhere in that method.
6.2.8. Processing Lambda Expressions
Up to now, you have seen how to produce lambda expressions and pass them to a method that expects a functional interface. Now let us see how to write methods that can consume lambda expressions.
The point of using lambdas is deferred execution. After all, if you wanted to execute some code right now, you’d do that, without wrapping it inside a lambda. There are many reasons for executing code later, such as:
Running the code in a separate thread
Running the code multiple times
Running the code at the right point in an algorithm (for example, the comparison operation in sorting)
Running the code when something happens (a button was clicked, data has arrived, and so on)
Running the code only when necessary
Let's look at a simple example. Suppose you want to repeat an action n times. The action and the count are passed to a repeat method:
repeat(10, () -> System.out.println("Hello, World!"));
To accept the lambda, we need to pick (or, in rare cases, provide) a functional interface. Table 6.2 lists the most important functional interfaces that are provided in the Java API. In this case, we can use the Runnable interface:
public static void repeat(int n, Runnable action) { for (int i = 0; i < n; i++) action.run(); }
Note that the body of the lambda expression is executed when action.run() is called.
Now let's make this example a bit more sophisticated. We want to tell the action in which iteration it occurs. For that, we need to pick a functional interface that has a method with an int parameter and a void return. The standard interface for processing int values is
public interface IntConsumer { void accept(int value); }
Here is the improved version of the repeat method:
public static void repeat(int n, IntConsumer action) { for (int i = 0; i < n; i++) action.accept(i); }
And here is how you call it:
repeat(10, i -> System.out.println("Countdown: " + (9 - i)));
Table 6.2: Common Functional Interfaces
Functional Interface |
Parameter Types |
Return Type |
Abstract Method Name |
Description |
Other Methods |
---|---|---|---|---|---|
Runnable |
none |
void |
run |
Runs an action without parameters or return value |
|
Supplier<T> |
none |
T |
get |
Supplies a value of type T |
|
Consumer<T> |
T |
void |
accept |
Consumes a value of type T |
andThen |
BiConsumer<T, U> |
T, U |
void |
accept |
Consumes values of types T and U |
andThen |
Function<T, R> |
T |
R |
apply |
A function with parameter of type T |
compose, andThen, identity |
BiFunction<T, U, R> |
T, U |
R |
apply |
A function with parameters of types T and U |
andThen |
UnaryOperator<T> |
T |
T |
apply |
A unary operator on the type T |
compose, andThen, identity |
BinaryOperator<T> |
T, T |
T |
apply |
A binary operator on the type T |
andThen, maxBy, minBy |
Predicate<T> |
T |
boolean |
test |
A boolean-valued function |
and, or, negate, isEqual, not |
BiPredicate<T, U> |
T, U |
boolean |
test |
A boolean-valued function with two parameters |
and, or, negate |
Table 6.3 lists the 34 available specializations for primitive types int, long, and double. As you will see in Chapter 8, it is more efficient to use these specializations than the generic interfaces. For that reason, I used an IntConsumer instead of a Consumer<Integer> in the example of the preceding section.
Table 6.3: Functional Interfaces for Primitive Types p, q is int, long, double; P, Q is Int, Long, Double
Functional Interface |
Parameter Types |
Return Type |
Abstract Method Name |
---|---|---|---|
BooleanSupplier |
none |
boolean |
getAsBoolean |
PSupplier |
none |
p |
getAsP |
PConsumer |
p |
void |
accept |
ObjPConsumer<T> |
T, p |
void |
accept |
PFunction<T> |
p |
T |
apply |
PToQFunction |
p |
q |
applyAsQ |
ToPFunction<T> |
T |
p |
applyAsP |
ToPBiFunction<T, U> |
T, U |
p |
applyAsP |
PUnaryOperator |
p |
p |
applyAsP |
PBinaryOperator |
p, p |
p |
applyAsP |
PPredicate |
p |
boolean |
test |
6.2.9. Creating Comparators
The Comparator interface has a number of convenient static methods for creating comparators. These methods are intended to be used with lambda expressions or method references.
The static comparing method takes a “key extractor” function that maps a type T to a comparable type (such as String). The function is applied to the objects to be compared, and the comparison is then made on the returned keys. For example, suppose you have an array of Person objects. Here is how you can sort them by name:
Arrays.sort(people, Comparator.comparing(Person::getName));
This is certainly much easier than implementing a Comparator by hand. Moreover, the code is clearer since it is obvious that we want to compare people by name.
You can chain comparators with the thenComparing method for breaking ties. For example,
Arrays.sort(people, Comparator.comparing(Person::getLastName) .thenComparing(Person::getFirstName));
If two people have the same last name, then the second comparator is used.
There are a few variations of these methods. You can specify a comparator to be used for the keys that the comparing and thenComparing methods extract. For example, here we sort people by the length of their names:
Arrays.sort(people, Comparator.comparing(Person::getName, (s, t) -> Integer.compare(s.length(), t.length())));
Moreover, both the comparing and thenComparing methods have variants that avoid boxing of int, long, or double values:
Arrays.sort(people, Comparator.comparing(Person::getName, Comparator.comparingInt(String::length)))
A shorter but perhaps less elegant way of producing the preceding operation would be:
Arrays.sort(people, Comparator.comparingInt(p -> p.getName().length()));
If your key function can return null, you will like the nullsFirst and nullsLast adapters. These static methods take an existing comparator and modify it so that it doesn’t throw an exception when encountering null values but ranks them as smaller or larger than regular values. For example, suppose getMiddleName returns a null when a person has no middle name. Then you can use Comparator.comparing(Person::getMiddleName, Comparator.nullsFirst(. . .)).
The nullsFirst method needs a comparator—in this case, one that compares two strings. The naturalOrder method makes a comparator for any class implementing Comparable. A Comparator.<String>naturalOrder() is what we need. (See Chapter 8 for an explanation of this syntax. Fortunately, the generic type can usually be inferred.) Here is the complete call for sorting by potentially null middle names. I use a static import of java.util.Comparator.*, to make the expression more legible.
Arrays.sort(people, comparing(Person::getMiddleName, nullsFirst(naturalOrder())));
The static reverseOrder method gives the reverse of the natural order. To reverse any comparator, use the reversed instance method. For example, naturalOrder().reversed() is the same as reverseOrder().