日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JDK8新特性之函数式接口

發布時間:2023/12/3 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JDK8新特性之函数式接口 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載自?JDK8新特性之函數式接口

什么是函數式接口

先來看看傳統的創建線程是怎么寫的

Thread t1 = new Thread(new Runnable() {@Overridepublic 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> { ? ? }

?

總結

以上是生活随笔為你收集整理的JDK8新特性之函数式接口的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。