Answer
Functional Interface in Java 8
A functional interface is an interface with exactly ONE abstract method. It is the foundation of lambda expressions in Java 8.
Java
@FunctionalInterface // optional but recommended — compiler enforces 1 method
public interface Greeting {
void greet(String name); // single abstract method
// void anotherMethod(); // ❌ would make it non-functional
}
// Lambda implements the interface
Greeting g = name -> System.out.println("Hello, " + name + "!");
g.greet("Alice"); // "Hello, Alice!"
Built-in Functional Interfaces (java.util.function)
Predicate — takes T, returns boolean
Java
Predicate<String> isLong = s -> s.length() > 5;
Predicate<Integer> isEven = n -> n % 2 == 0;
System.out.println(isLong.test("Hello")); // false
System.out.println(isLong.test("Welcome")); // true
// Combine predicates
Predicate<Integer> isPositiveEven = isEven.and(n -> n > 0);
Predicate<String> isLongOrEmpty = isLong.or(String::isEmpty);
Predicate<Integer> isOdd = isEven.negate();
Function<T, R> — takes T, returns R
Java
Function<String, Integer> strLen = String::length;
Function<Integer, String> intToStr = n -> "Number: " + n;
System.out.println(strLen.apply("Hello")); // 5
System.out.println(intToStr.apply(42)); // "Number: 42"
// Chain functions
Function<String, String> upper = String::toUpperCase;
Function<String, String> exclaim = s -> s + "!";
Function<String, String> shout = upper.andThen(exclaim);
System.out.println(shout.apply("hello")); // "HELLO!"
Consumer — takes T, returns nothing
Java
Consumer<String> print = System.out::println;
Consumer<String> logInfo = msg -> System.out.println("[INFO] " + msg);
print.accept("Test passed");
logInfo.accept("Login successful");
// andThen — chain consumers
Consumer<String> printAndLog = print.andThen(logInfo);
printAndLog.accept("Hello"); // prints then logs
Supplier — no input, returns T
Java
Supplier<String> greeting = () -> "Good morning!";
Supplier<Double> randomNum = Math::random;
Supplier<List<String>> newList = ArrayList::new;
System.out.println(greeting.get()); // "Good morning!"
System.out.println(randomNum.get()); // 0.7823...
BiFunction<T, U, R> — takes two inputs, returns R
Java
BiFunction<String, Integer, String> repeat = (s, n) -> s.repeat(n);
System.out.println(repeat.apply("Ha", 3)); // "HaHaHa"
Custom Functional Interface
Java
@FunctionalInterface
public interface TriFunction<A, B, C, R> {
R apply(A a, B b, C c);
}
TriFunction<Integer, Integer, Integer, Integer> sum = (a, b, c) -> a + b + c;
System.out.println(sum.apply(1, 2, 3)); // 6
In Automation Testing
Java
// Define custom functional interface for wait conditions
@FunctionalInterface
interface PageCondition {
boolean isMet(WebDriver driver);
}
public void waitFor(PageCondition condition) {
new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(10))
.pollingEvery(Duration.ofMillis(500))
.until(condition::isMet);
}
// Usage
waitFor(driver -> driver.findElement(By.id("result")).isDisplayed());
waitFor(driver -> driver.getTitle().equals("Dashboard"));
