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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/73775.html