Major 4 types of Functional Interface in Java
Published on August 16, 2024
Exploring Java Functional Interfaces: An Overview with Examples
Functional interfaces in Java are a cornerstone of functional programming, introduced with Java 8. They allow you to pass behavior as parameters, making your code more flexible and expressive. There are four primary types of functional interfaces provided by Java's java.util.function
package:
Function<T, R>
: Represents a function that takes an argument of typeT
and produces a result of typeR
.Predicate<T>
: Represents a predicate (boolean-valued function) that takes an argument of typeT
and returns a boolean.Consumer<T>
: Represents an operation that takes an argument of typeT
and returns no result.Supplier<T>
: Represents a supplier of results that returns an instance of typeT
without taking any input.
Let's dive into each type with examples:
1. Function<T, R>
The Function<T, R>
interface represents a function that takes a single argument of type T
and returns a result of type R
.
Example:
import java.util.function.Function;
public class FunctionExample {
public static void main(String[] args) {
// Function that converts a string to its length
Function<String, Integer> stringLengthFunction = str -> str.length();
String testString = "Hello, World!";
Integer length = stringLengthFunction.apply(testString);
System.out.println("Length of the string: " + length);
}
}
Explanation:
stringLengthFunction
is aFunction
that takes aString
and returns its length as anInteger
.
2. Predicate<T>
The Predicate<T>
interface represents a predicate (boolean-valued function) that takes an argument of type T
and returns a boolean
.
Example:
import java.util.function.Predicate;
public class PredicateExample {
public static void main(String[] args) {
// Predicate that checks if a number is even
Predicate<Integer> isEvenPredicate = number -> number % 2 == 0;
Integer testNumber = 10;
boolean isEven = isEvenPredicate.test(testNumber);
System.out.println("Is the number even? " + isEven);
}
}
Explanation:
isEvenPredicate
is aPredicate
that checks if a givenInteger
is even.
3. Consumer<T>
The Consumer<T>
interface represents an operation that takes a single argument of type T
and returns no result. It performs an action without returning any value.
Example:
import java.util.function.Consumer;
public class ConsumerExample {
public static void main(String[] args) {
// Consumer that prints a string
Consumer<String> printConsumer = str -> System.out.println(str);
String message = "Hello, World!";
printConsumer.accept(message);
}
}
Explanation:
printConsumer
is aConsumer
that takes aString
and prints it to the console.
4. Supplier<T>
The Supplier<T>
interface represents a supplier of results. It does not take any input and provides a result of type T
.
Example:
import java.util.function.Supplier;
public class SupplierExample {
public static void main(String[] args) {
// Supplier that provides a random number
Supplier<Double> randomNumberSupplier = () -> Math.random();
Double randomNumber = randomNumberSupplier.get();
System.out.println("Random number: " + randomNumber);
}
}
Explanation:
randomNumberSupplier
is aSupplier
that generates and returns a randomDouble
value.
Conclusion
Functional interfaces in Java provide a powerful way to pass around behavior and create more flexible and reusable code. By understanding and utilizing Function
, Predicate
, Consumer
, and Supplier
, you can write cleaner, more expressive, and functional code. Each of these interfaces plays a distinct role and is used in different scenarios to handle functions, predicates, actions, and results.