posts - 325,  comments - 25,  trackbacks - 0
          //原來的匿名內(nèi)部類
           @Test
           public void test1(){
            Comparator<String> com = new Comparator<String>(){
             @Override
             public int compare(String o1, String o2) {
              return Integer.compare(o1.length(), o2.length());
             }
            };
            
            TreeSet<String> ts = new TreeSet<>(com);
            
            TreeSet<String> ts2 = new TreeSet<>(new Comparator<String>(){
             @Override
             public int compare(String o1, String o2) {
              return Integer.compare(o1.length(), o2.length());
             }
             
            });
           }
           
           //現(xiàn)在的 Lambda 表達式
           @Test
           public void test2(){
            Comparator<String> com = (x, y) -> Integer.compare(x.length(), y.length());
            TreeSet<String> ts = new TreeSet<>(com);
           }

          *
           * 一、Lambda 表達式的基礎(chǔ)語法:Java8中引入了一個新的操作符 "->" 該操作符稱為箭頭操作符或 Lambda 操作符
           *           箭頭操作符將 Lambda 表達式拆分成兩部分:
           *
           * 左側(cè):Lambda 表達式的參數(shù)列表
           * 右側(cè):Lambda 表達式中所需執(zhí)行的功能, 即 Lambda 體
           *
           * 語法格式一:無參數(shù),無返回值
           *   () -> System.out.println("Hello Lambda!");
           *
           * 語法格式二:有一個參數(shù),并且無返回值
           *   (x) -> System.out.println(x)
           *
           * 語法格式三:若只有一個參數(shù),小括號可以省略不寫
           *   x -> System.out.println(x)
           *
           * 語法格式四:有兩個以上的參數(shù),有返回值,并且 Lambda 體中有多條語句
           *  Comparator<Integer> com = (x, y) -> {
           *   System.out.println("函數(shù)式接口");
           *   return Integer.compare(x, y);
           *  };
           *
           * 語法格式五:若 Lambda 體中只有一條語句, return 和 大括號都可以省略不寫
           *   Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
           *
           * 語法格式六:Lambda 表達式的參數(shù)列表的數(shù)據(jù)類型可以省略不寫,因為JVM編譯器通過上下文推斷出,數(shù)據(jù)類型,即“類型推斷”
           *   (Integer x, Integer y) -> Integer.compare(x, y);
           *
           * 上聯(lián):左右遇一括號省
           * 下聯(lián):左側(cè)推斷類型省
           * 橫批:能省則省
           *
           * 二、Lambda 表達式需要“函數(shù)式接口”的支持
           * 函數(shù)式接口:接口中只有一個抽象方法的接口,稱為函數(shù)式接口。 可以使用注解 @FunctionalInterface 修飾
           *     可以檢查是否是函數(shù)式接口
           */
          public class TestLambda2 {
           
           @Test
           public void test1(){
            int num = 0;//jdk 1.7 前,必須是 final
            
            Runnable r = new Runnable() {
             @Override
             public void run() {
              System.out.println("Hello World!" + num);
             }
            };
            
            r.run();
            
            System.out.println("-------------------------------");
            
            Runnable r1 = () -> System.out.println("Hello Lambda!");
            r1.run();
           }
           
           @Test
           public void test2(){
            Consumer<String> con = x -> System.out.println(x);
            con.accept("我大尚硅谷威武!");
           }
           
           @Test
           public void test3(){
            Comparator<Integer> com = (x, y) -> {
             System.out.println("函數(shù)式接口");
             return Integer.compare(x, y);
            };
           }
           
           @Test
           public void test4(){
            Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
           }
           
           @Test
           public void test5(){
          //  String[] strs;
          //  strs = {"aaa", "bbb", "ccc"};
            
            List<String> list = new ArrayList<>();
            
            show(new HashMap<>());
           }

           public void show(Map<String, Integer> map){
            
           }
           
           //需求:對一個數(shù)進行運算
           @Test
           public void test6(){
            Integer num = operation(100, (x) -> x * x);
            System.out.println(num);
            
            System.out.println(operation(200, (y) -> y + 200));
           }
           
           public Integer operation(Integer num, MyFun mf){
            return mf.getValue(num);
           }
          }


          posted on 2018-03-06 08:32 長春語林科技 閱讀(138) 評論(0)  編輯  收藏 所屬分類: java8
          <2018年3月>
          25262728123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

           

          長春語林科技歡迎您!

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 庆元县| 长葛市| 内黄县| 长丰县| 灌阳县| 博白县| 长兴县| 富顺县| 招远市| 奉节县| 开鲁县| 繁昌县| 本溪市| 海淀区| 鄂托克前旗| 景宁| 保靖县| 镇江市| 邯郸市| 许昌市| 昌宁县| 龙海市| 柯坪县| 鄢陵县| 大渡口区| 永济市| 施甸县| 道孚县| SHOW| 突泉县| 缙云县| 靖江市| 滨海县| 唐河县| 石城县| 兴山县| 田林县| 嘉善县| 贺兰县| 聂拉木县| 平邑县|