Functional Interface and Lambda Expression
Functional Interface and Lambda Expression
✅ Functional Interface
A Functional Interface is an interface that has only one abstract method (but it can have
multiple default or static methods).
It can be used as the target for a lambda expression or method reference.
@FunctionalInterface
interface MyFunctionalInterface {
void sayHello(); // only one abstract method
}
✅ Lambda Expression
A Lambda Expression provides a clear and concise way to implement the abstract method of a
functional interface.
Syntax:
✅ Output:
Hello from Lambda!
✅ Output:
Addition: 8
Multiplication: 15
Java provides several built-in functional interfaces in the java.util.function package. Let's look at
two common ones:
✅ 1. Function<T, R>
Represents a function that takes one argument of type T and returns a result of type R.
🔹 Example:
import java.util.function.Function;
✅ Output:
Length of 'Hello': 5
Length of 'Java': 4
Step-by-Step Explanation:
✅ import java.util.function.Function;
This line imports the Function interface from the java.util.function package.
Function<T, R> is a generic functional interface which takes:
o T: Input type
o R: Return type
It has one abstract method:
R apply(T t);
Part Meaning
Function<String, You are creating a function that takes a String as input and returns an
Integer> Integer.
stringLength This is the name of the variable (a reference to the lambda function).
✅ stringLength.apply("Hello")
✅ System.out.println(...)
Length of 'Hello': 5
Length of 'Java': 4
✅ Final Output:
Length of 'Hello': 5
Length of 'Java': 4
🎯 Summary
Function<T, R> is a built-in functional interface to transform data from one type to another.
Lambda expressions simplify implementing functional interfaces.
In this example, the string's length is calculated and printed using Function<String,
Integer>.
✅ 2. Predicate<T>
🔹 Example:
import java.util.function.Predicate;
✅ Output:
Is 4 even? true
Is 7 even? False
This Java program demonstrates the use of the Predicate functional interface from the
java.util.function package. Let's break it down:
🔹 What is a Predicate?
🔹 Code Explanation
import java.util.function.Predicate;
🔹 Output
Is 4 even? true
Is 7 even? false
NOTE:----These built-in interfaces save you from having to define your own interfaces for common
tasks.
quick summary of the most commonly used built-in functional interfaces in Java (from
java.util.function package):
🔹 Examples:
import java.util.function.Consumer;
Output:
Hello, World
This Java program demonstrates the use of the Consumer<T> functional interface from the
java.util.function package. Let's break it down:
🔹 What is Consumer<T>?
Consumer<T> is a functional interface that takes a single input argument and returns
no result.
It's typically used to perform an action (like printing, logging, modifying) on an input.
It has a single method:
🔹 Code Breakdown
import java.util.function.Consumer;
Standard Java class and the main() method (program entry point).
print.accept("World");
Hello, World
🔹 Output
Hello, World
2. Supplier<T> – Provide a value
import java.util.function.Supplier;
Output (example):
This program demonstrates the use of the Supplier<T> functional interface from the
java.util.function package.
🔹 What is Supplier<T>?
T get();
🔹 Code Explanation
import java.util.function.Supplier;
🔹 Output (Example)
Random number: 0.7312538249035913
(The actual number will vary each time you run the program.)
🔚 Summary
✅ In this program:
import java.util.function.UnaryOperator;
Output:
Square of 5: 25
This program demonstrates how to use the UnaryOperator<T> functional interface in Java to
square a number.
🔹 What is UnaryOperator<T>?
✅ Method:
T apply(T t);
🔹 Code Explanation
import java.util.function.UnaryOperator;
Square of 5: 25
🔹 Output
Square of 5: 25
🔚 Summary
Interface Method Description
UnaryOperator<T> apply(T t) Takes one argument of type T, returns T
🔧 Use case:
You use UnaryOperator when the input and output types are the same, like:
o Squaring a number
o Negating a value
o Converting strings to lowercase
import java.util.function.BinaryOperator;
Output:
5 * 3 = 15
This program demonstrates how to use the BinaryOperator<T> functional interface in Java to
multiply two numbers.
🔹 What is BinaryOperator<T>?
✅ Method:
🔹 Code Explanation
import java.util.function.BinaryOperator;
🔹 Output
5 * 3 = 15
🔚 Summary
✅ Use cases:
4o