What are functional interfaces in Java 8?
What are functional interfaces in Java 8?
Functional interfaces in Java 8 are a type of interface that contains a single abstract method (SAM). A SAM is an abstract method that does not have any implementation, meaning it has to be implemented by its subclasses or classes that implement the interface.
A functional interface can be used with lambda expressions to create objects that implement the interface. This allows developers to write concise and expressive code that uses functions as first-class citizens.
Here are some key characteristics of functional interfaces:
Single Abstract Method (SAM): The most important characteristic is that it should contain a single abstract method that does not have any implementation. No default methods: A functional interface cannot have any default methods or static methods. Exactly one abstract method: It must have exactly one abstract method, which means no more and no less.Examples of built-in functional interfaces in Java 8 include:
Runnable
: This is a SAM that has an empty implementation. It's used for running a piece of code without returning any value. Callable<T>
: This is a SAM that returns a value of type T
. It's used for executing a piece of code that returns a value. Predicate<T>
: This is a SAM that takes in one argument and returns a boolean value indicating whether the predicate is true or false. It's often used to filter data based on a certain condition. Function<T, R>
: This is a SAM that takes in one argument of type T
and returns a value of type R
. It's often used for mapping data from one type to another.
You can create your own functional interfaces by defining a simple interface with a single abstract method. For example:
@FunctionalInterface
public interface MyFunction {
int add(int x, int y);
}
With this interface, you can use lambda expressions to create objects that implement the MyFunction
interface:
MyFunction myFunction = (x, y) -> x + y;
int result = myFunction.add(2, 3); // returns 5
Functional interfaces are a powerful feature in Java 8 that allows developers to write more concise and expressive code. They can be used for a wide range of purposes, from simple tasks like running code or filtering data to complex tasks like processing streams or building reactive systems.
In conclusion, functional interfaces in Java 8 are a type of interface that contains a single abstract method (SAM). They can be used with lambda expressions to create objects that implement the interface. This allows developers to write concise and expressive code that uses functions as first-class citizens.
Types of functional interfaces in Java 8
Java 8 introduced a new type of interfaces called Functional Interfaces. These interfaces are used to represent functions that can be executed using Lambda expressions or Method References.
A functional interface is an interface that has exactly one abstract method (also known as a single abstract method, or SAM). This abstract method can take any number of parameters and return any type of value.
Here are some key characteristics of functional interfaces:
Single Abstract Method: A functional interface must have only one abstract method. If the interface has multiple abstract methods, it is not considered a functional interface. Abstract Method Signature: The abstract method signature determines whether two functional interfaces are considered equivalent. This means that even if two interfaces have different names but identical method signatures, they can be considered equivalent as functional interfaces. Functional Interface Types: There are several built-in functional interface types in Java 8, including:java.lang.Predicate<T>
: A predicate is a function that takes one input parameter and returns a boolean value indicating whether the input meets certain criteria. java.util.function.Function<T,R>
: A function is a general-purpose functional interface that takes one input parameter and returns an output result. java.util.function.UnaryOperator<T>
: An unary operator is a function that takes one input parameter and returns an output result. Unlike the Function interface, the output result must be of the same type as the input parameter. java.util.function.BinaryOperator<T>
: A binary operator is a function that takes two input parameters and returns an output result. java.util.function.Consumer<T>
: A consumer is a function that takes one input parameter and performs some action on it, without returning any value. java.util.function.Supplier<T>
: A supplier is a function that provides a default value or returns the result of a computation.
Here are some examples of using functional interfaces:
// Using Predicate
List strings = Arrays.asList("hello", "world", "lambda");
Predicate predicate = s -> s.length() > 5;
strings.stream()
.filter(predicate)
.forEach(System.out::println);
// Using Function
Function<Integer, String> function = i -> Integer.toString(i);
String result = function.apply(10); // Output: "10"
// Using Consumer
Consumer consumer = System.out::println;
consumer.accept("Hello, World!");
// Using Supplier
Supplier supplier = () -> "Hello, World!";
System.out.println(supplier.get()); // Output: "Hello, World!"
In summary, functional interfaces are a new type of interface introduced in Java 8. They are used to represent functions that can be executed using Lambda expressions or Method References. The key characteristic of a functional interface is that it has exactly one abstract method, and its signature determines whether two functional interfaces are considered equivalent.