In Java, a functional interface is an interface that has exactly one abstract method. Functional interfaces are used to represent functional concepts, and they can be implemented using lambda expressions or method references.
Functional interfaces are annotated with the @FunctionalInterface
annotation, which is a marker annotation that indicates to the compiler that the interface is intended to be a functional interface. If an interface marked with @FunctionalInterface
has more than one abstract method, the compiler will generate an error.
Functional interfaces are a key part of Java's functional programming support, which was introduced in Java 8. They provide a way to pass behavior around as if it were data, which can be very useful for things like event handling and callback functions. Some of the built-in functional interfaces in Java include Consumer
, Predicate
, and Supplier
.
Here's an example of a functional interface in Java:
csharp@FunctionalInterface
public interface MyFunctionalInterface {
void doSomething();
}
This interface has exactly one abstract method, doSomething()
. It can be implemented using a lambda expression like this:
scssMyFunctionalInterface myLambda = () -> System.out.println("Hello, world!");
myLambda.doSomething();
This will output "Hello, world!" when doSomething()
is called.
Java provides several predefined functional interfaces that can be used in various situations. These interfaces are part of the java.util.function
package and are grouped into several categories based on their purpose. Here are some of the most commonly used predefined functional interfaces in Java:
Consumer<T>: Represents an operation that accepts a single input argument of type T and returns no result. It is often used for side-effect operations such as printing or logging.
Function<T, R>: Represents a function that accepts an argument of type T and produces a result of type R. It is often used for mapping or transforming data.
Supplier<T>: Represents a supplier of results, which does not take any input argument. It is often used for lazy initialization or for generating constant values.
Predicate<T>: Represents a predicate (boolean-valued function) of one argument of type T. It is often used for filtering or selecting data.
BiFunction<T, U, R>: Represents a function that accepts two arguments of types T and U and produces a result of type R. It is often used for mapping or transforming data that involves two input arguments.
UnaryOperator<T>: Represents an operation that accepts a single input argument of type T and produces a result of the same type T. It is a special case of
Function<T, T>
.BinaryOperator<T>: Represents an operation that accepts two input arguments of type T and produces a result of type T. It is often used for reducing or combining data.
These predefined functional interfaces provide a lot of convenience and flexibility in working with functional programming constructs in Java. By using them, we can avoid writing boilerplate code and focus on the specific behavior we want to implement.
No comments:
Post a Comment