Java 8 Functional Interface

Java 8 Functional Interface

Functional Interface is an interface with only single abstract method. As a functional interface can have only one abstract method that’s why it is also known as Single Abstract Method Interfaces or SAM Interfaces. We can either create our own functional interface or can use predefined functional interfaces provided by java.

Note:
When we creating our own functional interface we should use @FunctionalInterface annotation to mark it as functional interface. Although it is not mandatory to use it, but it’s good to use it with functional interfaces to avoid addition of extra methods accidentally. It will throw compile time error if we try to add more than one abstract method in a functional interface.

Rules to define a functional interface:

A functional interface can have only one abstract method. Along with the one abstract method, it can have any number of default and static methods. It can also have methods of object class.

A functional interface can extends another interface only when it does not have any abstract method.

Java functional interface example

package com.w3schools;

@FunctionalInterface  
interface AddInterface{  
    void add(int a, int b);  
}  

public class FunctionalInterfaceExample implements AddInterface {
	public void add(int a, int b){  
        System.out.println(a+b);  
    } 
	public static void main(String args[]){
		FunctionalInterfaceExample fie = new FunctionalInterfaceExample();  
        fie.add(10, 20);  
	}
}

Output

30

A functional interface can have methods of object class.

package com.w3schools;

@FunctionalInterface  
interface AddInterface{  
    void add(int a, int b);  
    
    //It can contain any number of Object class methods.  
    int hashCode();  
    String toString();  
    boolean equals(Object obj);  
}  

public class FunctionalInterfaceExample implements AddInterface {
	public void add(int a, int b){  
        System.out.println(a+b);  
    } 
	public static void main(String args[]){
		FunctionalInterfaceExample fie = new FunctionalInterfaceExample();  
        fie.add(10, 20);  
	}
}

Output

30

Predefined function interfaces

Interface Description
BiConsumer<T,U> Represents an operation that accepts two input arguments and returns no result.
BiFunction<T,U,R> Represents a function that accepts two arguments and produces a result.
BinaryOperator<T> Represents an operation upon two operands of the same type, producing a result of the same type as the operands.
BiPredicate<T,U> Represents a predicate (boolean-valued function) of two arguments.
BooleanSupplier Represents a supplier of boolean-valued results.
Consumer<T> Represents an operation that accepts a single input argument and returns no result.
DoubleBinaryOperator Represents an operation upon two double-valued operands and producing a double-valued result.
DoubleConsumer Represents an operation that accepts a single double-valued argument and returns no result.
DoubleFunction<R> Represents a function that accepts a double-valued argument and produces a result.
DoublePredicate Represents a predicate (boolean-valued function) of one double-valued argument.
DoubleSupplier Represents a supplier of double-valued results.
DoubleToIntFunction Represents a function that accepts a double-valued argument and produces an int-valued result.
DoubleToLongFunction Represents a function that accepts a double-valued argument and produces a long-valued result.
DoubleUnaryOperator Represents an operation on a single double-valued operand that produces a double-valued result.
Function<T,R> Represents a function that accepts one argument and produces a result.
IntBinaryOperator Represents an operation upon two int-valued operands and producing an int-valued result.
IntConsumer Represents an operation that accepts a single int-valued argument and returns no result.
IntFunction<R> Represents a function that accepts an int-valued argument and produces a result.
IntPredicate Represents a predicate (boolean-valued function) of one int-valued argument.
IntSupplier Represents a supplier of int-valued results.
IntToDoubleFunction Represents a function that accepts an int-valued argument and produces a double-valued result.
IntToLongFunction Represents a function that accepts an int-valued argument and produces a long-valued result.
IntUnaryOperator Represents an operation on a single int-valued operand that produces an int-valued result.
LongBinaryOperator Represents an operation upon two long-valued operands and producing a long-valued result.
LongConsumer Represents an operation that accepts a single long-valued argument and returns no result.
LongFunction<R> Represents a function that accepts a long-valued argument and produces a result.
LongPredicate Represents a predicate (boolean-valued function) of one long-valued argument.
LongSupplier Represents a supplier of long-valued results.
LongToDoubleFunction Represents a function that accepts a long-valued argument and produces a double-valued result.
LongToIntFunction Represents a function that accepts a long-valued argument and produces an int-valued result.
LongUnaryOperator Represents an operation on a single long-valued operand that produces a long-valued result.
ObjDoubleConsumer<T> Represents an operation that accepts an object-valued and a double-valued argument, and returns no result.
ObjIntConsumer<T> Represents an operation that accepts an object-valued and a int-valued argument, and returns no result.
ObjLongConsumer<T> Represents an operation that accepts an object-valued and a long-valued argument, and returns no result.
Predicate<T> Represents a predicate (boolean-valued function) of one argument.
Supplier<T> Represents a supplier of results.
ToDoubleBiFunction<T,U> Represents a function that accepts two arguments and produces a double-valued result.
ToDoubleFunction<T> Represents a function that produces a double-valued result.
ToIntBiFunction<T,U> Represents a function that accepts two arguments and produces an int-valued result.
ToIntFunction<T> Represents a function that produces an int-valued result.
ToLongBiFunction<T,U> Represents a function that accepts two arguments and produces a long-valued result.
ToLongFunction<T> Represents a function that produces a long-valued result.
UnaryOperator<T> Represents an operation on a single operand that produces a result of the same type as its operand.