Functional Interfaces in Java 8 Explained
Overview
In this tutorial we will be looking at one of the most fundamental features of functional aspects of Java 8 - functional interfaces. We will start by looking at the definition of functional interfaces and the primary purpose for which they have been added to Java 8. This will be followed by understanding the new
Please note that
Example of using the
The code below shows the same
Examples of functional interfaces
Lets see some examples of functional interfaces to understand them better. I have split the examples into 3 types -
@FunctionalInterface
annotation introduced in Java 8 and see an example explaining the definition of a custom functional interface. Lastly, we will look at examples showing the 3 types of functional interfaces which are available at the disposal of a programmer in Java 8.
What is a Functional Interface
A functional interface, introduced in Java 8, is an interface which has only a single abstract method. Conversely, if you have any interface which has only a single abstract method, then that will effectively be a functional interface. This interface can then be used anywhere where a functional interface is eligible to be used.
The primary purpose served by Functional Interfaces
One of the most important uses of Functional Interfaces is that implementations of their abstract method can be passed around as lambda expressionsRead Java 8 Lambda Expressions Tutorial. By virtue of their ability to pass around functionality(i.e. behavior), functional interfaces primarily enable behavior parameterization.
@FunctionalInterface(java.lang.FunctionalInterface) annotation
@FunctionalInterface
annotation can be used to explicitely specify that a given interface is to be treated as a functional interface. Then the compiler would check and give a compile-time error in case the annotated interface does not satisfy the basic condition of qualifying as a functional interface(that of having a single abstract method).@FunctionalInterface
is an informative annotation, i.e. it is not mandatory to use this annotation for classifying the given interface as a valid functional interface. Rather, if one uses this annotation, then the compiler ensures that the interface is not inadvertently changed in such a way that it no longer remains a functional interface.Example of using the
@FunctionalInterface
tagThe code below shows the same
CustomFunctionalInterface
defined above with just the @FunctionalInterface
annotation added on top of it to demonstrate the use of the annotation - Java 8 code showing usage of @FunctionalInterface tag
package com.javabrahman.java8;
@FunctionalInterface
public interface CustomFunctionalInterface {
//Single abstract method
public void firstMethod();
}
- Custom Or User defined Functional Interfaces - These are interfaces defined by the user and have a single abstract method. These may/may not be annotated by
@FunctionalInterface
. Let us see the code for a user-defined functional interface namedCustomFunctionalInterface
below -
Example of custom Or user-defined Functional Interfacespackage com.javabrahman.java8; public interface CustomFunctionalInterface { //This is the only abstract method.Hence, this //interface qualifies as a Functional Interface public void firstMethod(); }
- Pre-existing functional interfaces in Java prior to Java 8 - These are interfaces which already exist in Java Language Specification and have a single abstract method. Eg.
java.lang.Runnable
,java.lang.Callable<V>
. Let use see the code used for defining these pre-existing functional interfaces.
Example of pre-existing functional interfaces in Java//java.lang.Runnable @FunctionalInterface public interface Runnable { public abstract void run(); } //java.lang.Callable<V> @FunctionalInterface public interface Callable<V> { V call() throws Exception; }
- Newly defined functional interfaces in Java 8 in
java.util.function
package - These are pre-defined Functional Interfaces introduced in Java 8. They are defined with generic types and are re-usable for specific use cases. One such Functional Interface is thePredicate<T>
interface which is defined as follows -
Example of new pre-defined functional interfaces in Java 8//java.util.function.Predicate<T> @FunctionalInterface public interface Predicate<T> { boolean test(T t); }
@FunctionalInterface
annotation introduced in Java 8 and then finally saw examples of various types of functional interfaces available in Java 8.Functional Interface tutorials on JavaBrahman
Functional Interfaces in Java 8Tutorial on basics of Functional Interfaces in Java 8 Predicate Interface TutorialClick to read tutorial on Predicate Functional Interface Consumer Interface TutorialClick to read tutorial on Consumer Functional Interface Function Interface TutorialClick to read tutorial on Function Interface Supplier Interface TutorialClick to read tutorial on Supplier Functional Interface java.util.function package overviewClick to read overview of java.util.function package
Functional Interfaces in Java 8Tutorial on basics of Functional Interfaces in Java 8 Predicate Interface TutorialClick to read tutorial on Predicate Functional Interface Consumer Interface TutorialClick to read tutorial on Consumer Functional Interface Function Interface TutorialClick to read tutorial on Function Interface Supplier Interface TutorialClick to read tutorial on Supplier Functional Interface java.util.function package overviewClick to read overview of java.util.function package