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.

1

2

3

4

5

// Multiplication functional interface with on abstract method

@FunctionalInterface

publicinterfaceMultiplication{

publicTmultiply(Tval1,Tval2);

}

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

publicclassTestMultiplication{

publicstaticvoidmain(String[]args){

//Implementation of abstract method

Multiplication multiply=(Integert1,Integert2)-&gt;{

returnt1 *t2;

};

// lambda expression

Multiplication mul1=(i1,i2)-&gt;i1 *i2;

// lambda expression

Multiplication mul2=(i1,i2)-&gt;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

1

2

3

4

@FunctionalInterface

publicinterfaceEqual{

booleanisEqual(Tval1,Tval2);

}

Implementing the Equal interface and providing different type of parameters to validate if two values are equal.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

publicclassTest{

publicstaticvoidmain(String[]args){

//Implementation of abstract method

Multiplication multiply=(Integert1,Integert2)-&gt;{

returnt1 *t2;

};

// lambda expression

Multiplication mul1=(i1,i2)-&gt;i1 *i2;

// lambda expression

Multiplication mul2=(i1,i2)-&gt;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=(Integert1,Integert2)-&gt;t1==t2;

System.out.println(eq1.isEqual(10,10));

Equal eq2=(t1,t2)-&gt;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.