Java 8 – Functional Interface with examples

In this article we will look into Java 8 functional interfaces, An interface which contains only one abstract method is called as Functional interface.

Rules for Functional Interface :

  • Functional Interface can have maximum of only one abstract method
  • In short, it is called as Single Abstract Method i.e.; SAM
  • In addition to one abstract method, we can have any number of static or default methods.
  • @FunctionalInterface annotation and abstract keyword are optional
  • If you declare an interface with @FunctionalInterface then the annotated interface must satisfy the requirements of a functional interface, if not match requirement will get the compile-time issue.
  • Note: To invoke Lambda Expression we need Functional Interface and it is must.

Some Built-in Java Functional Interfaces

Java 1.8 onwards, there are many interfaces that are converted into functional interfaces. All these interfaces are annotated with @FunctionalInterface. These interfaces are as follows –

  • Runnable : This interface only contains the run() method.
  • Comparable : This interface only contains the compareTo() method.
  • ActionListener : This interface only contains the actionPerformed() method.
  • Callable : This interface only contains the call() method.

Java 8 — Functional Interfaces:

Java 8 has some inbuilt functional interfaces that are heavily implemented by lambda expressions and used with stream APIs and these functional interfaces are defined in the java.util.function package.

Types of Functional interfaces

Functional interfaces in Java are mainly of four types:

  • Function
  • Supplier
  • Consumer
  • Predicate

Functional interfaces can be divided into the following types:

  • Consumer and Bi-consumer
  • Predicate and Bi-predicate
  • Function and Bi-Function

Function<T, R>

  • Abstract method: R apply(T t)
  • This represents a function that accepts one argument and produces a result.
  • It is commonly used for mapping or transforming input to output.

The function type functional interface receives a single argument, processes it, and returns a value. One of the applications of this type of functional interface is taking the key from the user as input and searching for the value in the map for the given key.

@FunctionalInterface
public interface Function<T, R>{
R apply(T t);
}

Supplier<T>

  • Abstract method: T get()
  • This represents a supplier of results, providing a value without taking any input.
  • It is commonly used for lazy initialization or generating values.

The supplier functional interface in Java is much like a functional interface, the only difference is it doesn’t take any arguments. On calling the supplier it simply returns a value. Supplier is a generic interface thus, it takes the type of value in <> (Angular brackets) while implementing to be returned by the get() method.

@FunctionalInterface
public interface Supplier<T>{
T get();
}

 Consumer<T>

  • Abstract method: void accept(T t)
  • This represents an operation that accepts a single input and returns no value or result.
  • It is used for performing actions on input values, such as printing, saving, or processing.

The Consumer functional interface in Java accepts a single gentrified argument and doesn’t return any value.

@FunctionalInterface
public interface Consumer<T>{
void accept(T t);
}

Predicate<T>

  • Abstract method: boolean test(T t)
  • It represents a predicate/condition, a boolean-valued function that checks a condition on an input.
  • It is used for filtering or testing elements based on a condition.

The predicate functional interface in Java takes a single argument and returns a boolean value. It is usually used in filtering values from the collection.

public interface Predicate<T>{
boolean test(T t);
}

UnaryOperator<T>

  • Abstract method: T apply(T t)
  • This is a special case of the Function interface where the input and output types are the same.
  • It is used for operations that transform an input into an output of the same type.

BinaryOperator

  • Abstract method: T apply(T t, T u)
  • It is a special case of the BiFunction interface where the two input arguments and the output are of the same type.
  • It is used for binary operations that combine two inputs into a single result.

Conclusion

We can directly implement the abstract method of the functional interface in our class using a lambda expression to make the code short and readable.

2 thoughts on “Java 8 – Functional Interface with examples”

Leave a Comment