Functional Interfaces in Java
Java 8 introduced lambda expressions, which allowed us to write short and simple code using default interface methods, lambda expressions, method reference and repeatable annotations. As we know java is a strongly typed language and it is mandatory to declare types but designers of lambda expressions omitted types when declaring the lambda expressions. To represents lambda expressions, the designer smartly used existing anonymous method strategy. I assume you are already familiar with anonymous classes.
A functional interface is a kind of special interface having only one abstract method and can be decorated using @FuntionalInterface annotation to be used as a lambda expression.
// Multiplication functional interface with on abstract method @FunctionalInterface public interface Multiplication { public T multiply(T val1, T val2); }
Multiplication interface is generic and has only one abstract method multiply and annotated with @FunctionalInterface. Therefore we are using different types in our examples and find the some examples given below to elaborate the use of Multiplication functional interface.
public class TestMultiplication { public static void main(String[] args) { //Implementation of abstract method Multiplication multiply = (Integer t1, Integer t2) -> { return t1 * t2; }; // lambda expression Multiplication mul1 = (i1, i2) -> i1 * i2; // lambda expression Multiplication mul2 = (i1, i2) -> i1 * i2; //printing the result generated by lambda expression System.out.println(mul1.multiply(10, 20)); //printing the result generated by lambda expression System.out.println(mul2.multiply(10.5, 20.5)); //printing the result generated by lambda expression System.out.println(multiply.multiply(10, 20)); } }
The following example: Equal functional interface
@FunctionalInterface public interface Equal { boolean isEqual(T val1, T val2); }
Implementing the Equal interface and providing different type of parameters to validate if two values are equal.
public class Test { public static void main(String[] args) { //Implementation of abstract method Multiplication multiply = (Integer t1, Integer t2) -> { return t1 * t2; }; // lambda expression Multiplication mul1 = (i1, i2) -> i1 * i2; // lambda expression Multiplication mul2 = (i1, i2) -> i1 * i2; //printing the result generated by lambda expression System.out.println(mul1.multiply(10, 20)); //printing the result generated by lambda expression System.out.println(mul2.multiply(10.5, 20.5)); //printing the result generated by lambda expression System.out.println(multiply.multiply(10, 20)); Equal eq1 = (Integer t1, Integer t2) -> t1==t2; System.out.println(eq1.isEqual(10, 10)); Equal eq2 = (t1, t2) -> t1==t2; System.out.println(eq2.isEqual(10.5, 10.2)); } }
Built in Functional Interfaces
Java Development kit (JDK 1.8) provides many built in functional interfaces. Few of them are well known and are available in older version of java like Comparator and Runnable. These are interfaces are updated with lambda support with @FuntionalInterface annotation. Few of these new interfaces belongs to Google Guava library and some useful interfaces examples are given below.
import java.util.function.Predicate; public class TestFunctional { public static void main(String[] args){ Predicatepredicate = (str) -> str.length() > 5; System.out.println(predicate.test("Hello World")); System.out.println(predicate.negate().test("Hello World")); } }