JDK8新特性(一)Lamda表达式

Lamda表达式

概念

​ lamda表达式,从本质上来讲是一个匿名函数。可以使用这个匿名函数,实现接口中的方法。对接口进行非常简洁的实现,从而简化代码。

使用场景

​ 接口的实现可以实现接口的实现类,也可以使用匿名内部类,lamda表达式实现接口比前两种都简单。使用lamda表达式,是用来简化接口实现的

使用范围

​ 必须是一个函数式接口:必须只有一个必须实现的方法。

@FunctionlInterface标注在函数式接口上。

1、基础语法

(参数)->{
	实现方法;
}
  • 无参无返回值
@FunctionalInterface
public interface NoneReNoneParam {
    void test();
}
----
public static void main(String[] args) {
    NoneReNoneParam lamda1 = ()->{
        System.out.println("无参无返回值");
    };
    lamda1.test();
}
  • 一个参数无返回值
@FunctionalInterface
public interface NoneReOneParam {
    void test(int i);
}
----
public static void main(String[] args) {
    NoneReOneParam lamda2 = (int a)->{
        System.out.println("一个参数无返回值。"+"参数:"+a);
    };
    lamda2.test(13);
}
  • 两个参数无返回值
@FunctionalInterface
public interface NoneReTwoParam {
    void test(int a,int b);
}
----
    public static void main(String[] args) {
    NoneReTwoParam lamda3 = (int a,int b)->{
        System.out.println("两个参数无返回值。"+"参数:"+a+"---"+b);
    };
    lamda3.test(13,12);
}
  • 无参数有返回值
@FunctionalInterface
public interface ReNoneParam {
    int test();
}
---
    public static void main(String[] args) {
    ReNoneParam lamda3 = ()->{
        return 12;
    };
    int value = lamda3.test();
    System.out.println("返回值"+value);
}
  • 一个参数有返回值
@FunctionalInterface
public interface ReOneParam {
    int test(int a);
}
---
    public static void main(String[] args) {
    ReOneParam lamda4 = (int a)->{
        System.out.println("一个参数:"+a);
        return 10;
    };
    int value = lamda4.test(13);
    System.out.println("返回值"+value);
}
  • 两个参数有返回值
@FunctionalInterface
public interface ReTwoParam {
    int test(int a,int b);
}
---
    public static void main(String[] args) {
    ReTwoParam lamda5 = (int a,int b)->{
        return a+b;
    };
    int value = lamda5.test(13,12);
    System.out.println("返回值"+value);
}

2、语法简化

接口中以定义了参数列表和返回值类型

  • 参数列表
ReTwoParam lamda5 =(a,b)->{
    return a+b;
};
  • 只有一个参数
ReOneParam lamda4 =a->{
    System.out.println("一个参数:"+a);
    return 10;
};
  • 方法体:只有一句逻辑
NoneReOneParam lamda2 = a->System.out.println("一个参数无返回值。"+"参数:"+a);

ReTwoParam lamda5 = a,b-> a+b;

3、方法引用

  • lamda表达式用与简化接口实现,不应该出现较复杂的逻辑。这时应该引用其他处实现该方法逻辑的方法,叫做方法引用。

  • 返回值相同,参数列表相同,语句逻辑相同,可使用方法引用

类名::方法

1、静态方法引用

  • 接口
@FunctionalInterface
public interface Calculate {
    int test(int a,int b);
}
  • 方法实现
public class SomeInterface {
    public static int caculate(int a,int b){
        if (a>b) {
            return a-b;
        }else if (a<b){
            return b-a;
        }
        return a+b;
    }
}
  • 接口实现
public static void main(String[] args) {
    //方法调用
    Calculate lamda1 = (a,b)->SomeInterface.calculate(a,b);  
    //方法引用
    Calculate lamda2 =SomeInterface::calculate;
}

2、非静态方法

@FunctionalInterface
public interface Calculate {
    int test(int a,int b);
}

public class SomeInterface {
    public int calculate(int a,int b){
        if (a>b) {
            return a-b;
        }else if (a<b){
            return b-a;
        }
        return a+b;
    }
}

public static void main(String[] args) {
    Calculate lamda2 =new SomeInterface()::calculate;
    System.out.println(lamda2.test(12, 3));
}

3、构造方法引用

@FunctionalInterface
public interface PersonTest {
    Person test(String name);
}

public class Person {
    private String name;
    private Integer age;
    public Person(String name) {
        this.name = name;
    }
    public Person() {}
    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}

public static void main(String[] args) {
    PersonTest lamda = Person::new;
    lamda.test("hhhhhh");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值