JDK8新特性之函式式介面
阿新 • • 發佈:2019-02-17
什麼是函式式介面
先來看看傳統的建立執行緒是怎麼寫的
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("t1");
}
});
t1.start();
再來看看使用了函式式介面是怎麼寫的
Thread t2 = new Thread(() -> System.out.println("t2"));
t2.start();
Runnable介面直接可以使用Lambda表示式來編寫,這是因為Runnable介面是一個函式式介面,來看看Runnable的原始碼。
@FunctionalInterface
public interface Runnable {
public abstract void run();
}
發現該介面加上了函式式介面的定義註解:@FunctionalInterface
,表明該介面是一個函式式介面。
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface FunctionalInterface {
}
在JDK8中,除了Runnbale介面,還有像Comparator、Callable等介面都加上了該註解定義為函式式介面。
內建函式式介面
JDK8提供了幾個內建的函式式介面,用在了許多API的地方,都可以拿來用,可以滿足大部分應用。
//Consumer<T> - T作為輸入,執行某種動作但沒有返回值
Consumer<String> con = (x) -> {
System.out.println(x);
};
con.accept("hello world");
//Supplier<T> - 沒有任何輸入,返回T
Supplier<String> supp = () -> {
return "Supplier";
};
System.out.println(supp.get());
// Predicate<T> -T作為輸入,返回的boolean值作為輸出
Predicate<String> pre = (x) -> {
System.out.print(x);
return x.startsWith("op");
};
System.out.println(": " + pre.test("op, hello World"));
// Function<T, R> -T作為輸入,返回的R作為輸出
Function<String, String> function = (x) -> {
System.out.print(x + ": ");
return "Function";
};
System.out.println(function.apply("hello world"));
//BinaryOperator<T> -兩個T作為輸入,返回一個T作為輸出,對於“reduce”操作很有用
BinaryOperator<String> bina = (x, y) -> {
System.out.print(x + " " + y);
return "BinaryOperator";
};
System.out.println(" " + bina.apply("hello ", "world"));
自定義函式式介面
1、自定義一個函式式介面
@FunctionalInterface
public interface CalcInterface<N, V> {
V operation(N n1, N n2);
}
這裡只有一個抽象方法,@FunctionalInterface註解可以不用寫,至於為什麼可以往下看。
2、新建一個引用函式式介面的類
public static class NumberOperation<N extends Number, V extends Number> {
private N n1;
private N n2;
public NumberOperation(N n1, N n2) {
this.n1 = n1;
this.n2 = n2;
}
public V calc(CalcInterface<N, V> ci) {
V v = ci.operation(n1, n2);
return v;
}
}
3、測試函式式介面
private static void testOperationFnInterface() {
NumberOperation<Integer, Integer> np = new NumberOperation(13, 10);
CalcInterface<Integer, Integer> addOper1 = (n1, n2) -> {
return n1 + n2;
};
CalcInterface<Integer, Integer> multiOper1 = (n1, n2) -> {
return n1 * n2;
};
System.out.println(np.calc1(addOper1));
System.out.println(np.calc1(multiOper1));
// 上面的可以簡寫為
System.out.println(np.calc1((n1, n2) -> n1 + n2));
System.out.println(np.calc1((n1, n2) -> n1 * n2));
}
最後輸出:
23
130
23
130
函式式介面規範
1、@FunctionalInterface標識為一個函式式介面只能用在只有一個抽象方法的介面上。
2、介面中的靜態方法、預設方法、覆蓋了Object類的方法都不算抽象方法。
3、@FunctionalInterface註解不是必須的,如果該介面只有一個抽象方法可以不寫,它預設就符合函式式介面,但建議都寫上該註解,編譯器會檢查該介面是否符合函式式介面的規範。
舉例說明
正確的函式式介面。
@FunctionalInterface
public interface CalcInterface<N, V> {
V operation(N n1, N n2);
}
加了幾個符合函式式的方法也沒事,編譯器也不會報錯。
@FunctionalInterface
public interface CalcInterface<N, V> {
V operation(N n1, N n2);
public boolean equals(Object object);
public default void defaultMethod() {
}
public static void staticMethod() {
}
}
這個沒用@FunctionalInterface函式式介面,有兩個抽象方法,不能用於Lambda表示式。
public interface CalcInterface<N, V> {
V operation(N n1, N n2);
V operation2(N n1, N n2);
}
這個有兩個抽象方法的用@FunctionalInterface註解的函式式介面編譯會報錯。
@FunctionalInterface
public interface CalcInterface<N, V> {
V operation(N n1, N n2);
V operation2(N n1, N n2);
}
這個沒有一個抽象方法,編譯報錯。
public interface CalcInterface<N, V> {
}
推薦閱讀
分享Java乾貨,高併發程式設計,熱門技術教程,微服務及分散式技術,架構設計,區塊鏈技術,人工智慧,大資料,Java面試題,以及前沿熱門資訊等。