java8鍥涘ぇ鍑芥暟寮忔帴鍙?/span>
璇村埌java8 閭d笉寰椾笉鑱婁笅瀹冪殑杩欎釜鍑芥暟寮忕紪绋嬪暒 馃榿
java8鍥涘ぇ鍑芥暟寮忔帴鍙?/span>鍑芥暟寮忔帴鍙?/span>娉ㄨВFunctionalInterface
鍥涘ぇ鎺ュ彛Consumer绀轰緥:Supplier绀轰緥:Function绀轰緥:Predicate绀轰緥:
鍑芥暟寮忔帴鍙?/span>
鍑芥暟寮忔帴鍙f寚鐨勬槸鎺ュ彛涓彧鏈変竴涓娊璞℃柟娉曠殑鎺ュ彛,鐢ㄦ敞瑙?/span>@FunctionalInterface
鏍囪,琛ㄧず璇ユ帴鍙f槸鍑芥暟寮忔帴鍙?/span>
娉ㄨВFunctionalInterface
濡傚浘,鍙槸涓€涓爣璁版帴鍙?
鍥涘ぇ鎺ュ彛
Consumer
婧愮爜濡備笅:
浣滅敤: 鍐呴儴娑堝寲浼犺繘鏉ョ殑鍙傛暟,娌℃湁杩斿洖鍊?/span>
绀轰緥:
Consumer<String> consumer=System.out::println;
consumer.accept("Consumer");
// 缁撴灉: "Consumer"
Supplier
婧愮爜濡備笅:
浣滅敤: 娌℃湁浼犲弬,鐩存帴杩斿洖涓€涓粨鏋?/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