java8 Function 接口

java8提供一系列函數式接口,簡化很多操作,直接上代碼

public class FunctionUtils {
    public static void main(String[] args) {
        System.out.println(fun2(2));
        System.out.println(fun3(2));
        consumer(2);
        System.out.println(supplierGetUser());
    }


    // -------------------Function-------------------
    // apply:函數式接口方法,輸入T,輸出R。
    // compose:compose接收一個Function參數,先執行傳入的邏輯,再執行當前的邏輯。
    // andThen:andThen接收一個Function參數,先執行當前的邏輯,再執行傳入的邏輯。
    // identity:方便方法的連綴,返回當前對象。
    public static int fun1(int i) {
        // i = 2時  結果為2 + 2 = 4
        Function<Integer, Integer> function = f -> f + f;
        return function.apply(i);
    }

    public static int fun2(int i) {
        // i = 2時  結果為 2*2=4 再 4+4 = 8
        Function<Integer, Integer> function1 = f -> f + f;
        Function<Integer, Integer> function2 = f -> f * f;
        return function1.compose(function2).apply(i);
    }

    public static int fun3(int i) {
        // i = 2時  結果為 2+2=4 再 4*4=16
        Function<Integer, Integer> function1 = f -> f + f;
        Function<Integer, Integer> function2 = f -> f * f;
        return function1.andThen(function2).apply(i);
    }

    //---------------------------Consumer------------------------------

    // consumer 表示消費的意思,其實也就是處理其中的邏輯運算 注意!!!無返回值

    /**
     * 然后調用accept,對這個參數做一系列的操作,沒有返回值
     *
     * @param i 入參
     */
    public static void consumer(int i) {
        Consumer<Integer> consumer = x -> {
            int a = x + 2;
            System.out.println(a);// 12
            System.out.println(a + "*");// 12*
        };
        consumer.accept(i);
    }

    //----------------------------Supplier-------------------------------
    // 看語義,可以看到,這個接口是一個提供者的意思,只有一個get的抽象類,
    // 沒有默認的方法以及靜態的方法,傳入一個泛型T的,get方法,返回一個泛型T
    public static String supplierGet() {
        Supplier<String> supplier = String::new;
        return supplier.get();
    }

    public static User supplierGetUser() {
        Supplier<User> supplier = User::new;
        // 獲取這個用戶
        User user = supplier.get();
        user.setName("123");
        return user;
    }

    //----------------------------Predicate------------------------------
    // 這個接口可以理解為做判斷的意思

    /**
     * Predicate 使用 判斷傳進來的值 再做對比判斷
     *
     * @param value     值
     * @param predicate 比對
     * @return true = 符合預期
     */
    public static boolean jug(int value, Predicate<Integer> predicate) {
        return predicate.test(value);
    }

    /**
     * 類型多條件判斷 xx&&xx 均成立時
     *
     * @param value        值
     * @param predicateOne 條件一的表達式
     * @param predicateTwo 條件二的表達式
     * @return true = 符合預期
     */
    public static boolean jug(int value, Predicate<Integer> predicateOne, Predicate<Integer> predicateTwo) {
        return predicateOne.and(predicateTwo).test(value);
    }

    /**
     * 類型多條件判斷 xx or xx 或的關系
     * negateJug(1, v1 -> v1 > 3, v2 -> v2 < 5)
     *
     * @param value        值
     * @param predicateOne 條件一的表達式
     * @param predicateTwo 條件二的表達式
     * @return true = 符合預期
     */
    public static boolean or(int value, Predicate<Integer> predicateOne, Predicate<Integer> predicateTwo) {
        return predicateOne.or(predicateTwo).test(value);
    }

    /**
     * Predicate 使用 判斷傳進來的值 再做對比判斷
     * 非的對比 類似x!=x
     * negateJug(1, v -> v > 1)
     *
     * @param value     值
     * @param predicate 比對
     * @return true = 符合預期
     */
    public static boolean negateJug(int value, Predicate<Integer> predicate) {
        return predicate.negate().test(value);
    }
}

@Data
class User {
    private String name;

    private String sex;
}
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容