Lambda expressions provide a clear and concise way of implementing a single-method interface using an expression. It allows you to reduce the amount of code you have to create and maintain. It's often a concise replacement for anonymous classes.
Functional Interfaces
Lambdas can only operate on a functional interface, which is an interface with just one abstract method. Functional interfaces can have any number of default or static methods.
When declaring a functional interface the @FunctionalInterface annotation can be added. This has no special effect, but a compiler error will be generated if this annotation is applied to an interface which is not functional, thus acting as a reminder that the interface should not be changed.
Examples of functional interfaces:
interface Foo1 {
void bar();
}
interface Foo2 {
int bar(boolean baz);
}
interface Foo3 {
String bar(Object baz, int mink);
}
interface Foo4 {
default String bar() { // default so not counted
return "baz";
}
void quux();
}
@FunctionalInterface
interface Foo5 {
void bar();
}
@FunctionalInterface
interface BlankFoo1 extends Foo3 { // inherits its abstract method from Foo3
}
@FunctionalInterface
interface Foo6 {
void bar();
boolean equals(Object obj); // overrides one of Object's method so not counted
}
Conversely, this is not a functional interface, as it has more than one method:
interface BadFoo {
void bar();
void quux(); // <-- Second method prevents lambda: which one do we use?
}
This is also not a functional interface, as it does not have any methods:
interface BlankFoo2 {}
Java 8 also provides a number of generic templated functional interfaces in the package java.util.function. For example, the built-in interface Predicate<T> wraps a single method which inputs a value of type T and returns a boolean.
Lambda Expressions
The basic structure of a Lambda expression is:
fi will then hold a singleton instance of an anonymous class which implements FunctionalInterfaceand where the one method's definition is { System.out.println("Hello") }. In other words, the above is exactly equivalent to:
FunctionalInterface fi = new FunctionalInterface() {
@Override
public void theOneMethod() {
System.out.println("Hello");
}
}
You cannot specify the name of the method when using a lambda—but you shouldn't need to, because a functional interface must have only one abstract method, so Java overrides that one.
In cases where the type of the lambda is not certain, (e.g. overloaded methods) you can add a cast to the lambda to tell the compiler what its type should be, like so:
Object fooHolder = (Foo1) () -> System.out.println("Hello");
System.out.println(fooHolder instanceof Foo1); // returns true
If the functional interface's single method takes parameters, the local formal names of these should appear between the brackets of the lambda. There is no need to declare the type of the parameter or return as these are taken from the interface (although it is not an error to declare the parameter types if you want to). Thus, these two examples are equivalent:
Foo2 longFoo = new Foo2() {
@Override
public int bar(boolean baz) {
if (baz) return 1; else return 0;
}
};
Foo2 shortFoo = (x) -> { if (x) return 1; else return 0; };
The parentheses around the argument can be omitted if the function only has one argument:
Foo2 np = x -> { if (x) return 1; else return 0; }; // okay
Foo3 np2 = x, y -> x.toString() + y // not okay
Implicit Returns
If the code placed inside a lambda is a Java expression rather than a statement, it is treated as a method which returns the value of the expression. Thus, the following two are equivalent:
IntUnaryOperator addOneShort = (x) -> (x+1);
IntUnaryOperator addOneLong = (x) -> { return (x+1); };
Accessing Local Variables (value closures)
Since lambdas are syntactic shorthand for anonymous classes, they follow the same rules for accessing local variables in the enclosing scope; the variables must be treated as final and not modified inside the lambda.
IntUnaryOperator makeAdder(int amount) {
return (x) -> (x + amount); // Legal even though amount will go out of scope
// because amount is not modified
}
IntUnaryOperator makeAccumulator(int value) {
return (x) -> { value += x; return value; }; // Will not compile
}
If it is necessary to wrap a changing variable in this way, a regular object that keeps a copy of the variable should be used.
Accepting Lambdas
Because a lambda is an implementation of an interface, nothing special needs to be done to make a method accept a lambda: any function which takes a functional interface can also accept a lambda.
public void passMeALambda(Foo1 f) {
f.bar();
}
passMeALambda(() -> System.out.println("Lambda called"));
Please share your Opinion on this article.
ReplyDelete