Introduction
In the last article, while implementing lambda expressions we came across the word functional interfaces and this article explains what, why, and implementation of functional interfaces in our production code.
Functional Interface was introduced with Java 8, which is an interface that has only a single abstract method. Multi abstract methods in the same functional interface will return in error. Conversely, if you have an interface that has only a single abstract method, then that will effectively be a functional interface and can be used anywhere where a functional interface is eligible to be used.
This is present under the java.util.Function package and includes something around 40+ functional interfaces which can be directly implemented in your code. Some functional interfaces are used repeatedly, which are shown below:

Fig 1 : Functional Interfaces
Each functional interface has a single abstract method, called functional method. For that functional interface, implementation can be passed around lambda expressions and method references.
@functionalInterface Annotation
For any interface, to be a functional interface we use an annotation i.e @functionalInterface.
Functional Interface annotation can be used to explicitly specify that a given interface is to be treated as a functional interface, then the compiler will check and give a compile-time error in case the annotated interface doesn’t satisfy the basic condition of qualifying as a functional Interface.
Here, each interface is explained one by one below:
Consumer Functional Interface
Fig 1.1 : Different Consumer Functional Interfaces
Consumer<T> is an inbuilt functional interface introduced in java 8 in the java.util.Function package, where T is the type of input to the operation. This is a functional interface that can be used with a lambda expression and method reference.
This interface can be used in all contexts where an object needs to be consumed i.e it takes an input, and some operation is performed on the object without returning any result.
- @FunctionalInterface
- public interface Consumer<T> {
- void accept(T t);
- }
Where accept() method is the primary abstract method of consumer interface, its functional descriptor T->() i.e accept() method takes an input type of T and returns no value.
All other consumers functional interface differ in a way or other i.e,
BiConsumer<T,U> functional interface, here T, U is the type of the input to the operation, and represents an operation that accepts two input arguments and returns no result.
DoubleConsumer, IntConsumer andLongConsumer functional interface, these all are similar to some extent as represents an operation that accepts a single double/int/ long-valued argument and returns no result.
ObjIntConsumer<T>, ObjLongConsumer<T>, ObjDoubleConsumer<T> functional interfaces, these all functional interfaces are the same and only differ in their return type value of the method. This represents an operation which accepts an object valued and an int/long/double - valued argument and returns no result.
Example
- package Functional_Interface;
- import java.util.Arrays;
- import java.util.List;
- import java.util.function.Consumer;
- public class ConsumerDemo {
- public static void main(String[] args) {
- List < String > al = Arrays.asList("abcba", "asffsa", "mnbvcvbnm", "klnff", "fweffqqewf", "dwfew", "ffwf", "dsvsd");
- Consumer < List < String >> cons = list - > {
- for (int i = 0; i < al.size(); i++) {
- StringBuffer sb = new StringBuffer(al.get(i));
- al.set(i, sb.reverse().toString());
- }
- };
- cons.accept(al); //consumer interface function
- // Using Method Reference to print elements
- al.forEach(System.out::println);
- }
- }
Predicate Functional Interface

Fig 1.2 Different Predicate Functional Interfaces
Predicate<T> is an inbuilt functional interface introduced in java 8 in the java.util.Function package, where T is the type of input to the operation. This is a functional interface that can be used with a lambda expression and method reference.
This interface can be used where an object needs to be evaluated for a given test condition anda boolean value needs to be returned based on whether a condition is met or not. The user need not to define his/her own predicate-type functional interface.
- @FunctionalInterface
- public interface Predicate < T > {
- boolean test(T t);
- }
Where test() method is the primary abstract method of predicate interface, its functional descriptor T->() i.e test() method takes an input type of T and returns a boolean result based on whether a condition is met or not.
All other predicate functional interfaces differ in a way or other i.e,



Roshan RathodPosted Jul 23, 2020, 1:06 AM
Nice..keep it up
ankurr joshiPosted Jul 21, 2020, 6:43 AM
Nicely explained
Pranshu SinghalPosted Jul 21, 2020, 6:18 AM
Nicely Explained