java8鍥涘ぇ鍑芥暟寮忔帴鍙?/h1>

Posted 缁欎綘涓€鏍洓鍙惰崏

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java8鍥涘ぇ鍑芥暟寮忔帴鍙?/h1>

java8鍥涘ぇ鍑芥暟寮忔帴鍙?/span>

璇村埌java8  閭d笉寰椾笉鑱婁笅瀹冪殑杩欎釜鍑芥暟寮忕紪绋嬪暒 馃榿

java8鍥涘ぇ鍑芥暟寮忔帴鍙?/span>鍑芥暟寮忔帴鍙?/span>娉ㄨВFunctionalInterface鍥涘ぇ鎺ュ彛Consumer绀轰緥:Supplier绀轰緥:Function绀轰緥:Predicate绀轰緥:


鍑芥暟寮忔帴鍙?/span>

鍑芥暟寮忔帴鍙f寚鐨勬槸鎺ュ彛涓彧鏈変竴涓娊璞℃柟娉曠殑鎺ュ彛,鐢ㄦ敞瑙?/span>@FunctionalInterface鏍囪,琛ㄧず璇ユ帴鍙f槸鍑芥暟寮忔帴鍙?/span>


娉ㄨВFunctionalInterface

濡傚浘,鍙槸涓€涓爣璁版帴鍙?


鍥涘ぇ鎺ュ彛

Consumer

婧愮爜濡備笅:

java8鍥涘ぇ鍑芥暟寮忔帴鍙?></span></p> 
 <p cid=

浣滅敤: 鍐呴儴娑堝寲浼犺繘鏉ョ殑鍙傛暟,娌℃湁杩斿洖鍊?/span>

绀轰緥:

Consumer<String> consumer=System.out::println;

consumer.accept("Consumer");

// 缁撴灉: "Consumer"


Supplier

婧愮爜濡備笅:

java8鍥涘ぇ鍑芥暟寮忔帴鍙?></span></p> 
 <p cid=

浣滅敤: 娌℃湁浼犲弬,鐩存帴杩斿洖涓€涓粨鏋?/span>

绀轰緥:

Supplier<String> supplier= () -> "Supplier";

System.out.println(supplier.get());

// 缁撴灉: "Supplier"


Function

棰濊繖涓簮鐮佸お闀夸簡,鎴彇閮ㄥ垎婧愮爜濡備笅:

鍙互鐪嬪埌瀹冩槸缁煎悎浜嗚繖涓?/span>consumer鍜?/span>Supplier馃悥


浣滅敤: 鏍规嵁浼犵殑鍙傛暟,杩斿洖涓€涓粨鏋?杩欓噷鏄棦鏈変紶鍙?鍙堟湁杩斿洖鍊拣煒?/span>


绀轰緥:

Function<String,String> func= String::toUpperCase;

System.out.println(func.apply("Function"));

// 缁撴灉: "FUNCTION"


Predicate

棰濊繖涓簮鐮佷篃姣旇緝闀?鎴彇閮ㄥ垎婧愮爜濡備笅:


浣滅敤: 鍒ゆ柇缁欑殑鍙傛暟鏄惁绗﹀悎鏉′欢


绀轰緥:

Predicate<String> predicate= "predicate"::equals;

System.out.println(predicate.test("predicate"));

// 缁撴灉: true



以上是关于java8鍥涘ぇ鍑芥暟寮忔帴鍙?/h1>

鍑芥暟寮忕紪绋婩P 鍒濇帰

鍑芥暟寮忕紪绋嬩腑鐨勫壇浣滅敤姒傚康

馃鈥嶅嚱鏁板紡缂栫▼鎬濇兂锛屼綘浜嗚В澶氬皯鍛?

銆愬皬鐧藉涔燙++ 鏁欑▼銆戝叚銆丆++鍐呯疆鍑芥暟鍜屽嚱鏁颁紶鍙?/h1>

闃块噷宸村反寰湇鍔℃灦鏋勭殑鍥涘ぇ閲戝垰鍒╁櫒