«Back to Home

Core Java

Topics

Functional Interfaces In Java

Functional Interfaces
 
Functional interfaces have a single functionality to show. Such as, a Comparable interface has a single method ‘compareTo’ only and it is used for comparison purpose.
 
Java 8 has defined many functional interfaces to be used widely in lambda expressions.
 
Functional interfaces that are define in java.util.Function package

Interfaces

BiConsumer<T,U>

This interface represents an operation which accepts two input arguments.
 
BiFunction<T,U,R>

This interface represents a function which accepts two arguments and returns a result.
 
BinaryOperator<T>

This interface represents an operation upon two operands of the same type and returns a result of the same type as the operands.
 
BiPredicate<T,U>

This interface represents a predicate of two arguments.
 
BooleanSupplier

This interface represents a supplier of Boolean-valued results.
 
Consumer<T>

This interface represents an operation which accepts a single input argument.
 
DoubleBinaryOperator

This interface represents an operation upon two double-valued operands and returns a double-valued result.
 
DoubleConsumer

This interface represents an operation which accepts a single double-valued argument.
 
DoubleFunction<R>

This interface represents a function which accepts a double-valued argument and returns a result.
 
DoublePredicate

This interface represents a predicate of one double-valued argument.
 
DoubleSupplier

This interface represents a supplier of double-valued results.
 
DoubleToIntFunction

This interface represents a function which accepts a double-valued argument and returns an int-valued result.
 
DoubleToLongFunction

This interface represents a function which accepts a double-valued argument and returns a long-valued result.


DoubleUnaryOperator

This interface represents an operation on a single double-valued operand which returns a double-valued result.
 
Function<T,R>

This interface represents a function which accepts one argument and returns a result.
 
IntBinaryOperator

This interface represents an operation upon two int-valued operands and returns an int-valued result.
 
IntConsumer

This interface represents an operation which accepts a single int-valued argument.
 
IntFunction<R>

This interface represents a function which accepts an int-valued argument and returns a result.
 
IntPredicate

This interface represents a predicate of one int-valued argument.
 
IntSupplier

This interface represents a supplier of int-valued results.
 
IntToDoubleFunction

This interface represents a function which accepts an int-valued argument and returns a double-valued result.
 
IntToLongFunction

This interface represents a function which accepts an int-valued argument and returns a long-valued result.
 
IntUnaryOperator

This interface represents an operation on a single int-valued operand that returns an int-valued result.
 
LongBinaryOperator

This interface represents an operation upon two long-valued operands and returns a long-valued result.
 
LongConsumer

This interface represents an operation which accepts a single long-valued argument.
 
LongFunction<R>

This interface represents a function which accepts a long-valued argument and returns a result.
 
LongPredicate

This interface represents a predicate of one long-valued argument.
 
LongSupplier

This interface represents a supplier of long-valued results.
 
LongToDoubleFunction

This interface represents a function which accepts a long-valued argument and returns a double-valued result.
 
LongToIntFunction

This interface represents a function which accepts a long-valued argument and returns an int-valued result.
 
LongUnaryOperator

This interface represents an operation on a single long-valued operand that returns a long-valued result.
 
ObjDoubleConsumer<T>

This interface represents an operation which accepts an object-valued and a double-valued argument.
 
ObjIntConsumer<T>

This interface represents an operation which accepts an object-valued and an int-valued argument.
 
ObjLongConsumer<T>

This interface represents an operation which accepts an object-valued and a long-valued argument.
 
Predicate<T>

This interface represents a predicate of one argument.
 
Supplier<T>

This interface represents a supplier of results.
 
ToDoubleBiFunction<T,U>

This interface represents a function which accepts two arguments and returns a double-valued result.
 
ToDoubleFunction<T>

This interface represents a function which returns a double-valued result.
 
ToIntBiFunction<T,U>

This interface represents a function which accepts two arguments and returns an int-valued result.
 
ToIntFunction<T>

This interface represents a function which returns an int-valued result.
 
ToLongBiFunction<T,U>

This interface represents a function which accepts two arguments and returns a long-valued result.
 
ToLongFunction<T>

This interface represents a function which returns a long-valued result.
 
UnaryOperator<T>

This interface represents an operation on a single operand which returns a result of the same type as its operand.
 
Let’s see an example of Functional Interface.
 
Code
  1. import java.util.Arrays;  
  2. import java.util.List;  
  3. import java.util.function.Predicate;  
  4. public class FunctionalInterfacesExample {  
  5.    public static void main(String args[]){  
  6.       List<Integer> li = Arrays.asList(12345);  
  7.       System.out.println("Numbers:");  
  8.       //pass a as parameter  
  9.       eval(li, a->true);  
  10.       // a is passed as parameter to test method of Predicate interface  
  11.       // test method will return true if a%2 comes to be zero  
  12.       System.out.println("Even numbers:");  
  13.       eval(li, a-> a%2 == 0 );     
  14.       // a is passed as parameter to test method of Predicate interface  
  15.       // test method will return true if a is greater than 2.     
  16.       System.out.println("Numbers greater than 2:");  
  17.       eval(li, a-> a > 2 );  
  18.    }  
  19.    public static void eval(List<Integer> li, Predicate<Integer> predicate) {  
  20.       for(Integer a: li) {    
  21.          if(predicate.test(a)) {  
  22.             System.out.println(a + " ");  
  23.          }  
  24.       }  
  25.    }  
  26. }  
16

Output

17

In the above example, we pass Predicate interface that takes a single input and returns Boolean. Predicate <T> interface is a functional interface and a method test(Object) to return a Boolean value. This interface shows that an object is tested to be true or false.
 
Summary

Thus, we learned that Functional interfaces have a single functionality to show and Java 8 has defined many functional interfaces to be used widely in lambda expressions and also learns how to use it in Java.