zoukankan      html  css  js  c++  java
  • Lambda表达式语法

    基础语法:‘->’Lambda操作符
    * 左侧:Lambda表达式的参数列表 对应接口中方法中的参数列表中的参数(比如nice1中MyPredict这个接口中的方法)
    * 右侧:Lambda表达式中所需要执行的功能。 对应接口中方法的实现(比如nice1中MyPredict这个接口中的方法)
    *
    * 语法格式1:无参数,无返回值 ()->System.out.println("aaa")
    * 语法格式2:有一个参数,无返回值
    * 语法格式3:只有一个参数小括号可以不写,无返回值
    * 语法格式4:两个以上参数,并且Lambda体中多条语句--->test3
    * 语法格式5:若Lambda中只有一个语句,return和大括号都可以不写,参数列表中的参数类型可以不写,JVM编译器可以通过上下文推断出类型

    package airycode_java8.nice2;
    
    import org.junit.Test;
    
    import java.util.Comparator;
    import java.util.function.Consumer;
    
    /**
     * 基础语法:‘->’Lambda操作符
     * 左侧:Lambda表达式的参数列表    对应接口中方法中的参数列表中的参数(比如nice1中MyPredict这个接口中的方法)
     * 右侧:Lambda表达式中所需要执行的功能。  对应接口中方法的实现(比如nice1中MyPredict这个接口中的方法)
     *
     * 语法格式1:无参数,无返回值    ()->System.out.println("aaa")
     * 语法格式2:有一个参数,无返回值
     * 语法格式3:只有一个参数小括号可以不写,无返回值
     * 语法格式4:两个以上参数,并且Lambda体中多条语句--->test3
     * 语法格式5:若Lambda中只有一个语句,return和大括号都可以不写,参数列表中的参数类型可以不写,JVM编译器可以通过上下文推断出类型
     *
     *
     *
     *
     *
     *
     *
     */
    public class TestLambda2 {
    
       @Test
       public void test1(){
           int num = 0;//jdk1.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("airycode");
       }
    
       @Test
       public void test3(){
    
    //       Comparator<Integer> com = (x,y)->{
    //           System.out.println("函数式接口");
    //           return Integer.compare(x,y);
    //       };
    
           Comparator<Integer> com = (x,y)-> Integer.compare(x,y);
    
           Comparator<Integer> com2 = (Integer x,Integer y)-> Integer.compare(x,y);
    
       }
    
       @Test
       public void test5(){
           //必须这样的写法,不能拆开
           String[] strs = {"aaa","bbb"};
    
       }
    
       @Test
       public void test6(){
           Integer operation = operation(100, (x) -> x * x);
           System.out.println(operation);
           System.out.println("----------------");
           Integer operation2 = operation(200, (x) -> x +200);
           System.out.println(operation2);
       }
    
       public Integer operation(Integer num,MyFun myFun){
           return myFun.getValue(num);
       }
    
    
    
    }
    
    
    package airycode_java8.nice2;
    
    /**
     * Created by admin on 2019/1/2.
     */
    @FunctionalInterface
    public interface MyFun<T> {
    
        public Integer getValue(Integer num);
    }
    

      

  • 相关阅读:
    红帽中出现”This system is not registered with RHN”的解决方案
    消息队列
    安装nmap
    工作后才知道
    虚拟空间测速
    Update 两个表之间数据更新 (转)
    破解
    一步步打造自己的代码生成器
    Table轻松实现报表 转载
    sql使用convert转化
  • 原文地址:https://www.cnblogs.com/airycode/p/10231607.html
Copyright © 2011-2022 走看看