方法引用是lambda表达式的一种特殊形式,如果正好有某个方法满足一个lambda表达式的形式(函数式接口的参数),那就可以将这个lambda表达式用方法引用的方式表示,但是如果这个lambda表达式的比较复杂就不能用方法引用进行替换。实际上方法引用是lambda表达式的一种语法糖。
方法引用的定义
简单地说,就是一个Lambda表达式。方法引用提供了一种引用而不执行方法的方式,它需要由兼容的函数式接口构成的目标类型上下文。计算时,方法引用会创建函数式接口的一个实例。(就是说,一个方法的某个参数是函数式接口的类型,可以用Lambda表达式构建,或者方法引用来构建,其实最后的功能都是创建一个实现函数式接口的默认方法的实现类而已)
当Lambda表达式中只是执行一个方法调用时,不用Lambda表达式,直接通过方法引用的形式可读性更高一些。
因为我们的Lambda表达式可能仅仅调用一个已经存在的方法(比如只有:System.out.println(str);),那么我们就允许通过方法名来引用这个已经存在的方法。
1.对象::实例方法名
2.类::静态方法名
3.类::实例方法名
4.类::new (构造器引用)
5.数组引用。Type::new (new String[]::new)
实例讲解
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Student {
private String stuName; //学生姓名
private Integer stuAge; //学生年龄
private Double stuGrade;//学生成绩
//单独写一个 只有一个参数的构造函数
public Student(String stuName) {
this.stuName = stuName;
}
}
对象::实例方法名
public static void main(String[] args) {
Student stu = new Student("张三", 18, 85.0);
//Lambda写法
Supplier<String> supp1=()->stu.getStuName();
//方法引用的写法
Supplier<String> supp2 = stu::getStuName;
System.out.println(supp2.get()); //张三
}
//不接受参数但有返回值类型的函数式接口
@FunctionalInterface
public interface Supplier<T> {
/**
* Gets a result.
* @return a result
*/
T get();
}
类::静态方法名
Comparator<Integer> com1=(x,y)->Integer.compare(x, y); //[原来写法]
Comparator<Integer> com2 = Integer::compare; //[使用方法引用]
System.out.println(com2.compare(1, 2)); //-1
接收2个泛型参数,返回一个int值
接收2个int类型参数,返回int类型的静态方法
类::实例方法名
BiPredicate<String, String> biPre1=(str1,str2)->str1.equals(str2); //[原来写法]
BiPredicate<String, String> biPre2 = String::equals; //[使用方法引用]
System.out.println(biPre2.test("aaa", "aaa")); //true
此处需要注意:test函数传入的两个参数str1和str2,必须满足:
第一个实例(str1)调用方法
第二个实例(str2)作为参数传入方法
就像str1.equals(str2)
总结:以上三总基本符合共同原则:参数列表和lambda表达式都一样
类::new (构造器引用)
public static void main(String[] args) {
//Supplier<Student> supp=()->new Student(); //[原来写法]
//Function<String, Student> supp = (name) -> new Student(name); //[原来写法]
Supplier<Student> supp = Student::new; //无参构造
Function<String, Student> funOneParam = Student::new; //有参构造
//我们发现 如果参数实在太多(超过3个),就不用使用构造函数引用了 (除非自定义一个函数式接口)
//BiFunction<String,Integer,Function<Double,Student>> suppAllParam = Student::new; //全参构造
System.out.println(supp.get()); //Student(stuName=null, stuAge=null, stuGrade=null)
System.out.println(funOneParam.apply("fsx")); //Student(stuName=fsx, stuAge=null, stuGrade=null)
}
数组引用。Type::new (new String[]::new)
public static void main(String[] args) {
//Function<Integer, String[]> supp = x -> new String[x];
Function<Integer, String[]> supp = String[]::new;
String[] array = supp.apply(10);
System.out.println(array.length); //输出 10
}
参考:
小家java】java8新特性之—方法引用
Java 8之方法引用(Method References)
Java四种方法引用