保定seo建站,网站程序语言那个好,广阳区建设局网站,九江网站建设求职简历Java8新特性2——方法引用 注#xff1a;以下内容基于Java 8#xff0c;所有代码都已在Java 8环境下测试通过 目录#xff1a;
Java8新特性1——函数式接口lambda表达式方法引用Stream
1. 方法引用
方法引用提供了一种替代 lambda 表达式的语法#xff0c;允许以更…Java8新特性2——方法引用 注以下内容基于Java 8所有代码都已在Java 8环境下测试通过 目录
Java8新特性1——函数式接口lambda表达式方法引用Stream
1. 方法引用
方法引用提供了一种替代 lambda 表达式的语法允许以更简洁的方式使用 lambda 表达式特别是在需要传递方法或者函数作为参数时。
方法引用本质是 lambda 表达式的语法糖如果一个 lambda 表达式仅仅是调用了另外一个方法此时可用方法引用替换此 lambda 表达式。
方法引用仅仅是简化了 lambda 表达式的写法因此方法引用并不能脱离 lambda 表达式单独使用。
2. 使用方法
方法引用使用引用运算符 :: 指向一个方法有以下几种常见写法
引用构造函数引用静态方法引用成员函数引用某个类型的任意对象的实例方法
3. 引用构造函数
分为构造器引用和数组构造函数引用。
3.1 构造器引用
语法格式 类名::new 如 lambda表达式 () - new String() 等价于 构造器引用 String::new 使用示例
public class Main {public static void main(String[] args) {MyInterface myClassLambda (x) - new MyClass(x);//lambda 表达式myClassLambda.f(10);MyInterface myClassFunction MyClass::new;//方法引用myClassFunction.f(20);}
}class MyClass {/*** 参数整型* 返回值MyClass类型*/MyClass(int arg) {System.out.println(有参构造器参数是 arg);}
}FunctionalInterface
interface MyInterface {/*** 参数整型* 返回值MyClass类型*/MyClass f(int a);
}3.2 数组构造函数引用
语法格式 数据类型[]::new 如 lambda表达式 () - new int[] 等价于 数组构造函数引用 int[]::new 使用示例
import java.util.function.IntFunction;public class Main {public static void main(String[] args) {IntFunctionint[] intArrayLambda (len) - new int[len];//lambda 表达式intArrayLambda.apply(10);IntFunctionint[] intFunction int[]::new;//方法引用intFunction.apply(10);}
}注IntFunction 是 Java 内置的函数式接口可用于创建泛型数组。
4.引用静态方法
语法格式 类名::静态方法名 如 lambda表达式 (x) - Math.sin(x) 等价于 方法引用 Math::sin 使用示例
public class Main {public static void main(String[] args) {MyInterface sinLambda (x) - Math.sin(x);//lambda 表达式MyInterface sinFunction Math::sin;//方法引用System.out.println(sinLambda.sin(1.2));System.out.println(sinFunction.sin(1.2));}
}FunctionalInterface
interface MyInterface {double sin(double x);
}5. 引用成员函数
可以分为以下三种
使用对象使用 super 关键字引用超类使用 this 关键字
以 this 关键字为例另外两种类似
语法格式 this::成员函数名 如 lambda表达式 (s) - this.fun(s) 等价于 方法引用 this::fun 使用示例
public class Main extends Father {public static void main(String[] args) {new Main().test();}private void test() {//lambda 表达式MyInterface lambda1 (s) - new Main().fun(s);MyInterface lambda2 (s) - super.fun(s);MyInterface lambda3 (s) - this.fun(s);lambda1.f(lambda1);lambda2.f(lambda2);lambda3.f(lambda3);//方法引用MyInterface function1 new Main()::fun;MyInterface function2 super::fun;MyInterface function3 this::fun;function1.f(function1);function2.f(function2);function3.f(function3);}void fun(String s) {System.out.println(调用子类中的 fun: s);}
}class Father {void fun(String s) {System.out.println(调用父类中的 fun: s);}
}FunctionalInterface
interface MyInterface {void f(String s);
}6. 引用某个类型的任意对象的实例方法
使用示例
import java.util.Arrays;public class Main {public static void main(String[] args) {String[] strings {abc, ghi, def};Arrays.sort(strings, String::compareToIgnoreCase);for (String s : strings) {System.out.println(s);}}
}