Simple understanding of lambad expression

Lambad expression: It is a new feature in Java8. The introduction of lambad expression in Java8 enables Java to be functionally programmed, which is a substantial step forward in concurrent performance.
1. The basic grammar of lambad
(parameter) -> expression or (parameter) -> {method body}
2. The parameter list of the
parameter list allows the omission of the parameter type. If there is only one parameter in the parameter list, the ( ) Can also be omitted
. 3. The arrow (->)
must be formed by the English dash and greater than symbol
4. Code block
If the code block contains only one statement, the lambad expression allows to omit the curly braces of the code block, then this statement is Do not use curly braces to indicate the end of the statement.
5. The return value
lambad code block has only one return statement, and you can even omit the return keyword. The
lambad expression requires a return value, and there is only one omitted return statement in the code block, lambad will automatically return The result of this statement

As long as this interface has an abstract method, we can use lambad expressions to write

The lambad expression must be used when there is only one abstract method in the interface. The following example simply implements the lambad expression

public class Demo6 {
    
    
    public static void main(String[] args) {
    
    
        InterA interA1 =()->{
    
    
            System.out.println("lambad-show");
        };
        interA1.show();
        InterA interA2 = ()-> System.out.println("lambad简化版");
        interA2.show();
       //匿名内部类,这个也可以实现接口,但是他对于
       //接口中有几个抽象方法没有要求没几个都可以
        InterA interA = new InterA() {
    
    
            @Override
            public void show() {
    
    
                System.out.println("interA-show");
            }
        };
        interA.show();
    }
}
interface  InterA{
    
    
    public void show();
}

For lambad, there are parameters, no parameters, and the introduction of return value

public class Demo7 {
    
    
    public static void main(String[] args) {
    
    
        //对于没有参数的情况
        InterA1 interA1 = () -> {
    
    
            System.out.println("没有参数的");
        };
        interA1.show();
        //简化版,如果
        InterA1 interA12 = () -> System.out.println("没有参数的简化版");
        interA12.show();

        //对于有一个参数的情况
        InterB interB = (a)->{
    
    
            System.out.println("有一个参数"+a);
        };
        interB.showB(3);
        //简化版,如果只有一个参数,可以把参数的()省略
        InterB interB1 = a->System.out.println("有一个参数的简化版"+a);
        interB1.showB(3);

        //对于有两个参数
        InterC interC = (a,b)->{
    
    
            System.out.println("有两个参数"+a+""+b);
        };
        interC.showC(3,4);
        //简化版
        InterC interC1 = (a,b)-> System.out.println("简化版"+a+""+b);
        interC1.showC(3,4);

        //有返回值
        InterD interD = (a,b)->{
    
    
            return a+b;
        };
        int sum1 = interD.showD(3,4);
        System.out.println(sum1);
        //简化版,对于有返回值的,如果一句话,也可以不写return
        InterD interD1 = (a,b)->a+b;
        int sum2 = interD1.showD(2,3);
        System.out.println(sum2);

        //接口作为参数,匿名对象
        fun2(new InterC() {
    
    
            @Override
            public void showC(int a, int b) {
    
    
                System.out.println(a+b);
            }
        });
        //lambad实现接口作为参数
        fun2((i,j)->{
    
    System.out.println("lambad实现接口作为参数");});
    }
    public static void fun2(InterC interC){
    
    

    }
}
interface InterA1{
    
    
    public void show();
}
interface  InterB{
    
    
    public void showB(int a);
}
interface  InterC{
    
    
    public void showC(int a,int b);
}
interface InterD{
    
    
    public int showD(int a,int b);
}
//主要作用就是检查当前接口是不是函数接口
@FunctionalInterface
interface InterE{
    
    
    public void showF(int a);
}

All omitted in the application are parameters

/*
省略的都是参数
 */
public class Demo8 {
    
    
    public static void main(String[] args) {
    
    
        //1.引用类方法
       Test.fun1();
        //2.引用特定对象的实例方法
        Test.fun2();
        //3.引用某类对象的实例方法
        Test.fun3();
        //4.引用构造方法
        Test.fun4();
    }
}
interface Convert{
    
    
  Integer convert(String name);
}
interface IA{
    
    
    public void show(String message);
}
class A{
    
    
    public void play(String i){
    
    
        System.out.println("这是A的方法"+i);
    }
}
interface IB{
    
    
    String subString1(String string,int start,int end);
}
interface IC{
    
    
    Object show(String name,int age);
}
class Test{
    
    
  public static void fun1(){
    
    
      Convert convert = name -> Integer.valueOf(name);
      convert.convert("111");
      //简化
      Convert convert1 =  Integer::valueOf;
      convert1.convert("22");
  }
  public static void fun2(){
    
    
      //正常
      IA ia = (mes)->new A().play(mes);
      ia.show("mes");
      //简化
      IA ia1 = new A()::play;
      ia1.show("mes");
  }
  public static void fun3(){
    
    
      IB ib = (string,start,end)->string.substring(start,end);
      String str = ib.subString1("aswe",0,2);
      System.out.println(str);
      //简化
      IB ib1 =String::substring;
      String str1 = ib1.subString1("aff",0,1);
      System.out.println(str1);
  }
  public static void fun4(){
    
    
      IC ic = (name,age)->new Person5(name,age);
      Object o1 =ic.show("bing",10);
      System.out.println(o1);
      //简化
      IC ic1 = Person5::new;
      Object o2 = ic1.show("aaa",20);
      System.out.println(o2);
  }
}
class Person5{
    
    
    String name;
    int age;

    @Override
    public String toString() {
    
    
        return "Person5{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Person5(String name, int age) {
    
    
        this.name = name;
        this.age = age;
    }
}

Guess you like

Origin blog.csdn.net/m0_51327764/article/details/109264409