Java Functional Interfaces and Method References
Introduction
Java 8 introduced functional programming concepts through Functional Interfaces and Method References. These features enable concise, expressive, and reusable code, forming the backbone of modern Java development.
Functional Interfaces
Definition
A functional interface is an interface with exactly one abstract method. It can have multiple default or static methods. They are the foundation of lambda expressions.
Examples of Built-in Functional Interfaces
Predicate<T>– Tests a condition and returns boolean.Function<T,R>– Takes input T and returns result R.Consumer<T>– Accepts input T and performs an action.Supplier<T>– Supplies a value of type T.
Code Examples
// Predicate example
Predicate isEven = n -> n % 2 == 0;
System.out.println(isEven.test(4)); // true
// Function example
Function length = s -> s.length();
System.out.println(length.apply("Hello")); // 5
// Consumer example
Consumer printer = s -> System.out.println(s);
printer.accept("Printing with Consumer");
// Supplier example
Supplier randomSupplier = () -> Math.random();
System.out.println(randomSupplier.get());
Custom Functional Interfaces
@FunctionalInterface
interface Calculator {
int calculate(int a, int b);
}
Calculator add = (a, b) -> a + b;
System.out.println(add.calculate(5, 3)); // 8
Method References
Definition
Method references are shorthand for lambda expressions that call existing methods. They improve readability and reduce boilerplate.
Types of Method References
- Reference to a static method:
ClassName::staticMethod - Reference to an instance method:
object::instanceMethod - Reference to a constructor:
ClassName::new
Examples
// Static method reference
Function parser = Integer::parseInt;
System.out.println(parser.apply("123")); // 123
// Instance method reference
String str = "hello";
Supplier supplier = str::toUpperCase;
System.out.println(supplier.get()); // HELLO
// Constructor reference
Supplier> listSupplier = ArrayList::new;
List list = listSupplier.get();
list.add("Java");
System.out.println(list);
Advanced Usage
- Combining functional interfaces with streams.
- Using method references in collectors.
- Chaining functions with
andThen()andcompose().
Performance Analysis
Functional interfaces and method references improve readability and maintainability. While lambdas and references introduce minor overhead, the benefits in clarity and reduced boilerplate outweigh performance costs.
Interview-Ready Notes
- Q: What is a functional interface?
A: An interface with exactly one abstract method, used with lambdas. - Q: Difference between lambda and method reference?
A: Lambdas define behavior inline; method references reuse existing methods. - Q: Why use functional interfaces?
A: They enable functional programming and concise code. - Q: Example of built-in functional interfaces?
A: Predicate, Function, Consumer, Supplier.
Conclusion
Functional Interfaces and Method References are cornerstones of Java’s functional programming paradigm. Mastering them enables developers to write cleaner, more expressive, and modern Java code. In interviews, emphasize their definitions, examples, and practical use cases.