JDK8新特性-java.util.function-Function接口

14年,Oracle公司如期發佈了Java 8正式版。現如今4年過去了,終於鼓起勇氣認真對待它,就好似雖然認識了好幾年的夥伴,突然感覺要成爲情侶的感覺……
JDK 1.8 API包含了很多內建的函數式接口,在老Java中常用到的比如Comparator或者Runnable接口,這些接口都增加了@FunctionalInterface註解以便能用在lambda上。現如今,我們則從Function常用函數入口,真正瞭解一下。

name type description
Consumer Consumer< T > 接收T對象,不返回值
Predicate Predicate< T > 接收T對象並返回boolean
Function Function< T, R > 接收T對象,返回R對象
Supplier Supplier< T > 提供T對象(例如工廠),不接收值
UnaryOperator UnaryOperator 接收T對象,返回T對象
BinaryOperator BinaryOperator 接收兩個T對象,返回T對象

標註爲FunctionalInterface的接口被稱爲函數式接口,該接口只能有一個自定義方法,但是可以包括從object類繼承而來的方法。如果一個接口只有一個方法,則編譯器會認爲這就是一個函數式接口。是否是一個函數式接口,需要注意的有以下幾點:

  • 該註解只能標記在”有且僅有一個抽象方法”的接口上。
  • JDK8接口中的靜態方法和默認方法,都不算是抽象方法。
  • 接口默認繼承java.lang.Object,所以如果接口顯示聲明覆蓋了Object中方法,那麼也不算抽象方法。
  • 該註解不是必須的,如果一個接口符合”函數式接口”定義,那麼加不加該註解都沒有影響。加上該註解能夠更好地讓編譯器進行檢查。如果編寫的不是函數式接口,但是加上了@FunctionInterface,那麼編譯器會報錯。
  • 在一個接口中定義兩個自定義的方法,就會產生Invalid ‘@FunctionalInterface’ annotation; FunctionalInterfaceTest is not a functional interface錯誤.

Function常用方法&&實踐

//將Function對象應用到輸入的參數上,然後返回計算結果。
R apply(T t);
//返回一個先執行當前函數對象apply方法再執行after函數對象apply方法的函數對象。
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }
//返回一個先執行before函數對象apply方法再執行當前函數對象apply方法的函數對象
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

compose 和 andThen 的不同之處是函數執行的順序不同。compose 函數先執行參數,然後執行調用者,而 andThen 先執行調用者,然後再執行參數。

public static void main(String[] args) {
        Function<Integer, Integer> name = e -> e * 2;
        Function<Integer, Integer> square = e -> e * e;
        int value = name.andThen(square).apply(3);
        System.out.println("andThen value=" + value);
        int value2 = name.compose(square).apply(3);
        System.out.println("compose value2=" + value2);
        //返回一個執行了apply()方法之後只會返回輸入參數的函數對象
        Object identity = Function.identity().apply("huohuo");
        System.out.println(identity);
    }

直接看結果:

andThen value=36
compose value2=18
huohuo

apply基本應用

字符串長度記錄返回

public class MyFunction implements Function<String,Integer>{

    @Override
    public Integer apply(String s) {
        return s.length();
    }
}

返回兩個字符串的連接,BiFunction與Function的不同就是傳入兩個參數,依舊返回一個值。

public class MyBiFunction implements BiFunction<String, String, String> {
    @Override
    public String apply(String s, String s2) {
        return s+";"+s2;
    }
}

最後調用結果:

private static String hello = "Nice to meet you";
    private static String name = "my name is huohuo";

    public static void main(String[] args) {
        MyFunction myFunction = new MyFunction();
        MyBiFunction biFunction = new MyBiFunction();
        int num = myFunction.apply(hello);
        String valueBi = biFunction.apply(hello, name);
        //hello長度返回
        System.out.println(num);
        //語句整合返回
        System.out.println(valueBi);
    }

返回值:

16
Nice to meet you;my name is huohuo

其實使用的過程感覺這些都無所必要,但是對於新特性以及代碼規範而言,即使是簡易代碼,也有了一個整合的過程。Function簡單實踐僅此爲止,下篇文章講述Predicate的使用。

發佈了220 篇原創文章 · 獲贊 477 · 訪問量 36萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章