Lesson 5 of 8
Custom Functional Interfaces & Function Composition
You can define custom functional interfaces for domain-specific operations. Combined with higher-order functions (functions that take or return functions), this enables powerful, reusable, and composable code patterns.
Custom Functional Interfaces
import java.util.function.*;
// Custom functional interfaces for the Library domain
@FunctionalInterface
public interface BookFilter {
boolean test(Book book);
// Default methods for composition (like Predicate)
default BookFilter and(BookFilter other) {
return book -> this.test(book) && other.test(book);
}
default BookFilter or(BookFilter other) {
return book -> this.test(book) || other.test(book);
}
default BookFilter negate() {
return book -> !this.test(book);
}
}
@FunctionalInterface
public interface DiscountStrategy {
double apply(double price, int membershipYears);
}
// Higher-order function - takes a function, returns a function
public class FunctionComposition {
// Strategy pattern via functional interface
static final DiscountStrategy STANDARD = (price, years) -> price * 0.95; // 5%
static final DiscountStrategy PREMIUM = (price, years) -> price * (1 - years * 0.02); // +2% per year
static final DiscountStrategy NO_DISCOUNT = (price, years) -> price;
// Higher-order function: takes BookFilter, returns filtered/processed result
public static List<Book> applyFilter(List<Book> catalog, BookFilter filter) {
return catalog.stream()
.filter(filter::test)
.collect(java.util.stream.Collectors.toList());
}
// Function that RETURNS a function (closure)
public static Predicate<Book> priceBelowThreshold(double maxPrice) {
return book -> book.getPrice() < maxPrice; // maxPrice captured in closure
}
public static Function<Book, String> formattedEntry(String currency) {
return book -> String.format("[%s] %s - %s%.2f",
book.getIsbn(), book.getTitle(),
currency, book.getPrice());
}
public static void main(String[] args) {
List<Book> catalog = getCatalog();
// Compose filters with and/or
BookFilter available = Book::isAvailable;
BookFilter affordable = b -> b.getPrice() < 50;
BookFilter recommended = available.and(affordable);
List<Book> picks = applyFilter(catalog, recommended);
// Function returning function
var under40 = priceBelowThreshold(40.0);
var under60 = priceBelowThreshold(60.0);
catalog.stream().filter(under40).forEach(b -> System.out.println(b.getTitle()));
// Strategy selection
int membershipYears = 3;
DiscountStrategy strategy = membershipYears >= 5 ? PREMIUM : STANDARD;
double finalPrice = strategy.apply(49.99, membershipYears);
System.out.printf("Final price: $%.2f%n", finalPrice); // $47.49
// Closured formatter
var gbpFormatter = formattedEntry("£");
catalog.stream().map(gbpFormatter).forEach(System.out::println);
}
static List<Book> getCatalog() { return new java.util.ArrayList<>(); }
}Quick Quiz
Tip
Tip
Practice Custom Functional Interfaces Function Composition in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for Custom Functional Interfaces & Function Composition
Practice Task
Note
Practice Task - (1) Write a working example of Custom Functional Interfaces Function Composition from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Custom Functional Interfaces Function Composition is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.
Finished this lesson?