pag. 18

Functional Interfaces

 
/**
 * FUNCTIONAL INTERFACES
 * ---------------------
 * A functional interface is an interface with one abstract method.
 * 
 * Example:
 *  - Runnable
 *  - Comparator<T>
 *  - Predicate<T>
 *  - BinaryOperator<T>
 * 
 * @FunctionalInterface
 * public interface Predicate<T> {
 *      boolean test(T var1); // abstract
 *      ...
 * 
 * Functional interfaces are commonly used with lambdas, streams, 
 * and method references. 
 */

package com.minte9.lambdas.functional_interfaces;

import java.util.function.Function;
import java.util.function.Predicate;

public class FunctionalInterfaces {
    public static void main(String[] args) {

        // -----------------------------------------
        // Predicate using a lambda expression
        // -----------------------------------------
        Predicate<Integer> isEven = x -> x % 2 == 0;
        System.out.println(isEven.test(36));  // true

        // -------------------------
        // Function example
        // ----------------------------
        Function<Long, Long> increment = x -> x + 1;
        System.out.println(increment.apply(20L));  // 21
    }
}

Custom Functional Interface

 
/**
 * CUSTOM FUNCTIONAL INTERFACE
 * ---------------------------
 * You can define your own functional interface when:
 *   - built-in ones (Predicate, Function, etc) don't fit
 *   - you want clearer domain-specific meaning
 * 
 * A functional interface may containt:
 *   - exactly ONE abstract method
 *   - zero or more default methods
 *   - zero or more static methods
 * 
 * Only the abstract method counts toward the "functional" rule.
 * 
 * A method reference works when the referenced method
 * has the SAME signature as the functional interface method.
 */

package com.minte9.lambdas.functional_interfaces;

public class CustomFunctionalInterface {
    public static void main(String[] args) {

        MyFuncInterface exclaim = s -> s + "!";
        MyFuncInterface question = s -> s + "?";

        System.out.println(exclaim.run("Hello World"));  // Hello World!
        System.out.println(question.run("Java Lambdas"));  // Java Lambdas?

        System.out.println(exclaim.describe());  // MyFuncInterface: transforms a String

    }
}

// Custom functional interface
@FunctionalInterface
abstract interface MyFuncInterface {

    // Single abstract method (SAM)
    String run(String s);

    // Default method (does NOT count as abstract)
    default String describe() {
        return "MyFuncInterface: transforms a String";
    }


}




References: