Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now

Functional Interfaces in Java

Level : Intermediate
Mentor: Shailendra Chauhan
Duration : 00:02:00

What are Functional Interfaces in Java?

A functional interface, often known as a Single Abstract Method (SAM) interface, can include only one abstract method. The @FunctionalInterface annotation identifies these interfaces, which can have any number of default and static methods, as well as methods declared by the object class.

Ways to use Functional Interfaces in a class

Functional Interfaces in a class can be used in two ways:

  1. Using Lambda Expressions
  2. By implementing the interface

@FunctionalInterface Annotation

@FunctionalInterface assures that a functional interface has just one abstract method; otherwise, it generates a compiler error. However, its use is optional.

Built-in Java Functional Interfaces

  • Runnable: This interface just has the run() function.
  • Comparable: This interface just has a compareTo() function.
  • ActionListener: This interface just has the actionPerformed() function.
  • Callable: This interface only has a call() function.

Types of Functional Interfaces

There are 4 major types of functional interfaces:

  1. Consumer
  2. Supplier
  3. Function
  4. Predicate

1. Consumer Interface

The functional interface's consumer interface accepts only one argument, often known as a gentrified argument. The consumer interface provides no return value. 

Types of Consumer Interface

There are 2 types of Consumer Interface:

  1. BiConsumer
  2. intConsumer

2. Supplier Interface

In Java, the supplier functional interface is similar to a functional interface but does not accept arguments. When you call the supplier, it just returns a value. Supplier is a generic interface that uses Angular brackets to specify the type of value returned by the get() method.

3. Function Interface

The function type functional interface accepts one argument, processes it, and returns a value. One application of this type of functional interface is to take the user's key as input and search for the value in the map for that key.

4. Predicate Interface

In Java, the predicate functional interface converts a single parameter into a boolean value. It is typically used to filter values from a collection. In Java, the predicate interface has three default methods: and(), negate(), and or, as well as one static method isEqual(). The abstract method test is not included.

Unary Operator and Binary Operator

There are also two other functional interfaces: Unary Operator and Binary Operator. They both expand the Function and the Bi-Function. In simple terms, the Unary Operator extends Function, while the Binary Operator extends Bi-Function. 

Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this