This post compliments the previous post where I have discussed about Lambda Expression, link. To unleash the power of lambda, a good understanding of functional interface is must. Let's briefly cover it:
List<> interface adds a sort method so that you can sort a list by calling sort method on its instance instead of calling sort method from Collections utility class.
Functional Interface
Functional Interfaces are those Interfaces in Java which has only one abstract method. Interfaces having single method (like Runnable, Callable, Comparable etc) existed in Java even before version 8. It's just that this new term was coined in context of Lamda Expression.
Functional interfaces are pre-condition for Lambda expression. Lambda expression allows you to provide the implementation of the method inline as an anonymous implementation of the interface. Before Java 8, programmers used to provide implementation of functional interfaces using Anonymous class (which let you declare and instantiate the class at same time). Below is mostly commonly seen way to implement a Runnable task.
Runnable task = new Runnable(){
public void run(){
//implementation...bla bla.
}
};
If interface is NOT functional, implementing the interface or providing anonymous implementation as shown above are two options. But if there is just one abstract method in the interface, Java 8 provides a more succinct and less verbose style to provide implementation. Java also introduces an optional annotation to mark an interface as functional.
@FunctionalInterface
public interface MyFunctionalIf {
public String doSomething(String);
}
Annotation is used to indicate that the interface is intended to be functional interface. It's not mandatory to use the annotation but definitely a good practice. This annotation will also prevent you to accidentally add another method and break the functional contract of having just a single method. One important point to keep in mind is that default and static methods don't break the functional contract.
Above interface can be used by implementing it in a concrete class or by providing ad-hoc implementation using anonymous class as shown for Runnable.
Above interface can be used by implementing it in a concrete class or by providing ad-hoc implementation using anonymous class as shown for Runnable.
Implementing Functional Interface using Lambdas
Functions were not first class citizen (before Java 8) and you need to have a handle of object to call/use method. Now let's see how lambda changes the implementation of functional interfaces:
Runnable lambdaTask = () -> { //implementation };
lambdaTask.run(); //using lambda
MyFunctionalIf myFunctionalIf = (String message) -> "hello "+ message;
myFunctionalIf.doSomething("Sid"); //using lambda
Now, look how cleaner the functional implementation of the interfaces look. It's very concise, flexible and re-usable.
Please note that in second case, the curly brace as well as return clause is missing (both can be removed if there is only one line in the method body. Even type in argument is optional, so you can ignore String in above lambda expression. Notice that the name of the method is missing in lambda so it is also called as anonymous method.
Please note that in second case, the curly brace as well as return clause is missing (both can be removed if there is only one line in the method body. Even type in argument is optional, so you can ignore String in above lambda expression. Notice that the name of the method is missing in lambda so it is also called as anonymous method.
Default Method
Now interfaces can also provide a default methods. Obviously, functional interfaces can also have any number of default methods. You just need to add default keyword in method definition.List<> interface adds a sort method so that you can sort a list by calling sort method on its instance instead of calling sort method from Collections utility class.
default void sort(Comparator<? super E> c) {
//implementation
}
In-built Functional Interfaces Added In Java 8
Imagine creating your own multiple functional interfaces. But, Java helps programmers by providing some reusable functional interfaces out of the box.
Java designers have created a separate package for same, java.util.funtion.
So do check this package before creating your own functional interfaces. Chances are high that you will find one matching your needs.
---
keep coding !!!
No comments:
Post a Comment