【lambda 表达式方法引用】

lambda 表达式方法引用

方法引用满足条件:如果lambda表达式方法体已经有其他方法实现那么则可以使用方法引用

mySum方法引用sum方法

public class Demo {
    public static void main(String[] args) {
        // int c = a + b;
        int c = mySum(10,20);
        System.out.println(c);
    }
    public static int mySum(int a, int b){
        // return a + b;
        return sum(a, b);
    }
    public static int sum(int a,int b){
        return a + b;
    }
}

输出

30

lambda 表达式静态方法引用

静态方法引用使用 类名::方法名

Person类

public class Person {
    private String nickname;
    private int gender;
    public Person() {
    }
    public Person(String nickname, int gender) {
        this.nickname = nickname;
        this.gender = gender;
    }
    public String getNickname() {
        return nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    public int getGender() {
        return gender;
    }
    public void setGender(int gender) {
        this.gender = gender;
    }
    public static int staticSum(int a,int b){
        return a + b;
    }
    public int sum(int a , int b){
        return a + b;
    }
    @Override
    public String toString() {
        return "Person{" +
                "nickname='" + nickname + '\'' +
                ", gender=" + gender +
                '}';
    }
}

测试类

public class LambdaTest {
    @Test
    public void test01(){
        int i = Person.staticSum(10, 20);
        BiFunction<Integer, Integer, Integer> biFunction = Person::staticSum;
        Integer sum = biFunction.apply(10, 20);
        System.out.println(sum);
    }
    @Test
    public void test02(){
        BiFunction<Integer,Integer,Integer> biFunction = (x, y) -> x + y;
        Integer sum = biFunction.apply(10, 20);
        System.out.println(sum);
    }
}

输出

30

在这里插入图片描述

lambda 表达式实例方法引用

实例方法引用使用 实例名::方法名

public class LambdaTest {
@Test
    public void test03(){
        Person person = new Person();
        BiFunction<Integer, Integer, Integer> biFunction = person::sum;
        Integer sum = biFunction.apply(10, 20);
        System.out.println(sum);
    }
}

输出

30

在这里插入图片描述

lambda 表达式无参构造方法引用

无参构造方法引用使用 类名::new

public class LambdaTest {
@Test
    public void test04(){
        Supplier<Person> supplier = Person::new;
        Person person = supplier.get();
        person.setNickname("张三");
        person.setGender(1);
        System.out.println(person);
    }
    @Test
    public void test05(){
        Supplier<Person> supplier = () -> new Person();
        Person person = supplier.get();
        System.out.println(person);
    }
}

输出

Person{nickname='张三', gender=1}

在这里插入图片描述

lambda 表达式有参构造方法引用

有参构造方法引用使用 类名::new

public class LambdaTest {
@Test
    public void test06(){
        BiFunction<String, Integer, Person> biFunction = (nickname, gender) -> new Person(nickname, gender);
        Person person = biFunction.apply("张三",1);
        System.out.println(person);
    }
    @Test
    public void test07(){
        BiFunction<String,Integer,Person> biFunction = Person::new;
        Person person = biFunction.apply("张三",1);
        System.out.println(person);
    }
}

输出

Person{nickname='张三', gender=1}

在这里插入图片描述

lambda 表达式特殊实例方法引用

Person 实现 Comparable接口重写compareTo方法比较两个对象是否相等(相等返回0)

Person类

public class Person implements Comparable{
@Override
    public int compareTo(Object o) {
        Person person = (Person) o;
        int x = this.gender - person.getGender();
        int y = this.nickname.hashCode() - person.getNickname().hashCode();
        if (x != 0){
            return x;
        }else if (y != 0){
            return y;
        }else {
            return 0;
        }
    }
}

普通比较

public class LambdaTest {
@Test
    public void test08(){
        Person person01 = new Person("张三", 1);
        Person person02 = new Person("李四", 0);
        if (person01.compareTo(person02) != 0){
            System.out.println("不是同一个对象");
        }else {
            System.out.println("是同一个对象");
        }
    }
}	

输出

不是同一个对象

lambda 表达式方法引用

@Test
    public void test10(){
        BiFunction<Person, Person,Integer> biFunction = (person01, person02) -> person01.compareTo(person02);
        Person person01 = new Person("张三", 1);
        Person person02 = new Person("李四", 0);
        Integer num = biFunction.apply(person01, person02);
        if (num != 0){
            System.out.println("不是同一个对象");
        }else {
            System.out.println("是同一个对象");
        }
    }
    @Test
    public void test11(){
    	// 特殊实例方法引用 第一个参数方法的参数是第二个参数
        BiFunction<Person, Person,Integer> biFunction = Person::compareTo;
        Person person01 = new Person("张三", 1);
        Person person02 = new Person("李四", 0);
        Integer num = biFunction.apply(person01, person02);
        if (num != 0){
            System.out.println("不是同一个对象");
        }else {
            System.out.println("是同一个对象");
        }
    }
}

输出

不是同一个对象

在这里插入图片描述

lambda 表达式数组引用

@Test
    public void test12(){
        // String[] names = new String[10];
        Function<Integer,String[]> function = (count) -> new String[count];
        String[] names = function.apply(10);
        System.out.println(names.length);
    }
    @Test
    public void test13(){
        Function<Integer,String[]> function = String[]::new;
        String[] names = function.apply(10);
        System.out.println(names.length);
    }
}

输出

10

在这里插入图片描述


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rita_zzf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值