日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Java 8中,lamda函數(shù)編程

 ansatsing 2018-05-02

本文僅僅作為科普,大牛請(qǐng)無(wú)視. (本文的所有例子都是寫(xiě)在junit里的, 不過(guò)貼代碼的時(shí)候我把@Test去掉了)

Function,Consumer,Predicate,Supplier這些接口有一個(gè)共性,就是都有一個(gè)@FunctionalInterface的注解, 有了這個(gè)注解,你就可以自定義lamda表達(dá)式了.

本文先介紹一些例子,然后自定義一個(gè)lamda表達(dá)式的接口.

先看一下Function接口定義:

    @FunctionalInterface    
    public interface Function<T, R>

接口接受兩個(gè)泛型類型<T, R>.

再看一下接口定義的方法(非靜態(tài),非default), 支持lamda表達(dá)式的接口只允許定義一個(gè)抽象方法(@FunctionalInterface注解的接口,只允許定義一個(gè)抽象方法),只要記住這一點(diǎn),你就不會(huì)弄混了.

    apply(T t);    
    /**
     * T 入?yún)㈩愋? t 輸入?yún)?shù)
     * R 返回值類型
     */

OK, 現(xiàn)在明確了, 該接口的lamda表達(dá)式應(yīng)該是接受一個(gè)入?yún)?最后要有一個(gè)返回值, 寫(xiě)法應(yīng)該是這樣的: (x) -> {return y;} 

如果你的lamda表達(dá)式非常簡(jiǎn)單,只有一行,那么你可以不寫(xiě)return, 不加花括號(hào){}, 返回值后面可以不加分號(hào).

下面就可以寫(xiě)example了, 寫(xiě)一個(gè)簡(jiǎn)單的, 再寫(xiě)一個(gè)標(biāo)準(zhǔn)的.

    public void testFunction(){
                //簡(jiǎn)單的,只有一行
		Function<Integer, Stringfunction1 = (x) -> "test result: " + x;
		
		//標(biāo)準(zhǔn)的,有花括號(hào), return, 分號(hào).
		Function<StringStringfunction2 = (x) -> {
			return "after function1";
		};
		System.out.println(function1.apply(6));
		System.out.println(function1.andThen(function2).apply(6));
	}

OK, Function的例子寫(xiě)完了,接下來(lái)寫(xiě)其他的,其實(shí)原理懂了,其他的就都簡(jiǎn)單了,然后就是熟能生巧了.



再看看Supplier的接口定義,這個(gè)接口定義比較簡(jiǎn)單,我就都貼上來(lái)了

    @FunctionalInterface
    public interface Supplier<T{

        /**
         * Gets a result.
         *
         * @return a result
         */
        get();
    }

接口接受一個(gè)泛型<T>, 接口方法是一個(gè)無(wú)參數(shù)的方法, 有一個(gè)類型為T(mén)的返回值. OK, 那么接口的lamda表達(dá)式應(yīng)該是這樣的: () -> { return something; }, 好,下面來(lái)寫(xiě)一個(gè)example.

    public void testSupplier(){
                //簡(jiǎn)寫(xiě)
		Supplier<String> supplier1 = () -> "Test supplier";
		System.out.println(supplier1.get());
		
		//標(biāo)準(zhǔn)格式
		Supplier<Integer> supplier2 = () -> {
			return 20;
		};
		System.out.println(supplier2.get() instanceof Integer);
	}

到這里你或許有一點(diǎn)疑惑, 這Supplier到底能用在哪啊? Java 8里新增了一個(gè)異步線程的類,很牛逼,很強(qiáng)大的類: CompletableFuture, 里面的很多方法的入?yún)⒍加玫降腟upplier, 例如: supplyAsync方法. 本文暫時(shí)不介紹CompletableFuture.



接下來(lái)是Consumer, 我們來(lái)看一下接口的定義:

    @FunctionalInterface
    public interface Consumer<T>

然后再看一下里面的抽象方法:

    void accept(T t);

現(xiàn)在了解了: 接口接受一個(gè)泛型<T>, 接口方法是入?yún)㈩愋蜑門(mén), 無(wú)返回值的方法, OK,下面開(kāi)始寫(xiě)example:

    public void testConsumer(){
		Consumer<String> consumer1 = (x) -> System.out.print(x);
		Consumer<String> consumer2 = (x) -> {
			System.out.println(" after consumer 1");
		};
		consumer1.andThen(consumer2).accept("test consumer1");
	}



接下來(lái)看一下Predicate接口

接口定義:

    @FunctionalInterface    
    public interface Predicate<T>

抽象方法:

    boolean test(T t);

接口接受一個(gè)泛型<T>, 接口方法的入?yún)㈩愋褪荰, 返回值是一個(gè)布爾值, OK, 下面寫(xiě)example:

    public void testPredicate(){
		Predicate<String> predicate = (x) -> x.length() > 0;
		System.out.println(predicate.test("String"));
	}

Predicate接口在stream里面用的比較多, 感興趣的可以去看看stream, java 8 里另一個(gè)新的東西,很好玩.




到這里基本明白這些lamda表達(dá)式的接口怎么用了,接下來(lái)自定義一個(gè)支持lamda表達(dá)式的接口玩玩,

    @FunctionalInterface    
    public interface CustomLamda<T{
    
    	testCustomFunction(Consumer<T> cunsumer);
    	
    	/*如果把下面方法的注釋放開(kāi), 那么接口就報(bào)錯(cuò)了. 驗(yàn)證了前面所說(shuō)的:@FunctionalInterface注解的接口只允許         *有一個(gè)抽象方法
    	 */
        //T anErrorMethod();
    }

下面是實(shí)現(xiàn):

    public void testCustomLamda(){
		Consumer<String> consumer = (x) -> {
			System.out.println("test" + x);
		};
		CustomLamda<String> customLamda = (x) -> {
			x.accept("6");
			return "6";
		};
		customLamda.testCustomFunction(consumer);
	}

本文僅僅是拋磚引玉, 深入的東西還需要多讀多看.


DEMO:

自定義lamda接口類

@FunctionalInterface
public interface Sum {
int plus(int a,int b);
}
測(cè)試接口
/**
* 測(cè)試自定義lamda接口
* @author sunyuanqing
* @date 2018/5/2 0002
*/
public class Main {
public static void main(String[] args) {
Sum sum1 = (a,b)->a+b;
Sum sum2 = (a,b)->a+b+10;
System.out.println(sum1.plus(2,3));//5
System.out.println(sum2.plus(2,3));//5+10=15
}
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多