Sitemap
hansini gunasena

hansini gunasena

Functional Interfaces

3 min readJul 27, 2025

--

Functional Interfaces

A functional interface is an interface that has exactly one abstract method. It is used primarily with lambda expressions and method references in Java. It can have one or more static and default methods.

Key Points:

  • Introduced in Java 8 for functional programming.
  • Used as targets for lambda expressions.

Can have:

  • One abstract method
  • Any number of default or static methods
  • Annotated with @FunctionalInterface (optional but recommended)

Runnable, ActionalListner, and Comparator are common examples of Java functional interfaces. From Java 8 onwards, lambda expressions and method references can be used to represent the instance of a functional interface.

Built-In Java Functional Interfaces

Since Java SE 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.

Types of Functional Interfaces in Java

Java SE 8 included four main kinds of functional interfaces which can be applied in multiple situations as mentioned below:

  1. Consumer — No result
  2. Predicate — Give a Boolean result.
  3. Function — input and output
  4. Supplier — no input only supply

1. Consumer

Consumer<T> is a functional interface that represents an operation that accepts a single input argument and returns no result.

It is generally used for performing side effects, like printing, logging, updating values, etc

2. Predicate

Get hansini gunasena’s stories in your inbox

Join Medium for free to get updates from this writer.

Predicate<T> is a functional interface in Java 8 used to represent a boolean-valued function of one argument. It’s often used in filtering, condition checks, etc.

3. Function

🔹 Definition

Function<T, R> is a functional interface that represents a function that:

  • Takes one input (T)
  • Returns a result (R)
Press enter or click to view image in full size
  • Bi-Function: The Bi-Function is substantially related to a Function. Besides, it takes two arguments, whereas Function accepts one argument.
  • Unary Operator and Binary Operator: There are also two other functional interfaces which are named as Unary Operator and Binary Operator. They both extend the Function and Bi-Function respectively, where both the input type and the output type are same.

4. Supplier

Supplier<T> is a functional interface that represents a function with no input but produces a result (T).

why Functional Interfaces?

To write cleaner, concise, and functional-style code
To pass behavior (logic) as a parameter
Enables higher-order functions (functions taking functions)

Sources : https://www.geeksforgeeks.org/java/java-functional-interfaces/

--

--