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 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.

The following example: Equal functional interface

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

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.



Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.