FunctionInterface

FunctionalInterface 是 Java 8 引入的一個新特性,它是一個接口,通常用於表示只有一個抽象方法的接口。Java 8 引入了函數式編程的概念,並且支持使用 Lambda 表達式來實現函數式接口的抽象方法。

以下是對 FunctionalInterface 的簡要說明:

  • 用途FunctionalInterface 用於聲明函數式接口,即只有一個抽象方法的接口。它可以幫助我們更清楚地標識出一個接口是否是用於函數式編程。

  • 特點FunctionalInterface 接口只允許有一個抽象方法,但可以有多個默認方法或靜態方法。如果一個接口聲明為 @FunctionalInterface,但實際上包含多於一個抽象方法,編譯器將報錯。

  • Lambda 表達式FunctionalInterface 配合 Lambda 表達式可讓我們更方便地實現函數式接口的抽象方法。使用 Lambda 表達式,我們可以將一段代碼塊傳遞給接口的抽象方法,實現更簡潔的代碼。

  • 內置函數式接口:Java 8 提供了許多內置的函數式接口,例如 ConsumerPredicateFunction 等,它們都是 FunctionalInterface 的實例。這些接口可用於處理集合、過濾數據、映射轉換等常見的操作。

總之,FunctionalInterface 是 Java 8 引入的一個接口,它的主要作用是標識一個接口是否是函數式接口,並且配合 Lambda 表達式實現函數式編程。這使得代碼更具可讀性和簡潔性,並支持更優雅的函數式風格的編程。

簡單測試一下

FunctionInterfaceTest
import com.caster.test.enums.CommonOnOffStatus;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.function.*;

public class FunctionInterfaceTest {

    // 預設的 DemoBean 整數檢查 Predicate
    static Predicate<DemoBean> checkDemoBeanInteger = (o -> o.getInteger() >= 10);

    // 預設的 DemoBean 類型檢查 Predicate
    static Predicate<DemoBean> checkDemoBeanType = (o -> StringUtils.equalsIgnoreCase(o.getType(), "C"));

    // 預設的轉換 String 到 Integer 的 Function
    Function<String, Integer> defaultFunction = Integer::parseInt;

    // 預設的 DemoBean 整數印出 Consumer
    static Consumer<DemoBean> defaultConsumerFun = (o -> System.out.println(o.getInteger()));

    // 預設的 DemoBean 日期印出 Consumer
    static Consumer<DemoBean> defaultConsumerFun_2 = (o -> System.out.println(o.getDate()));

    // 預設的 DemoBean 類型印出 Consumer
    static Consumer<DemoBean> defaultConsumerFun_3 = (o -> System.out.println(o.getType()));

    // 預設的 DemoBean 供應者 Supplier
    static Supplier<DemoBean> defaultSupplierFun = (() -> new DemoBean().setDate(new Date(System.currentTimeMillis() - 5896L))
            .setType("B")
            .setInteger(100)
            .setBigDecimal(BigDecimal.valueOf(520L))
            .setStatus(CommonOnOffStatus.OFF));

    // 預設的 DemoBean 單元操作符 UnaryOperator
    static UnaryOperator<DemoBean> defaultUnaryOperatorFun = (o -> o.setBigDecimal(BigDecimal.valueOf(777L)));

    // 預設的 DemoBean 雙引數函數 BiFunction
    static BiFunction<DemoBean, Boolean, String> defaultBiFunction = (DemoBean::printAddress);

    // 預設的 DemoBean 轉換 Function
    static Function<DemoBean, String> defaultFunction_1 = DemoBean::getType;

    // 更新 DemoBean 資訊
    static void updateInfo(DemoBean obj) {
        System.out.println(String.format("toString:%s", obj));
    }

    public static void main(String[] args) throws ClassNotFoundException, InterruptedException {
        DemoBean bean = new DemoBean().setDate(new Date(System.currentTimeMillis() - 60000L))
                .setType("A")
                .setInteger(158)
                .setBigDecimal(BigDecimal.valueOf(177L))
                .setStatus(CommonOnOffStatus.ON);

        // 檢查整數和類型條件,返回結果
        Boolean result = checkDemoBeanInteger.or(checkDemoBeanType).test(bean);
        System.out.println(BooleanUtils.isTrue(result));

        // 使用多個 Consumer 依序處理 DemoBean
        defaultConsumerFun_3.andThen(defaultConsumerFun_2).andThen(defaultConsumerFun).accept(bean);

        // 使用 Supplier 創建 DemoBean
        DemoBean supplierBean = defaultSupplierFun.get();
        System.out.println(String.format("toString:%s, obj address:%s", supplierBean, supplierBean.printAddress(true)));

        // 使用 UnaryOperator 修改 DemoBean
        DemoBean unaryOperatorBean = defaultUnaryOperatorFun.apply(supplierBean);
        System.out.println(String.format("toString:%s, obj address:%s", unaryOperatorBean, unaryOperatorBean.printAddress(true)));

        System.out.println("=========== separate =========");

        // 使用 BiFunction 處理 DemoBean
        System.out.println(String.format("toString:%s, obj address:%s", unaryOperatorBean, defaultBiFunction.apply(unaryOperatorBean, true)));
        System.out.println(String.format("toString:%s, obj address:%s", unaryOperatorBean, defaultBiFunction.apply(unaryOperatorBean, false)));

        List<DemoBean> list = List.of(bean, supplierBean, unaryOperatorBean);

        // 遍歷並更新 DemoBean 資訊
        list.forEach(FunctionInterfaceTest::updateInfo);
    }
}

Last updated