Java Tutorials
Java 8 introduced Lambda Expressions, These expressions depends on Functional Interfaces. A Functional Interface is an Interface which has only one Abstract method, and also it can have static and default methods. It can optionally be decorated with @FunctionalInterface Annotation.
Earlier java(i.e before JDK8) , an interfaces can be implemented using class/anonymous class. An anonymous class is a class which has no class name, and can implement interface methods.
Functional interfaces can be used as a return type for lambda expressions and method references.
for ex: implementing interface as an Anonymous class.interface IA { int add ( int a, int b ); } public class TestImpl { IA obj = new IA(){ @override int add ( int a, int a ) { return a + b; } }; public static void main(String[] args) { System.out.println(" Interface implementation using Anonymous Class"); TestImpl impl = new TestImpl (); int result = impl.obj.add( 10 , 20 ); System.out.println("Calling interface add method : "+ result); } }
//creating Anonymous method, assiging it to functional interface IA IA anyonymous = ( a , b ) -> a + b; //Invoking Anonymous method int result = anonymous.add( 10, 20 ); System.out.println("Calling interface add method using Lambda Expression : "+ result);
Java 8 provides many built-in Functional Interfaces. These interfaces located in java.util.function package
BiConsumer BiFunction BiPredicate BinaryOperator BooleanSupplier Consumer DoubleBinaryOperator DoubleConsumer DoubleFunction DoublePredicate DoubleSupplier DoubleToIntFunction DoubleToLongFunction DoubleUnaryOperator Function IntBinaryOperator IntConsumer IntFunction IntPredicate IntSupplier IntToDoubleFunction IntToLongFunction IntUnaryOperator LongBinaryOperator LongConsumer LongFunction LongPredicate LongSupplier LongToDoubleFunction LongToIntFunction LongUnaryOperator ObjDoubleConsumer ObjIntConsumer ObjLongConsumer Predicate Supplier ToDoubleBiFunction ToDoubleFunction ToIntBiFunction ToIntFunction ToLongBiFunction ToLongFunction UnaryOperatorJava built-in functional interfaces are categorized into 5 parts
Consumer Interfaces accepts input produces no result.
Please refer Consumer Functional Interfaces Implementation
Does not take any input and produces a result.
Please refer Supplier Functional Interfaces Implementation
Accepts input and Produces result
Please refer Operator Functional Interfaces Implementation
Represents a predicate i.e boolean-values True or False
Please refer Predicate Functional Interfaces Implementation
Takes one argument type and returns another argument type. This produces result of another type.
Please refer Function Functional Interfaces Implementation
ADS