Java8新特性-方法引用

方法引用的使用场景

我们用Lambda表达式来实现匿名方法。但有些情况下,我们用Lambda表达式仅仅是调用一些已经存在的方法,除了调用动作外,没有其他任何多余的动作,在这种情况下,我们倾向于通过方法名来调用它,而Lambda表达式可以帮助我们实现这一要求,它使得Lambda在调用那些已经拥有方法名的方法的代码更简洁、更容易理解。方法引用可以理解为Lambda表达式的另外一种表现形式。

方法引用的分类

类型 语法 对应的Lambda表达式 对象:实例方法引用 inst::instMethod (args) -> inst.instMethod(args) 类名:实例方法引用 类名::instMethod (inst,args) -> 类名.instMethod(args) 类名:静态方法引用 类名::staticMethod (args) -> 类名.staticMethod(args) 类名:构建方法引用 类名::new (args) -> new 类名(args)

1.对象:实例方法引用

实例方法引用,顾名思义就是调用已经存在的实例的方法

// 获取对象属性的值:有返回值
@Test
public void test2(){
    // lombda表达式方式
    Employee emp = new Employee(101, "张三", 18, 9999.99);
    Supplier sup = () -> emp.getName();
    System.out.println(sup.get());

    System.out.println("----------------------------------");

    // 方法表达式
    Supplier sup2 = emp::getName;
    System.out.println(sup2.get());
}
// 输出字符串:无返回值
@Test
public void test1(){
    // lombda表达式方式
    PrintStream ps = System.out;
    Consumer con = (str) -> ps.println(str);
    con.accept("Hello World!");

    System.out.println("--------------------------------");

    // 方法表达式
    Consumer con2 = ps::println;
    con2.accept("Hello Java8!");
    Consumer con3 = System.out::println;
}

2.类:实例方法引用

这种方式需要注意的是, 若Lambda 的参数列表的第一个参数,是实例方法的调用者,第二个参数(或无参)是实例方法的参数时,则可以使用格式: ClassName::MethodName

//类名 :: 实例方法名
@Test
public void test2(){
    // x是exquals实例方法的调用者,y是实例方法的参数
    BiPredicate bp = (x, y) -> x.equals(y);
    System.out.println(bp.test("abcde", "abcde"));

    System.out.println("-----------------------------------------");

    BiPredicate bp2 = String::equals;
    System.out.println(bp2.test("abc", "abc"));

    System.out.println("-----------------------------------------");

    // e是show实例方法的调用者,y是实例方法的参数
    Function fun = (e) -> e.show();
    System.out.println(fun.apply(new Employee()));

    System.out.println("-----------------------------------------");

    Function fun2 = Employee::show;
    System.out.println(fun2.apply(new Employee()));

}

public class Employee {
    public Employee() {
    }
    public String show() {
        return "测试方法引用!";
    }
}

3.类:静态方法引用

@Test
public void test3(){
    Comparator com = (x, y) -> Integer.compare(x, y);

    System.out.println("-------------------------------------");

    Comparator com2 = Integer::compare;
}

@Test
public void test4(){
    BiFunction fun = (x, y) -> Math.max(x, y);
    System.out.println(fun.apply(1.5, 22.2));

    System.out.println("--------------------------------------------------");

    BiFunction fun2 = Math::max;
    System.out.println(fun2.apply(1.2, 1.5));
}

4.类名:构建方法引用

构造器的参数列表,需要与函数式接口中参数列表保持一致

@Test
public void test6(){
    Supplier sup = () -> new Employee();
    System.out.println(sup.get());

    System.out.println("------------------------------------");

    Supplier sup2 = Employee::new;
    System.out.println(sup2.get());
}

@Test
public void test7(){
    Function fun = Employee::new;

    BiFunction fun2 = Employee::new;
}
// 同样也适用于数组
@Test
public void test8(){
    Function fun = (args) -> new String[args];
    String[] strs = fun.apply(10);
    System.out.println(strs.length);

    System.out.println("--------------------------");

    Function fun2 = Employee[] :: new;
    Employee[] emps = fun2.apply(20);
    System.out.println(emps.length);
}

Original: https://www.cnblogs.com/mrxccc/p/16504733.html
Author: 狮子挽歌丿
Title: Java8新特性-方法引用

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/588662/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球