Java8新特性:Lambda表达式

JAVA学习网 2019-06-18 06:00:05

Lambda表达式,案例一:new Thread(() -> System.out.println("thread"));

Lambda表达式,案例二:由参数/箭头和主体组成:

(Apple a1, Apple a2)  -> a1.getWeight().compareTo(a2.getWeight());

之前的代码形式:

Comparator<Apple> byWeight = new Comparator<Apple>() {
  public int compare(Apple a1, Apple a2) {

    return a1.getWeight().compareTo(a2.getWeight());

  }

}

针对这种情形,我们怎么理解呢?其实很简单,上看一下上述lambda表达式的语法:() -> {}(): 括号就是接口方法的括号,接口方法如果有参数,也需要写参数。只有一个参数时,括号可以省略。-> : 分割左右部分的,没啥好讲的。{} : 要实现的方法体。只有一行代码时,可以不加括号,可以不写return。在控制台打印,不返回任何值(看起来像是返回void)  

函数式接口就是只显式声明一个抽象方法的接口。为保证方法数量不多不少,java8提供了一个专用注解@FunctionalInterface,这样,当接口中声明的抽象方法多于或少于一个时就会报错。如下图所示:

Lambda表达式和函数式接口结合

步骤:

  1. 新建无参函数式接口(先演示无参);
  2. 新建包含属性为函数式接口的类;
  3. 实现函数式接口;
  4. 测试函数式接口的方法;

新建无参函数式接口

@FunctionalInterface
public interface InterfaceWithNoParam {
    void run();
}

新建包含属性为函数式接口的类

public class TestJava8{

    //匿名内部类
    InterfaceWithNoParam param1 = new InterfaceWithNoParam() {
        @Override
        public void run() {
            System.out.println("通过匿名内部类实现run()");
        }
    };
    //Lambda表达式
            //空括号表示无参
    InterfaceWithNoParam param = () -> System.out.println("通过Lambda表达式实现run()") ;
}

测试函数式接口的方法

@Test
public void testIntfaceWithNoparam() {

    this.param.run();
    this.param1.run();
}

运行结果

其他形式的函数式接口及实现

上述内容实现了无参无返回值的函数接口与实现,当然还有其他形式:

有参无返回值

接口

@FunctionalInterface
public interface InterfaceWithParams {
    void run(String s);
}

实现

InterfaceWithParams params = new InterfaceWithParams() {
    @Override
    public void run(String s) {
        System.out.println("通过" + s + "实现run(String)");
    }
};
InterfaceWithParams params1 = (String s) -> System.out.println("通过" + s + "实现run(String)");

测试

    this.params.run("匿名类");
    this.params1.run("Lambda");

运行

无参有返回值

接口

@FunctionalInterface
public interface InterfaceUnVoidWithNoParam {
    String run();
}

实现

InterfaceUnVoidWithNoParam interfaceUnVoidWithNoParam = new InterfaceUnVoidWithNoParam() {
    @Override
    public String run() {
        return "Hello World!";
    }
};
InterfaceUnVoidWithNoParam interfaceUnVoidWithNoParam1 = () -> "Hello Lambda!";

测试

    String s = this.interfaceUnVoidWithNoParam.run();
    System.out.println("返回结果是:"+s);
    String s0 = this.interfaceUnVoidWithNoParam1.run();
    System.out.println("返回结果是:"+s0);

运行

有参有返回值

接口

@FunctionalInterface
public interface InterfaceUnVoidWithParams {
    String run(Integer integer);
}

实现

InterfaceUnVoidWithParams interfaceWithParams = new InterfaceUnVoidWithParams() {
    @Override
    public String run(Integer integer) {
        return String.valueOf(integer);
    }
};
InterfaceUnVoidWithParams interfaceWithParams1 = (Integer integer) -> String.valueOf(integer);

测试

    String s1 = this.interfaceWithParams.run(1);
    System.out.println("您输入的是:"+s1);
    String s2 = this.interfaceWithParams1.run(2);
    System.out.println("您输入的是:"+s2);

 

阅读(2461) 评论(0)