/** * 方法引用:若Lambda体中内容有方法已经实现了,可以使用方法引用,lambda表达式的另一种表现形式 * <p> * 三种语法格式 * 1.对象 :: 实例方法名 * <p> * 2.类 :: 静态方法名 * <p> * 3.类 :: 实例方法名 */ public class TestMethodRef { @Test public void test1() { //lambda体中 已经有方法完成该功能 println方法已经有实现 可以使用“方法引用”的形式 //(x)-> ps1.println(x)实现的是Consumer中的accept方法 //注意:函数式接口的参数列表和返回值类型 要与 调用的方法的参数列表和返回值类型相同 即Consumer accept()方法和 //println()方法 PrintStream ps1 = System.out; Consumer<String> consumer = (x) -> ps1.println(x); PrintStream ps2 = System.out; Consumer<String> son = ps2::println; } @Test public void test2() { Employee emp = new Employee("Yan", 24, '男', 14000); Supplier<String> sup = emp::getName; // 与上面语句功能相同 Supplier<String> sup = emp.getName(); String s = sup.get(); System.out.println(s); } //类 :: 静态方法 @Test public void test3() { Comparator<Integer> com1 = (x, y) -> Integer.compare(x, y); Comparator<Integer> com2 = Integer::compare; } //类 :: 实例方法名 @Test public void test4() { BiPredicate<String, String> bp1 = (x, y) -> x.equals(y); //限制条件:只有当(x, y) -> x.equals(y) 第一个参数x作为参数的调用者,第二个参数y为方法参数时,才可以使用这种方式 //ClassName::method BiPredicate<String, String> bp2 = String::equals; } //构造器引用 ClassName::new @Test public void test5() { Supplier<Employee> sup1 = () -> new Employee(); Employee employee = sup1.get();//接口sup1 调用方法 get() get方法被() -> new Employee() 重写 //调用无参构造器 因为 需要和Supplier get方法中的入参和返回值类型保持一致 Supplier<Employee> sup2 = Employee::new; //如需使用其他构造器则 BiFunction<String, Integer, Employee> bg1 = (x, y) -> new Employee(x, y); System.out.println(bg1.apply("Yan", 23)); //调用有参构造器,取决于BiFunction中的apply方法的入参 BiFunction<String, Integer, Employee> bg2 = Employee::new; } //数组引用 type[] :: new @Test public void test6() { Function<Integer, String[]> fun1 = (x) -> new String[x]; String[] apply = fun1.apply(10); System.out.println(apply); Function<Integer, String[]> fun2 = String[]::new; }