Functional Interface was added in Java 8. Functional Interface is like a normal interface with only one abstract method. Remember, a Functional interface can have a default method but should have only one abstract method which is unimplemented. Also, @FunctionalInterface annotation is optional to define the functional interface.
Example for Functional Interface:
@FunctionalInterface //It is optional
interface PrintMessage{
public void message(String text);
}
Table of Contents
Built-In Functional Interfaces
java.util.function package has many functional interfaces. However below are the most popular used:
default Function andThen(Function super R,? extends V> after);
default Function compose(Function super V,? extends T> before)
static Function identity();
Example: Find the length of String using Function Interface
Here Function<String, Integer>, the first type String is for input type and second type Integer is for return type for apply() method.
public class FunctionExample
{
public static void main(String[] args) {
Function length = (a)->{
return a.length();
};
System.out.println("Length is : "+length.apply("Function interface"));
}
}
Output:
Length is : 18
What is andThen() Method?
In Function Interface, apply() will execute the implementation given in the lambda expression. And If we have multiple Function Interfaces and we want to execute those Function Interfaces in particular sequence where the output of one function can be input to the other function and at the end you get the output.
Example: Find the length of String using Function Interface with andThen() method
public class FunctionExample
{
// Function to calculate the length
Function length = (a)->a.length();
// Function to format the result
Function format = (a)-> "Length is " + a;
//Store the final result
String output = length.andThen(format).apply("Function interface");
System.out.println(output);
}
Output:
Length is : 18
What is compose() Method?
compose() method is also similar to andThen() method but it works a bit differently. compose() method accepts another function variable as a parameter.
Example:
Function<T,R> fun1 = (a)->a;
Function<T,R> fun2 = (b)->b;
Let’s say fun1.compose(fun2) in this statement we are calling the compose() method on fun1 and it accepts the other function fun2 as an input. While executing this statement, first fun2.apply() will be performed and the output of this will be passed to fun1. It is just the opposite of how andThen() method works.
Example: Find the length of String using Function Interface with compose() method
public class FunctionExample
{
// Function to calculate the length
Function length = (a)->a.length();
// Function to format the result
Function format = (a)-> "Length is " + a;
//Store the final result
String output = format.compose(length).apply("Function interface");
System.out.println(output);
}
Output:
Length is : 18
What is identity() Method?
identity() method has a very simple task. it just returns the value of the input. if you call Function.identity() it will return the same value which you gave in input. other than this it doesn’t have any other task.
public class FunctionExample
{
Function value = Function.identity();
System.out.println(value.apply("Test"));
}
Java 8 got a brand new API called streams. it makes every developer’s life easy. Stream APIs are used to process the group of data. mostly streams are applied on collection objects but we can use stream with any group
Consumer Interface is another Built-In Functional Interface of java.util.function package of Java 8. The consumer interface consumes the object and doesn’t return any object. it has only one abstract method called accept(). Along with this, it has one more default
Supplier Interface is another Built-In Functional Interface of java.util.function package of Java 8. Supplier Interface used for assignment purposes. It has only one abstract method and doesn’t have any default or static method. Below is the get() method signature. It
Predicate Interface is another Built-In Functional Interface of java.util.function package of Java 8. Predicate Interface can be used to evaluate the expression which returns the boolean result like true or false of the given expression. Example: Predicate logic = X
Functional Interface was added in Java 8. Functional Interface is like a normal interface with only one abstract method. Remember, a Functional interface can have a default method but should have only one abstract method which is unimplemented. Also, @FunctionalInterface