美文网首页
Lambda 表达式

Lambda 表达式

作者: zhangqinghua | 来源:发表于2018-09-17 15:28 被阅读0次

    实例

    1. 使用 Lambda 表达式对列表进行迭代

      // Java 8之前:
      List features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");
      for (String feature : features) {
          System.out.println(feature);
      }
      
      // Java 8之后:
      List features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");
      features.forEach(n -> System.out.println(n));
      
      // 使用Java 8的方法引用更方便,方法引用由::双冒号操作符标示,
      // 看起来像C++的作用域解析运算符
      features.forEach(System.out::println);
      
    2. lambdas 实现 Runnable 接口

      // 1.1使用匿名内部类  
      new Thread(new Runnable() {  
          @Override  
          public void run() {  
              System.out.println("Hello world !");  
          }  
      }).start();  
      
      // 1.2使用 lambda expression  
      new Thread(() -> System.out.println("Hello world !")).start();  
      
      // 2.1使用匿名内部类  
      Runnable race1 = new Runnable() {  
          @Override  
          public void run() {  
              System.out.println("Hello world !");  
          }  
      };  
      
      // 2.2使用 lambda expression  
      Runnable race2 = () -> System.out.println("Hello world !");  
      
      // 直接调用 run 方法(没开新线程哦!)  
      race1.run();  
      race2.run();  
      
    3. 使用 Lambda 表达式进行事件处理

      // Java 8之前:
      JButton show =  new JButton("Show");
      show.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              System.out.println("Event handling without lambda expression is boring");
          }
      });
      
      // Java 8方式:
      show.addActionListener((e) -> {
          System.out.println("Light, Camera, Action !! Lambda expressions Rocks");
      });
      
    4. 使用 Lambdas 排序集合

      String[] players = {"Rafael Nadal", "Novak Djokovic",   
          "Stanislas Wawrinka", "David Ferrer",  
          "Roger Federer", "Andy Murray",  
          "Tomas Berdych", "Juan Martin Del Potro",  
          "Richard Gasquet", "John Isner"};  
      
      // 1.1 使用匿名内部类根据 surname 排序 players  
      Arrays.sort(players, new Comparator<String>() {  
          @Override  
          public int compare(String s1, String s2) {  
              return (s1.substring(s1.indexOf(" ")).compareTo(s2.substring(s2.indexOf(" "))));  
          }  
      });  
      
      // 1.2 使用 lambda expression 排序,根据 surname  
      Comparator<String> sortBySurname = (String s1, String s2) ->   
          ( s1.substring(s1.indexOf(" ")).compareTo( s2.substring(s2.indexOf(" ")) ) );  
      Arrays.sort(players, sortBySurname);  
      
      // 1.3 或者这样,怀疑原作者是不是想错了,括号好多...  
      Arrays.sort(players, (String s1, String s2) ->   
          ( s1.substring(s1.indexOf(" ")).compareTo( s2.substring(s2.indexOf(" ")) ) )   
          );  
      
      // 2.1 使用匿名内部类根据 name lenght 排序 players  
      Arrays.sort(players, new Comparator<String>() {  
          @Override  
          public int compare(String s1, String s2) {  
              return (s1.length() - s2.length());  
          }  
      });  
      
      // 2.2 使用 lambda expression 排序,根据 name lenght  
      Comparator<String> sortByNameLenght = (String s1, String s2) -> (s1.length() - s2.length());  
      Arrays.sort(players, sortByNameLenght);  
      
      // 2.3 or this  
      Arrays.sort(players, (String s1, String s2) -> (s1.length() - s2.length()));  
      
      // 3.1 使用匿名内部类排序 players, 根据最后一个字母  
      Arrays.sort(players, new Comparator<String>() {  
          @Override  
          public int compare(String s1, String s2) {  
              return (s1.charAt(s1.length() - 1) - s2.charAt(s2.length() - 1));  
          }  
      });  
      
      // 3.2 使用 lambda expression 排序,根据最后一个字母  
      Comparator<String> sortByLastLetter =   
          (String s1, String s2) ->   
              (s1.charAt(s1.length() - 1) - s2.charAt(s2.length() - 1));  
      Arrays.sort(players, sortByLastLetter);  
      
      // 3.3 or this  
      Arrays.sort(players, (String s1, String s2) -> (s1.charAt(s1.length() - 1) - s2.charAt(s2.length() - 1)));
      

    相关文章

      网友评论

          本文标题:Lambda 表达式

          本文链接:https://www.haomeiwen.com/subject/njolnftx.html