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.
Functional Interfaces in a class can be used in two ways:
@FunctionalInterface assures that a functional interface has just one abstract method; otherwise, it generates a compiler error. However, its use is optional.
There are 4 major types of functional interfaces:
There are 2 types of Consumer 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.
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.
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.
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.