Chapter 11
Chapter 11
Chapter-11
Functional Interface
A functional interface in Java is a type of interface defined by having precisely
one abstract method. These interfaces are predominantly utilized with Lambda
Expressions and Method References, features introduced in Java 8. Functional
interfaces offer several advantages that can enhance your Java programming skills. A
functional interface can have default or static methods without affecting its
functional nature. By implementing functional interfaces, you can:
Syntax:
@FunctionalInterface // Optional but recommended
interface FunctionalInterfaceExample {
void display(); // Only ONE abstract method allowed
}
Key Features and Benefits
Here are the key features that define a functional interface:
a) Single Abstract Method: A functional interface contains exactly one abstract
method.
b) Default and Static Methods: Unlike traditional interfaces, functional interfaces can
include any number of default or static methods.
c) Compatibility with Lambda Expressions: Functional interfaces can be effortlessly
utilized with lambda expressions, method references, and constructor references.
d) Optional @FunctionalInterface Annotation: While the @FunctionalInterface
annotation is not mandatory, using it is beneficial. It acts as a tool for the compiler,
Object Oriented Programming With JAVA
ensuring that only one abstract method exists in the interface, thus enforcing the
functional interface contract.
Lambda Expression
A Lambda Expression in Java offers a streamlined way to define anonymous
functions. Introduced in Java 8, Lambda expressions simplify the coding process by removing
the necessity for anonymous classes. This advancement results in code that is not only
shorter but also more readable and efficient. Lambda expressions simplify the
implementation of functional interfaces, allowing you to write cleaner, more manageable
code. They enable you to treat functionality as an argument or pass code more dynamically.
Syntax:
(parameters) -> {body of the function}
Example:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.forEach(n -> System.out.println(n)); //In this example, n ->
System.out.println(n) is a lambda expression that iterates through the
collection, printing each number.
// Traditional method
public int add (int a, int b) {
return a + b;
}
Object Oriented Programming With JAVA
// Using Lambda Expression
(int a, int b) -> {return a + b;}
// Shorter version (no return & braces needed for single statements)
(a, b) -> a + b;
Example 1: Custom Functional Interface with Lambda
@FunctionalInterface
interface DisplayMsg {
void prints();
}
public class LambdaExample {
public static void main(String[] args) {
// Lambda expression for functional interface
DisplayMsg dm = () -> System.out.println("Hello, World!");
dm.prints();// Calling the method
}
}
Output:
Method Reference
In Java, method references offer a concise alternative to lambda expressions when
invoking existing methods. Rather than writing out a full lambda expression, you can simply
reference a method by name using the double colon (::) operator.it is introduced in Java 8
,comes embrace modern Java features like enhanced code readability and it is compatible
with functional Interfaces. Method References used because these reasons:
a) When a Lambda Expression calls a method without modifying its arguments
b) When using Streams, Collections API, and Functional Interfaces.
c) When you want cleaner and more readable code
Syntax:
ClassName::methodName
This is equivalent to
(param) -> object.method(param)
1. Static Method Reference- Instead of using a lambda expression, you can directly
reference a static method from a class.
Example:
import java.util.function.Function;
public class MyProg {
public static void main(String[] args) {
//Function<Integer,Double>squareRoot=num ->Math.sqrt(num);//Lambda Expression
//or
Function<Integer, Double> squareRoot = Math::sqrt; // Method Reference
Object Oriented Programming With JAVA
System.out.println(squareRoot.apply(25)); // Output: 5.0
}
}
Output: