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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

java8 注解: @FunctionalInterface (函数式接口)

發布時間:2023/12/18 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java8 注解: @FunctionalInterface (函数式接口) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。

Java8提倡函數式編程,因而新增了一個函數式接口。函數式接口保證了函數式編程,同時也保證了能夠兼容以前的java版本。

?????函數式接口的定義

?????在java8中,滿足下面任意一個條件的接口都是函數式接口:

1、被@FunctionalInterface注釋的接口,滿足@FunctionalInterface注釋的約束。

2、沒有被@FunctionalInterface注釋的接口,但是滿足@FunctionalInterface注釋的約束。

?

?????@FunctionalInterface注釋的約束:

1、接口有且只能有個一個抽象方法,只有方法定義,沒有方法體

2、在接口中覆寫Object類中的public方法,不算是函數式接口的方法。

下面三個接口都是函數式接口:

//接口一 @FunctionalInterface public interface FunctionInterfaceTest {String getInfo(String input);@OverrideString toString(); ?//Object中的方法@Overrideboolean equals(Object obj); //Object中的方法 } //接口二 @FunctionalInterface public interface FunctionInterfaceTest {String getInfo(String input);}//接口三 public interface FunctionInterfaceTest {String getInfo(String input);}


??????函數式接口實例的創建

函數式接口實例的創建有三種方式:1、lambda表達式;2、方法引用;3、構造方法引用。

public class Main {public static void main(String[] args) {/*** 1、lambda表達式* 這種形式最為直觀,lambda表達式,接收一個String類型的參數,返回一個String類型的結果。* 完全符合函數式接口FunctionInterfaceTest的定義*/FunctionInterfaceTest functionInterfaceTest1 = item -> item+1; ?/*** 2、方法引用* Main方法當中的getInstance和getMessage方法接收一個參數,返回一個結果。符合函數式接口* FunctionInterfaceTest的定義。* 函數式接口只是定義了個方法的約定(接收一個String類型的參數,返回一個String類型的結果),* 而對于方法內部進行何種操作則并沒有做任何的限制。在這點上,跟java以前的版本中的實現類與接口之間的* 關系很類似。不同的是,函數式接口更偏重于計算過程,約束了一個計算過程的輸入和輸出。* 這種約束計算過程的輸入和輸出的形式的好處可以看一下joinStr方法。*/FunctionInterfaceTest functionInterfaceTest2 = Main::getInstance; ?//方法引用FunctionInterfaceTest functionInterfaceTest3 = Main::getMessage; ?//方法引用String msg1 = joinStr("你好",functionInterfaceTest2); //輸出:你好!世界String msg2 = joinStr("你好",functionInterfaceTest3); //輸出:世界,你好!System.out.println(msg1);System.out.println(msg2);//還有更簡單的寫法,高度抽象化,具體處理由使用者自己決定String msg3 = joinStr("你好",item ->item+"!世界"); //輸出:你好!世界String msg4 = joinStr("你好",item ->"世界,"+ item+"!"); //輸出:世界,你好!System.out.println(msg3);System.out.println(msg4);/*** 3、構造方法引用* 構造函數的結構:接收輸入參數,然后返回一個對象。這種約束跟函數式接口的約束很像。* 所以只要“輸入參數類型”與“輸出參數類型”跟FunctionInterfaceTest中的方法約束相同,* 就可以創建出FunctionInterfaceTest接口的實例,如下,String的構造方法中有* new String(str)的構造方法,所以可以得到實例。* 這里存在一個類型推斷的問題,JDK的編譯器已經幫我們自動找到了只有一個參數,且是String類型的構造方法。* 這就是我們直接String::new,沒有指定使用哪一個構造方法,卻可以創建實例的原因*/FunctionInterfaceTest functionInterfaceTest4 = String::new; //方法引用}public static String getInstance(String item){return item+"!世界";}public static String getMessage(String massage){return "世界,"+ massage+"!";}public ?static String joinStr(String str,FunctionInterfaceTest functionTest){return functionTest.getInfo(str);}


???????java8中常用的函數式接口:

常用的函數式接口主要有四種類型,是通過其輸入和輸出的參數來進行區分的。定義了編碼過程中主要的使用場景。

public class FunctionalInterfaceMain {public static void main(String[] args) {/*** 先看看如何創建它們*/Function<String,String> function1 = item -> item +"返回值";Consumer<String> function2 = iterm -> {System.out.println(iterm);};//lambda語句,使用大括號,沒有return關鍵字,表示沒有返回值Predicate<String> function3 = iterm -> "".equals(iterm);Supplier<String> function4 = () -> new String("");/*** 再看看怎么使用* demo釋義:* 1、創建一個String類型的集合* 2、將集合中的所有元素的末尾追加字符串'1'* 3、選出長度大于2的字符串* 4、遍歷輸出所有元素*/List<String> list = Arrays.asList("zhangsan","lisi","wangwu","xiaoming","zhaoliu");list.stream().map(value -> value + "1") //傳入的是一個Function函數式接口.filter(value -> value.length() > 2) //傳入的是一個Predicate函數式接口.forEach(value -> System.out.println(value)); //傳入的是一個Consumer函數式接口}}

? ? 在實際使用中,我們往往會輸入多個參數,而不是一個參數。針對于多個參數的計算,最終都可以拆分兩個參數的運算,然后將兩個參數的運算結合起來。如:1+2+3+4 = 10,可以拆分為1+2 = 3,???3+3=6; ??6+4 = 10 三個步驟完成(在java中,是不允許一次返回多個值的)。

??????因此對于多個參數的操作也是如此。Java8中對于接收兩個參數的場景提供了相關的函數式接口。如下:

?public class FunctionalInterfaceTest {public static void main(String[] args) {/*** Bi類型的接口創建*/BiFunction<String, String, Integer> biFunction = (str1,str2) -> str1.length()+str2.length();BiConsumer<String, String> biConsumer = (str1,str2) -> System.out.println(str1+str2);BiPredicate<String, String> biPredicate = (str1,str2) -> str1.length() > str2.length();/*** Bi類型的接口使用*/int length = getLength("hello", "world", (str1,str2) -> str1.length() + str2.length()); //輸出10boolean boolean1 = getBoolean("hello", "world", (str1,str2) -> str1.length() > str2.length()); //輸出falseSystem.out.println(length);System.out.println(boolean1);noResult("hello", "world", (str1,str2) -> System.out.println(str1+" "+str2)); //沒有輸出}public ?static int getLength(String str1,String str2,BiFunction<String, String, Integer> function){return function.apply(str1, str2);}public static void noResult(String str1,String str2,BiConsumer<String, String> biConcumer){biConcumer.accept(str1, str2);}public static boolean getBoolean(String str1,String str2,BiPredicate<String, String> biPredicate){return biPredicate.test(str1, str2);} }

???????關于多個參數值的使用,無論實在Function接口中,還是在BI類型的接口都提供了類似的操作。(注:java8中,接口的方法是可以有實現的,但需要default關鍵字修飾,這是其他版本的jdk沒有的特性)

1、??Function接口的andThen方法和compose方法

源碼:

default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {Objects.requireNonNull(before);return (V v) -> apply(before.apply(v)); }default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {Objects.requireNonNull(after);return (T t) -> after.apply(apply(t)); }

說明:

???????Compose方法:方法接收一個Function類型的參數,返回一個值。這也是一個標準的Function類型的定義。在compose方法內部也有一個apply方法。在執行compose方法中的apply方法之前,它先執行了before接口的apply方法,也是compose方法的輸入參數。然后將before方法執行的返回值作為compose中apply方法的輸入參數。實際上是形成了一種鏈式組合。

???????andThen方法:該方法與compose方法很類似。不同之處在于,andThen是先執行自身的apply方法,將apply的返回值作為after接口的輸入值。相對于compose方法,只是方向的不同

使用:

public class FunctionalInterfaceTest {public static void main(String[] args) {String str1 = getLength1("hello", value -> "hello的長度:"+value, value -> value.length()); //輸出:hello的長度:5System.out.println(str1);Integer result = getLength2("hello", value -> value, value -> value.length()); //輸出:5System.out.println(result);}public ?static String getLength1(String str1,Function<Integer, String> function1,Function<String,Integer> function2){/*** 這里一定要注意,function1和function2的參數類型。* function2的輸出類型與function1的輸入類型一定要一致,* 否則編譯不會通過*/return function1.compose(function2).apply(str1);}public ?static Integer getLength2(String str1,Function<String, String> function1,Function<String,Integer> function2){/*** 這里一定要注意,function1和function2的參數類型。* function1的輸出類型與function2的輸入類型一定要一致,(方向相反)* 否則編譯不會通過*/return function1.andThen(function2).apply(str1);} }

相關接口:BiFunction

public static Integer getLength3(String str1,String str2,BiFunction<String, String, String> biFunction,Function<String,Integer> function){/*** biFunction只有andThen方法,這是有bi類型接口的特征決定的。* bi類型的接口需要接收兩個參數,然而java中是沒有返回兩個參數的情況的* 所以只有andThen方法,且其參數是function類型的,接收的是一個參數,* 返回一個值*/return biFunction.andThen(function).apply(str1, str2); }

2、Consumer接口的andThen方法

源碼:

default Consumer<T> andThen(Consumer<? super T> after) {Objects.requireNonNull(after);return (T t) -> { accept(t); after.accept(t); }; }


說明:
???????將輸入參數分別賦給andThen內部的accept方法和after內部的accept方法。After的計算在andThen之后,起到了后置連接的作用。在這里沒有compose方法,因為后置連接反過來就是前置連接,所以不需要一個多余的compose方法了。只需要在傳遞時,交換兩個consumer的順序即可。

使用:

public class FunctionalInterfaceTest {public static void main(String[] args) {noResult(Integer.valueOf(12),?value -> {int num = value + 12;System.out.println(num);},?value -> { int num = value + 24;System.out.println(num);}); //輸出:24,36}public static void noResult(Integer num,Consumer<Integer> consumer1,Consumer<Integer> consumer2){/*** 兩個consumer的接收類型必須一致*/consumer1.andThen(consumer2).accept(num);} }?

相關接口:BiConsumer

public static void noResultBi(Integer num1,Integer num2,BiConsumer<Integer,Integer> consumer1,BiConsumer<Integer,Integer> consumer2){/*** 兩個consumer的接收類型必須一致*/consumer1.andThen(consumer2).accept(num1,num2); }

3、 predicate接口的and、or、negate方法
源碼:

default Predicate<T> and(Predicate<? super T> other) {Objects.requireNonNull(other);return (t) -> test(t) && other.test(t); }default Predicate<T> negate() {return (t) -> !test(t); }default Predicate<T> or(Predicate<? super T> other) {Objects.requireNonNull(other);return (t) -> test(t) || other.test(t); }

說明:

???????源碼已經很清晰了,就不一一說明了。分別是&&, || 和取反操作。

使用:

public class FunctionalInterfaceTest {public static void main(String[] args) {getBoolean("hello", value -> value.length() > 2, value -> value.length() > 6);}public static boolean getBoolean(String str1,Predicate< String> predicate1,Predicate< String> predicate2){boolean test = predicate1.or(predicate2).test(str1);System.out.println(test); //輸出truetest = predicate1.and(predicate2).test(str1);System.out.println(test);//輸出falsetest = predicate1.negate().test(str1);System.out.println(test);//輸出falsereturn test;} }


相關接口:BiPreditcate

public static ?boolean getBooleanBi(String str1,String str2,BiPredicate<String, String> biPredicate1,BiPredicate<String, String> biPredicate2){boolean test = biPredicate1.and(biPredicate2).test(str1, str2);test = biPredicate1.negate().test(str1, str2);test = biPredicate1.or(biPredicate2).test(str1, str2);return test; }

???????此外,java8針對原生類型int,long,double都提供了相應的函數式接口。如:DoubleConsumer, DoubleFunction,IntConsumer等等,使用方式都相同,見java.util.function包。


————————————————
本文為CSDN博主「漠風-」的原創文章,遵循 CC 4.0 BY-SA 版權協議。

總結

以上是生活随笔為你收集整理的java8 注解: @FunctionalInterface (函数式接口)的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 亚洲成人二区 | 欧美天堂在线视频 | a天堂v| 精彩毛片 | 欧美午夜性春猛交 | 桥本有菜aⅴ一区二区三区 在线午夜电影 | 亚洲激情网址 | 欧美黄色精品 | av在线网页 | 久久久久女 | 中文字幕日本一区 | 女人18毛片水真多18精品 | 男人天堂av网 | 亚洲精品香蕉 | 偷自拍 | 黄页网址大全免费观看 | 婷婷亚洲综合 | 成片免费观看视频大全 | 久久精视频 | 成人免费在线小视频 | 日韩视频在线视频 | 日本一区二区三区在线视频 | av天堂永久资源网 | 禁漫天堂黄漫画无遮挡观看 | 韩国成人理伦片免费播放 | 性高潮久久久久 | 男人午夜av | 国内av| 国产青草视频在线观看 | 色欲久久久天天天综合网精品 | 日日骚av| 欧美一区精品 | 小宝贝真紧h军人h | 国产在线a视频 | 国产男女猛烈无遮挡免费视频 | 三级免费 | 又粗又大又硬又长又爽 | 91红桃视频 | 18女人毛片 | 欧美xxxxxxxxx| 小柔的裸露日记h | 亚洲精品在线免费看 | 青春草国产视频 | 大帝av | 五月天婷婷激情视频 | jlzzjlzz欧美大全 | 中文字幕第100页 | 男人天堂tv | 一道本一区二区 | 伊人网亚洲 | 香港三级在线视频 | 日韩在线视 | 日韩三级麻豆 | 私密spa按摩按到高潮 | 黄色片在线免费观看 | h片观看| 日日日视频| 国产视频首页 | 人妖一区| 国产欧美日韩综合精品一区二区三区 | 精品热久久| 亚洲av无码潮喷在线观看 | 激情五月综合色婷婷一区二区 | 波多野吉衣一区 | 337p亚洲精品色噜噜狠狠 | 国产一级特黄毛片 | 97超碰人人模人人人爽人人爱 | 精品在线小视频 | 日日噜噜夜夜狠狠久久丁香五月 | 亚洲女则毛耸耸bbw 边吃奶边添下面好爽 | 免费黄色小视频 | 在线观看成年人视频 | 日本少妇裸体做爰 | 国产精品久久久久久久一区二区 | 丰满护士巨好爽好大乳 | 日韩视频在线免费播放 | 色人综合| 亚欧日韩 | 日本高清免费观看 | 国产精品久久久久久影视 | 超碰伦理 | 不卡一二区 | 在线免费视频一区二区 | 青娱乐自拍视频 | 久久久久久久久亚洲 | 国产在线观看免费 | 欧美天堂在线观看 | a天堂在线 | 杨幂一区二区三区免费看视频 | 91中文国产| 国产无码精品合集 | 日韩黄色小视频 | 午夜在线观看免费视频 | 69视频在线 | 欧美精品四区 | 特黄做受又粗又大又硬老头 | 91av片| 欧美日本日韩 | 成人在线播放av |