In this article, we will discuss the functional interface in java 8. How to use the functional interface in Java 8.
Here is the table content of the article will we will cover this topic.
1. What is the functional interface in java 8?
2. What is the use of @FunctionalInterface annotation?
3. Important points about the functional interface?
4. What is Predicate in java 8?
5. Why predicate in java 8?
6. What is Consumer in Java 8?
7. Why consumer in java 8?
8. Supplier interface in Java?
9. Function interface in Java 8?
What is the functional interface in java 8?
An interface that can contain only one abstract method/function is known as a functional interface. The term Java functional interface was introduced in Java 8. From Java 8 onwards, you can ensure that your interface is functional or not. To ensure the functional interface you should add the @FunctionalInterface annotation. Runnable and Comparable are the most common examples of functional interfaces. You can check their source code. These interfaces are also called Single Abstract Method interfaces (SAM Interfaces). In java, there are some existing interfaces Runnable, Comparable.
In Java 8, functional interfaces can be represented using lambda expressions, method references, and constructor references as well. We will discuss it later.
What is the use of @FunctionalInterface annotation?
This notation ensures that your interface should contain only one abstract method. This annotation is not necessary for the creation of a functional interface. It’s not mandatory so the programmer can ignore it.
Without annotation: If the programmer does not specify annotation then the interface can contain any number of abstract methods. If the interface contains only an abstract method, then it considers a functional interface otherwise not.
Example of functional interface without annotation: interface ExampleOfFunctionInterface { public void firstMethod(); }
With annotation: If the programmer specifies annotation then the interface can contain only one abstract method. If the programmer specifies more than one abstract method, then the compiler shows the error message “Invalid ‘@FunctionalInterface’ annotation”.
@FunctionalInterface interface ExampleOfFunctionInterface { public void firstMethod(); }
Important points about the functional interface
1. In the functional interface, only one abstract method is allowed. If the programmer tries to add any other abstract method, then the compiler will show errors. The programmer must remove the @FunctionInterface annotation to add another abstract method, but it will make the interface a non-functional interface.
If the programmer specifies more than one abstract method then an error will appear:
2. You can create a functional interface without @FunctionalInterface annotation. This annotation is only for informing the compiler to enforce a single abstract method inside the interface.
3. From Java 8 onwards, the interface can contain the default method. In the interface default method has an implementation, they are not abstract.
Conceptually, a functional interface can contain only one abstract method. Since the default method is not abstract, So, you can add the default method also in the functional interface.
@FunctionalInterface interface ExampleOfFunctionInterface { public void firstMethod(); default void secondMethod() { System.out.println("It is a default method"); } }
4. You can’t inherit any functional interface to another functional interface. Because it breaks the law of functional interface has exactly one abstract method.
@FunctionalInterface interface ExampleOfFunctionInterface { public void firstMethod(); } @FunctionalInterface interface ExampleOfAnotherFunctionInterface extends ExampleOfFunctionInterface { public void secondMethod(); }
The compiler will show the error “Invalid ‘@FunctionalInterface’ annotation; ExampleOfAnotherFunctionInterface is not a functional interface”
5. You can override the methods of the Objects class by declaring them as abstract. The
compiler does not count toward the interface’s abstract method count.
@FunctionalInterface interface ExampleOfFunctionInterface { public void firstMethod(); @Override public String toString(); }
Java 8 provides a lot of functional interfaces. These interfaces are used in lambda expressions. All the interfaces are defined in java.util.function package.
- BiConsumer<T,U>: This interface has one method that performs an operation that accepts two input arguments and returns no result.??
- BiFunction<T,U,R>: This interface has one method that performs an operation and accepts two input arguments, and returns the result of the function.??
- BinaryOperator<T>: This interface performs the operation on two operands of same type. It returns the result of the same type.
- BiPredicate<T,U>: This interface has one method that checks the given input match with predicte or not.
- BooleanSupplier: This interface represents a boolean supplier. This interface has a method that returns boolean.
- Consumer<T>: This interface accepts the input but doesn’t produce output. There is one method that accepts only one argument.
- DoubleBinaryOperator: This interface accepts the input of double type and returns the result of the same type.
- DoubleConsumer: This interface accepts the input of double type but doesn’t produce output. There is one method that accepts only one argument.
- DoubleFunction<R>: This interface has one method that performs an operation and accepts one argument of double type and returns result.??
- DoublePredicate: This interface has one method that checks the given input of double type is matches with predicte or not.
- DoubleSupplier: This interface represents double supplier. This interface has a method that returns double.
- DoubleToIntFunction: This interface takes double as input and produces int value as output.
- DoubleToLongFunction: This interface takes long as the input and produces double value as output.
- DoubleUnaryOperator: This interface takes operand as input and produces the result.
- Function<T,R>: This interface has one method that performs an operation and accepts one input argument and returns the result of the function.
- IntBinaryOperator: This interface takes operand as input and produces the result of int type.
- IntConsumer: This interface accepts the input of int type but doesn’t produce output. There is one method that accepts only one argument.
- IntFunction<R>: This interface has one method that performs an operation and accepts one argument of int type and returns result.??
- IntPredicate: This interface has one method that checks the given input of int type is match with predicte or not.
- IntSupplier: This interface represents int supplier. This interface has a method that returns an int.
- IntToDoubleFunction: This interface takes an int as input and produces double value as output.
- IntToLongFunction: This interface takes an int as input and produces long value as output.
- IntUnaryOperator: This interface takes int operand as input and produces the result of the input type.
- LongBinaryOperator: This interface takes operand as input and produces the result of a long type.
- LongConsumer: This interface accepts the input of a long type but doesn’t produce output. There is one method that accepts only one argument.
- LongFunction<R>: This interface has one method that performs an operation and accepts one argument of long type and returns result.??
- LongPredicate: This interface represents a long predicate. This interface has a method that returns Long.
- LongSupplier: This interface represents a long supplier. This interface has a method that returns an int.
- LongToDoubleFunction: This interface takes long as the input and produces double value as output.
- LongToIntFunction: This interface takes long as the input and produces int value as output.
- LongUnaryOperator: This interface takes operand of long type as input and produces the result of the long type.
- ObjDoubleConsumer<T>: This interface accepts two inputs. The first input is the specified type, and another is a double type but doesn’t produce output. There is one method that accepts two arguments.
- ObjIntConsumer<T>: This interface accepts the input of the specified type and int type but doesn’t produce output.
- ObjLongConsumer<T>: This interface accepts the input of the specified type and long type but doesn’t produce output.
- Predicate<T>: This interface is used to test the given argument is matched with predict.
- Supplier<T>: This interface supplies the object of specified for Supplier.
- ToDoubleBiFunction<T,U>: This interface returns the result as a double data type. It applies the function of two arguments.
- ToDoubleFunction<T>: This interface returns the result as a double data type. It applies the function of one argument.
- ToIntBiFunction<T,U>: This interface returns the result as the int data type. It applies the function on two arguments.
- ToIntFunction<T>: This interface returns the result as the int data type. It applies the function of one argument.
- ToLongBiFunction<T,U>: This interface returns the result as a long data type. It applies the function of two arguments.
- ToLongFunction<T>: This interface returns the result as a long data type. It applies the function of one argument.
- UnaryOperator<T>: This interface performs an operation on a single operand that produces a result of the
same type as its operand.