Java 8 - Functional interface with lambda
15 Dec 2016Java 8 introduces lots of new elements in the java syntax like lambda and functional interfaces.
These changes, even though just syntactic sugar over the previous syntax, change drastically the way a Java code looks. Someone, who is used to traditional Java and is well versed with it, would easily be baffled looking at those arrows ->
and double quotes ::
.
I would like to explore one of the new elements here. That is the functional interface
.
Functional interface, also known as, Single Abstract Method (SAM) interface is a well known and already existing construct in Java. It looks something like this
public interface Runnable {
void run();
}
So, by definition, SAM or functional interface is an interface in java having only a single method defined in it. It might extend other interfaces as long as the following conditions are met.
- There is only one abstract method defined. This is considering both the current interface and its parent. There can be any number of default methods.
java.lang.Object
methods can be present, liketoString
andequals
. They are redundant.
Runnable interface is a well known example and belongs to java.lang package.
Till java 7, to execute something in a new thread, something like this would be used.
Thread t = new Thread(
new Runnable() {
@Override
public void run() {
System.out.println("Hello, parallel World!");
}
}
);
t.start();
It uses an anonymous class which implements the Runnable interface. But, as we can see, the real executable statement here is in the line 5. The other lines do not more than cluttering the code. This obviously makes the syntax heavy.
As an improvement, in Java 8, one can use the following lambda syntax to initialise a functional interface.
Thread t = new Thread(() -> System.out.println("Hello, world!"));
t.run();
Here the lambda expression () -> System.out.println()
is actually the implementation of the abstract run
method of Runnable interface.
As we can see, the syntax is way more lighter. And the real executable line is clearly visible without the clutter.
In a way, it can be said that java 8 has given new life to functional interfaces.
Pro Tip :
Use annotation @FunctionalInterface
while defining a functional interface to get compile time errors and warning.
Example
One more code sample for methods with arguments.
A sample functional interface
@FunctionalInterface
public interface Printable {
void print(String a, String b);
}
A function expection a Printable object and the calling main method.
public class DemoFunctionalInterface {
public static void callPrintable(Printable p) {
p.print("Apple", "Ball");
}
public static void main(String[] args) {
callPrintable((a, b) -> System.out.println("Printing " + a + " " + b));
}
}
This main method would print Printing Apple Ball
.
Reference