Functional interfaces in java

What is a functional interface?

Functional interfaces in java is an interface where you have only one abstract method. It can have any number of default methods (read more about default methods). Runnable, Comparator, Callable, and FileFilter are some examples of functional interfaces in java.

With Java 8, you can use lambda expressions to represent the instance of a functional interface.

Before Java 8, either you had to create an anonymous inner class or implement the particular interface to use the interface. For example, consider the below scenario.

interface Addition
{
  int add(int x,int y);
}

One way of using this interface before java 8 was to implement this interface.

class AdditionImpl implements Addition{

  @Override
  public int add(int x, int y) {
     return x+y;
  }
}

Another way of using the interface is to define an anonymous inner class 

Addition addition = new Addition() {

  @Override
  public int add(int x, int y) {
     return x+y;
  }
};

With java 8 lambda expressions you can use this asĀ 

Addition addition = (a,b)-> a+b;
addition.add(10,5);

Explanation (a,b)-> a+b

(a,b) The left side of the expression defines which parameters are expected by the method add. a+b , The right side of the expression defines the implementation of the method.

Functional interfaces provided by java 8

Java 8 provides 43 functional interfaces in the java.util.function package. As java developers, we are expected to be familiar with all of these interfaces in order to use them efficiently. The good news is that there are some variants that exists of the same functional interface among these 43.

For example, UnaryOperator<T> is a functional interface which receives the argument of type T and returns the result as type T. You also have DoubleUnaryOperator, IntUnaryOperator, LongUnaryOperator, etc. As you can see, all these are variations of the UnaryOperator functional interface for the primitive types just for performance reasons.

We can categories these 43 functional interfaces into six different types to make it simple to understand.

  • UnaryOperator<T>
  • BinaryOperator<T>
  • Predicate<T>
  • Function<T, R>
  • Supplier<T>
  • Consumer<T>

Creating your own functional interface

You must think twice before creating your own functional interface. That is because java has already provided 43 functional interfaces, and the developers have to get familiar with them. Creating your own functional interface will make them learn one more functional interface, which is not so great.

Before creating your own functional interface, its good to check whether its a variance of the existing functional interfaces provided by java and may try using that instead. However there can be situations where you have to define your own functional interface, and then you can define it as below.

@FunctionalInterface
interface MyFunctionalInterface
{
  public void doSomething();
}

Please note the @FunctionalInterface annotation. This is to let others know that this is a functional interface and should not add more abstract methods in this interface.


Leave a Reply

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