Functional Interfaces in Java
Definition:
A Functional Interface is an interface that contains only one abstract method but can have multiple default or static methods. It is primarily used to represent lambda expressions, enabling a clear and concise way to represent functionality as an argument to methods. Java introduced functional interfaces in Java 8 as part of its functional programming capabilities.
Key Characteristics:
- It contains exactly one abstract method.
- Can have multiple default and static methods.
- Can be used with lambda expressions and method references.
- @FunctionalInterface annotation can be used to ensure that the interface meets the functional interface requirements.
Commonly Used Functional Interfaces:
- Runnable: represents an action that takes no arguments and returns no result.
- Comparator<T>: used to compare two objects of type T.
- Supplier<T>: represents a supplier of results with no input and a return value.
- Consumer<T>: represents an operation that takes a single input argument and returns no result.
- Function<T, R>: represents a function that takes one argument and produces a result.
- Predicate<T>: represents a boolean-valued function of one argument.
Syntax:
@FunctionalInterface
interface MyFunctionalInterface {
// Abstract method
void myAbstractMethod();
// Default method
default void myDefaultMethod() {
System.out.println(“This is a default method in a functional interface.”);
}
// Static method
static void myStaticMethod() {
System.out.println(“This is a static method in a functional interface.”);
}
}
Example:
1. Custom Functional Interface Example with Lambda Expression:
@FunctionalInterface
interface Calculator {
int calculate(int a, int b);
}
public class Main {
public static void main(String[] args) {
// Lambda expression to define the calculate method
Calculator add = (a, b) -> a + b;
Calculator subtract = (a, b) -> a – b;
System.out.println(“Addition: ” + add.calculate(5, 3)); // Output: 8
System.out.println(“Subtraction: ” + subtract.calculate(5, 3)); // Output: 2
}
}
2. Built-in Functional Interface Example (Predicate):
public class Main {
public static void main(String[] args) {
// Predicate to check if a number is greater than 10
Predicate<Integer> isGreaterThanTen = number -> number > 10;
System.out.println(isGreaterThanTen.test(15)); // Output: true
System.out.println(isGreaterThanTen.test(8)); // Output: false
}
}
3. Built-in Functional Interface Example (Function):
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
// Function to get the length of a string
Function<String, Integer> stringLength = str -> str.length();
System.out.println(stringLength.apply(“Hello”)); // Output: 5
}
}
Important Built-In Functional Interfaces in Java:
Runnable:
Runnable runnable = () -> System.out.println(“Running a thread”);
new Thread(runnable).start(); // Output: Running a thread
1. Consumer<T>:
import java.util.function.Consumer;
Consumer<String> printer = s -> System.out.println(s);
printer.accept(“Hello, World!”); // Output: Hello, World!
2. Supplier<T>:
import java.util.function.Supplier;
Supplier<String> supplier = () -> “Hello from Supplier”;
System.out.println(supplier.get()); // Output: Hello from Supplier
3. Predicate<T>:
import java.util.function.Predicate;
Predicate<String> isEmpty = s -> s.isEmpty();
System.out.println(isEmpty.test(“”)); // Output: true
4. Function<T, R>:
import java.util.function.Function;
Function<Integer, String> intToString = i -> “Number: ” + i;
System.out.println(intToString.apply(5)); // Output: Number: 5
Tasks:
Question 1:
Create a functional interface MathOperation that defines a method for two integers. Implement this interface using a lambda expression to perform addition, subtraction, multiplication, and division.
Expected Output:
MathOperation add = (a, b) -> a + b;
MathOperation subtract = (a, b) -> a – b;
System.out.println(add.operate(10, 5)); // Output: 15
System.out.println(subtract.operate(10, 5)); // Output: 5
Question 2:
Implement the Predicate<T> functional interface to check if a given integer is even. Write a program that uses the predicate to check multiple numbers and print the results.
Question 3:
Write a program using the Function<T, R> functional interface to convert a list of integers into their corresponding string representations (e.g., Function<Integer, String>).
Question 4:
Create a custom functional interface StringOperation that defines a method to reverse a string. Implement this interface using a lambda expression and apply it to multiple strings.
Question 5:
Use the Consumer<T> functional interface to create a program that takes a list of integers and prints each element squared.
Course Video
YouTube Reference :
The training covers Java functional interfaces, including their concepts, syntax, and practical applications.
Yes, it includes examples demonstrating how to use built-in functional interfaces like Function
, Consumer
, and Predicate
, as well as how to create custom functional interfaces.
Yes, exercises involve implementing functional interfaces using lambda expressions to perform various tasks, such as reversing strings and processing lists of integers.
Yes, it’s designed for beginners with clear explanations and step-by-step examples.
Yes, it covers how functional interfaces enable functional programming in Java, leading to more concise and flexible code.
Yes, the training is self-paced and accessible online anytime.
Yes, it explains how lambda expressions can be used to instantiate functional interfaces, providing a more streamlined syntax for implementing single-method interfaces.
Yes, this Java Functional Interfaces training is completely free on Iqra Technology Academy’s website.