乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      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è)泛型類(lèi)型<T, R>.

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

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

      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è)類(lèi)型為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è)異步線程的類(lèi),很牛逼,很強(qiáng)大的類(lèi): 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)㈩?lèi)型為T(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)㈩?lèi)型是T, 返回值是一個(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接口類(lèi)

      @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)容均由用戶(hù)發(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)遵守用戶(hù) 評(píng)論公約

        類(lèi)似文章 更多