JAVA----Lambda表达式的方法和数组引用

一:Lambda静态方法引用

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

首先创建一个Person类,表明属性,封装一下,再写一个无参构造方法,

再写一个有参构造方法。

public class Person {
    private String nickname;
    private  int gander;

    public Person() {

    }

    public Person(String nickname, int gander) {
        this.nickname = nickname;
        this.gander = gander;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public int getGander() {
        return gander;
    }

    public void setGander(int gander) {
        this.gander = gander;
    }
    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 +
                '}';
    }

    @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;
        }
    }
}

写一个测试类LambdaTest进行引用测试:

(因为在方法体中已经有方法体啦,所以在测试类中可以直接引用)

方式一:类名::方法名

方式二:Lambda表达式

public class LambdaTest {
    @Test
    public void test01(){
        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);
    }
}

二: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);
    }
}

三:Lambda表达式的无参构造方法引用

第一个代码段中我们已经声明啦一个无参构造方法方法

这时我们还要重写to.String方法

测试引用:

方式一:类名::new

方式二:Lambda表达式()->new Person()

public class LambdaTest {
public class LambdaTest {
    @Test
    public void test04(){
        Supplier<Person>supplier= Person::new;
        Person person = supplier.get();
        person.setNickname("浩楠");
        person.setGander(1);
        System.out.println(person);
    }

    @Test
    public void test05(){
        Supplier<Person>supplier=()->new Person();
        Person person = supplier.get();
        System.out.println(person);
    }
}

四:Lambda表达式的有参构造方法引用

第一个代码段中我们已经声明啦一个无参构造方法方法

测试引用:

方式一:类名::new

方式二:Lambda表达式(nickname,gander)->new Person(nickname,gander)

public class LambdaTest {
    @Test
    public void test06(){
        BiFunction<String,Integer,Person>biFunction= (nickname, gander) -> new Person(nickname, gander);
        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);
    }
}

五:Lambda表达式特殊实例方法引用

第一个代码段中我们已经实现并重写compareTo方法

条件:第一个参数的方法的参数是第二参数

测试:先不引用,直接比较,再用Lambda表达式引用方法 或类::方法名

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("是同一个对象");
        }
    }
}
  BiFunction<Person, Person, Integer> biPredicate = (person01, person02) -> person01.compareTo(person02);
        Person person01 = new Person("陈浩楠", 1);
        Person person02 = new Person("浩楠", 0);
        Integer num = biPredicate.apply(person01, person02);
        if (num != 0) {
            System.out.println("不是同一个对象");
        } else {
            System.out.println("是同一个对象");
        }
   public void test11() {
        BiFunction<Person, Person, Integer> biPredicate = Person::compareTo;
        Person person01 = new Person("陈浩楠", 1);
        Person person02 = new Person("浩楠", 0);
        Integer num = biPredicate.apply(person01, person02);
        if (num != 0) {
            System.out.println("不是同一个对象");
        } else {
            System.out.println("是同一个对象");
        }

六:Lambda表达式数组引用(字符串数组)

方式一:(参数名)->new String[参数名]

方式二:数组::new

    @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() {
        // String[] names = new String[10];
        Function<Integer, String[]> function = String[]::new;
        String[] names = function.apply(10);
        System.out.println(names.length);
    }


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

星星不喝药

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

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

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

打赏作者

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

抵扣说明:

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

余额充值