CodesJava

Easy learning with example program codes

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.codesjava;
 
@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.codesjava;
 
@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

InterfaceDescription
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.
BooleanSupplierRepresents a supplier of boolean-valued results.
Consumer<T>Represents an operation that accepts a single input argument and returns no result.
DoubleBinaryOperatorRepresents an operation upon two double-valued operands and producing a double-valued result.
DoubleConsumerRepresents 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.
DoublePredicateRepresents a predicate (boolean-valued function) of one double-valued argument.
DoubleSupplierRepresents a supplier of double-valued results.
DoubleToIntFunctionRepresents a function that accepts a double-valued argument and produces an int-valued result.
DoubleToLongFunctionRepresents a function that accepts a double-valued argument and produces a long-valued result.
DoubleUnaryOperatorRepresents 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.
IntBinaryOperatorRepresents an operation upon two int-valued operands and producing an int-valued result.
IntConsumerRepresents 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.
IntPredicateRepresents a predicate (boolean-valued function) of one int-valued argument.
IntSupplierRepresents a supplier of int-valued results.
IntToDoubleFunctionRepresents a function that accepts an int-valued argument and produces a double-valued result.
IntToLongFunctionRepresents a function that accepts an int-valued argument and produces a long-valued result.
IntUnaryOperatorRepresents an operation on a single int-valued operand that produces an int-valued result.
LongBinaryOperatorRepresents an operation upon two long-valued operands and producing a long-valued result.
LongConsumerRepresents 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.
LongPredicateRepresents a predicate (boolean-valued function) of one long-valued argument.
LongSupplierRepresents a supplier of long-valued results.
LongToDoubleFunctionRepresents a function that accepts a long-valued argument and produces a double-valued result.
LongToIntFunctionRepresents a function that accepts a long-valued argument and produces an int-valued result.
LongUnaryOperatorRepresents 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.
Please follow and like us:








Copyright © 2019 CodesJava DMCA.com Protection Status