java Lambda表达式

Lambda表达式

1.Java 8(Java 1.8)提供的新语法

2.相当于一个简化版的匿名内部类,有人也称为匿名方法

3.Java集合提供了Lambda表达式功能.

4.Lambda是功能性接口的简单实现

/*
 * Lambda表达式演示:
 */
public class LambdaDemo01 {

    public static void main(String[] args) {
        //利用匿名内部类"快捷"实现接口
        Foo foo=new Foo(){
            public double test(int a,double b){
                return a+b;
            }
        };
        System.out.println(foo.test(4, 5)); //9.0
        
        //使用Lambda表达式实现功能接口
        Foo foo1=(int a,double b)->{return a+b;};
        //当语句块只有一行时候,可以省略{}
        Foo foo2=(a,b)->a+b;
        
        //测试
        System.out.println(foo1.test(5, 6.7)); //11.7
        System.out.println(foo1.test(5, 6.7)); //11.7
        //在Lambda表达式中使用多行语句
        Foo foo3=(a,b)->{a++;b++;return a+b;};
        System.out.println(foo.test(5,6.7));  //11.7
        
        //使用Lambda实现Goo接口
        Goo goo=b->System.out.println(b); //5.5
        //调用
        goo.test(5.5);
        
        //当方法参数是一个接口类型时候,Lambda可以作为方法参数使用
        Goo goo1=b->System.out.println(b); //5.5
        demo(goo1);
        demo(b->System.out.println(b));//简洁 5.5
        //匿名内部类
        demo(new Goo(){
            public void test(double b) {
                System.out.println(b); //5.5
            }
               
        });
        
    }
    
    public static void demo(Goo goo){
        goo.test(5.5);
        
    }

}

/*
 * 功能性接口:只有一个方法的接口
 */
interface Foo{
    double test(int a,double b);
}


interface Goo{
    void test(double b);
}

利用Lambda表达式实现Java API中的功能接口:

public class LambdaDemo02 {

    public static void main(String[] args) {
        
        Runnable r=()->{System.out.println("hello world");};
        Thread t=new Thread(r);
        t.start();
        //最简洁写法
        Thread t1=new Thread(()->System.out.println("hello world!"));
        t1.start();
        
        //利用Lambda实现FileFilter接口
        File dir=new File("D:/");
        //利用file tilter列文件夹恩荣
        File [] files=dir.listFiles(file->file.getName().endsWith(".conf"));
        for(File f:files){
            System.out.println(f);
        }
    }

}
/*
interface Runnable{
    void run();
}*/

功能性接口

只有一个方法的接口,称为功能性接口

public class LambdaDemo03 {

    public static void main(String[] args) {
        

    }

}
/*
 * Functional功能性 Interface接口
 * Java 8 提供了注解,用于约束接口必须为功能性接口,即只能有一个方法
 * 这是一个可选注解,可以不用!
 */
@FunctionalInterface
interface koo{
    void test();
}

Java 8在集合上提供了Lambda表达式遍历.也称为函数式遍历:

public static void main(String[] args) {
        List<String> list=new ArrayList<>();
        list.add("Tom");
        list.add("Jerry");
        list.add("熊大");
        list.add("熊二");
        
        //Java 8 提供了forEach
        list.forEach((name)->System.out.println(name));
        
    }
/**
 * Lambda与匿名内部类,最大的区别是this用法不同:
 * 1.在匿名内部类中this 代表当前匿名内部类的实例
 * 2.在lambda中this代表外部类的实例
 */
public class LambdaDemo05 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}

class Demo{
    int n=5;
    public void hello(){
        //匿名内部类 :this代表当前匿名内部类实例
        //Demo.this 代表外部类的实例,一般时候可以省略 this 引用 Demo.this
        Too t1=new Too(){
            int b=6;
            public void test(int a) {
                System.out.println(a+this.b+Demo.this.n);
                
            }
            
        };
        t1.test(8);
        //lambda中的this是外部类对象的引用 就是Demo.this
        Too t2=(a)->{System.out.println(a+this.n);};
        t2.test(5);
    }
}

interface Too{
    void test(int a);
}