Java Functional Interfaces

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java Functional Interfaces

An Interface that contains exactly one abstract method is known as functional interface. It can have any number of default, static methods but can contain only one abstract method. It can also declare methods of object class.

Functional Interface also known as Single Abstract Method Interfaces or SAM Interfaces. It is a new feature in Java, which helps to achieve functional programming approach.

Example 1

 
  1. @FunctionalInterface  
  2. interface sayable{  
  3.     void say(String msg);  
  4. }  
  5. public class FunctionalInterfaceExample implements sayable{  
  6.     public void say(String msg){  
  7.         System.out.println(msg);  
  8.     }  
  9.     public static void main(String[] args) {  
  10.         FunctionalInterfaceExample fie = new FunctionalInterfaceExample();  
  11.         fie.say("Hello there");  
  12.     }  
  13. }  

Output:

Hello there

A functional interface can have methods of object class. See in the following example.

Example 2

 
  1.       
  2. @FunctionalInterface  
  3. interface sayable{  
  4.     void say(String msg);   // abstract method  
  5.       
  6.     // It can contain any number of methods of Object class.  
  7.     int hashCode();  
  8.     String toString();  
  9.     boolean equals(Object obj);  
  10. }  
  11. public class FunctionalInterfaceExample implements sayable{  
  12.   
  13.     public void say(String msg){  
  14.         System.out.println(msg);  
  15.     }  
  16.     public static void main(String[] args) {  
  17.         FunctionalInterfaceExample fie = new FunctionalInterfaceExample();  
  18.         fie.say("Hello there");  
  19.     }  
  20. }  

Output:

Hello there

A functional interface can extends to other interface only when that does not have any abstract method.

Example 3

 
  1. interface sayable{  
  2.     void say(String msg);   // abstract method  
  3. }  
  4. @FunctionalInterface  
  5. interface doable extends sayable{  
  6.     // Invalid '@FunctionalInterface' annotation; doable is not a functional interface  
  7.     void doIt();  
  8. }  

Output:

compile-time error

Example 4

In the following example, a functional interface is extending to a non-functional interface.

 
  1. interface doable{  
  2.     default void doIt(){  
  3.         System.out.println("Do it now");  
  4.     }  
  5. }  
  6.   
  7. @FunctionalInterface  
  8. interface sayable extends doable{  
  9.     void say(String msg);   // abstract method  
  10. }  
  11.   
  12. public class FunctionalInterfaceExample implements sayable{  
  13.   
  14.     public void say(String msg){  
  15.         System.out.println(msg);  
  16.     }  
  17.     public static void main(String[] args) {  
  18.         FunctionalInterfaceExample fie = new FunctionalInterfaceExample();  
  19.         fie.say("Hello there");  
  20.         fie.doIt();  
  21.     }  
  22. }  

Output:

Hello there
Do it now

Java Predefined-Functional Interfaces

 

Java provides predefined functional interfaces to deal with functional programming by using lambda and method references.

You can also define your own custom functional interface. Following is the list of functional interface which are placed in java.util.function package.


Interface

Description

BiConsumer

It represents an operation that accepts two input arguments and returns no result.

Consumer

It represents an operation that accepts a single argument and returns no result.

Function

It represents a function that accepts one argument and returns a result.

Predicate

It represents a predicate (boolean-valued function) of one argument.

BiFunction

It represents a function that accepts two arguments and returns a a result.

BinaryOperator

It represents an operation upon two operands of the same data type. It returns a result of the same type as the operands.

BiPredicate

It represents a predicate (boolean-valued function) of two arguments.

BooleanSupplier

It represents a supplier of boolean-valued results.

DoubleBinaryOperator

It represents an operation upon two double type operands and returns a double type value.

DoubleConsumer

It represents an operation that accepts a single double type argument and returns no result.

DoubleFunction

It represents a function that accepts a double type argument and produces a result.

DoublePredicate

It represents a predicate (boolean-valued function) of one double type argument.

DoubleSupplier

It represents a supplier of double type results.

DoubleToIntFunction

It represents a function that accepts a double type argument and produces an int type result.

DoubleToLongFunction

It represents a function that accepts a double type argument and produces a long type result.

DoubleUnaryOperator

It represents an operation on a single double type operand that produces a double type result.

IntBinaryOperator

It represents an operation upon two int type operands and returns an int type result.

IntConsumer

It represents an operation that accepts a single integer argument and returns no result.

IntFunction

It represents a function that accepts an integer argument and returns a result.

IntPredicate

It represents a predicate (boolean-valued function) of one integer argument.

IntSupplier

It represents a supplier of integer type.

IntToDoubleFunction

It represents a function that accepts an integer argument and returns a double.

IntToLongFunction

It represents a function that accepts an integer argument and returns a long.

IntUnaryOperator

It represents an operation on a single integer operand that produces an integer result.

LongBinaryOperator

It represents an operation upon two long type operands and returns a long type result.

LongConsumer

It represents an operation that accepts a single long type argument and returns no result.

LongFunction

It represents a function that accepts a long type argument and returns a result.

LongPredicate

It represents a predicate (boolean-valued function) of one long type argument.

LongSupplier

It represents a supplier of long type results.

LongToDoubleFunction

It represents a function that accepts a long type argument and returns a result of double type.

LongToIntFunction

It represents a function that accepts a long type argument and returns an integer result.

LongUnaryOperator

It represents an operation on a single long type operand that returns a long type result.

ObjDoubleConsumer

It represents an operation that accepts an object and a double argument, and returns no result.

ObjIntConsumer

It represents an operation that accepts an object and an integer argument. It does not return result.

ObjLongConsumer

It represents an operation that accepts an object and a long argument, it returns no result.

Supplier

It represents a supplier of results.

ToDoubleBiFunction

It represents a function that accepts two arguments and produces a double type result.

ToDoubleFunction

It represents a function that returns a double type result.

ToIntBiFunction

It represents a function that accepts two arguments and returns an integer.

ToIntFunction

It represents a function that returns an integer.

ToLongBiFunction

It represents a function that accepts two arguments and returns a result of long type.

ToLongFunction

It represents a function that returns a result of long type.

UnaryOperator

It represents an operation on a single operand that returnsa a result of the same type as its operand.