Java8新特性之方法引用

一 前言

日常開發中,經常使用到Lambda表達式,例如:

public static void main(String[] args) {
    List<Integer> list = Arrays.asList(1, 5, 10, 4, 2);
    // 打印列表中的每一個數字
    list.forEach((x) -> System.out.println(x));
}

其中(x) -> System.out.println(x)就是使用的Lambda表達式。Lambda表達式可以分為三部分:

  • 左括號:Lambda的形參列表,對應接口的抽象方法的形參列表。
  • 箭頭:Lambda的操作符,可以理解為參數列表和Lambda體的分隔符。
  • Lambda體:即對應接口中的抽象方法的實現方法體。

你是否發現,上述例子的Lambda表達式的Lambda體僅僅調用一個已存在的方法,而不做任何其它事。對於這種情況,通過一個方法名字來引用這個已存在的方法會更加清晰。所以,方法引用應運而生,方法引用是一個更加緊湊,易讀的Lambda表達式,它是Lambda表達式的另外一種表現形式,方法引用的操作符是雙冒號 ::

使用了方法引用,上述例子編寫如下,變得更加緊湊,易讀了。

public static void main(String[] args) {
    List<Integer> list = Arrays.asList(1, 5, 10, 4, 2);
    // 打印列表中的每一個數字
    list.forEach(System.out::println);
}

二 方法引用

方法引用就是通過方法的名字來指向一個方法。它可以使語言的構造更緊湊簡潔,減少冗餘代碼。方法引用的操作符是雙冒號 :: 。方法引用有如下幾種分類:

類型 語法 Lambda表達式
靜態方法引用 類名::靜態方法名 (args) -> 類名.靜態方法名(args)
實例方法引用 實例::實例方法名 (args) -> 實例.實例方法名(args)
對象方法引用 類名::對象方法名 (inst,args) -> 類名.對象方法名(args)
構建方法引用 類名::new (args) -> new 類名(args)

三 實踐

以下例子主要借用學生類來演示,學生類定義如下:

public class Student {

    private String name;
    private Integer age;

    public static int compareByAge(Student s1, Student s2) {
        return s1.age.compareTo(s2.age);
    }

    // 省略屬性get/set方法
}

3.1 靜態方法引用

現假設有50個學生,存放在一個list列表中,現需要對年齡進行從小到大排序。我們一般會寫一個比較器進行排序,如下:

package com.nobody;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @Description
 * @Author Mr.nobody
 * @Date 2021/3/7
 * @Version 1.0
 */
public class Test {
    public static void main(String[] args) {

        List<Student> list = new ArrayList<>();
        // 添加元素省略,測試可自行添加
        // 排序
        list.sort(new StudentAgeComparator());
    }
	// 對學生年齡的比較器
    static class StudentAgeComparator implements Comparator<Student> {
        public int compare(Student s1, Student s2) {
            return s1.getAge().compareTo(s2.getAge());
        }
    }
}

我們發現,List的sort方法接受的參數Comparator是一個函數式接口,則可以用Lambda表達式改為如下形式:

list.sort((s1, s2) -> s1.getAge().compareTo(s2.getAge()));

我們又發現,Student類有個靜態方法compareByAge,其功能和上述Lambda表達式一樣,所以我們可以將以上Lambda表達式改為如下形式:

list.sort((s1, s2) -> Student.compareByAge(s1, s2));

可以看出,最終的Lambda表達式是調用Student類的一個方法,所以,根據靜態方法引用規則,可改為如下形式:

list.sort(Student::compareByAge);

3.2 實例方法引用

即引用已經存在的實例的方法。靜態方法引用類無需實例化,直接用類名來調用,而實例方法引用是要先實例化對象。

如果將Student類的靜態方法compareByAge改為非靜態方法,即:

public int compareByAge(Student s1, Student s2) {
    return s1.age.compareTo(s2.age);
}

則可通過如下方式對學生數組進行排序:

list.sort(new Student()::compareByAge);

3.3 對象方法引用

如果Lambda表達式的參數列表中,第一個參數是實例方法的調用者對象,第二個參數是實例方法的參數時,可使用對象方法引用。例如,String的equals()方法:

public static void main(String[] args) {
    BiPredicate<String, String> bp1 = (x, y) -> x.equals(y);
    boolean test1 = bp1.test("Mr.nobody", "Mr.anybody");
    System.out.println(test1);
    
    BiPredicate<String, String> bp2 = String::equals;
    boolean test2 = bp2.test("Mr.nobody", "Mr.anybody");
    System.out.println(test2);
}

再比如,我們在Student類定義如下實例方法,方法中用到了Srudent對象的toString方法。

public class Student {

    private String name;
    private Integer age;

    public static int compareByAge(Student s1, Student s2) {
        return s1.age.compareTo(s2.age);
    }

    // 省略屬性get/set方法

    public void whoIam() {
        System.out.println("I am " + this.toString());
    }
    
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public static void main(String[] args) {

    List<Student> list = new ArrayList<>();
    list.forEach(Student::whoIam);
}

3.4 構造方法引用

注意,引用的構造方法的參數列表要和函數式接口中抽象方法的參數列表保持一致。

public static void main(String[] args) {
    Supplier<Student> studentSupplier1 = () -> new Student();
    Student student1 = studentSupplier1.get();
    
    // 構造方法引用
    Supplier<Student> studentSupplier2 = Student::new;
    Student student2 = studentSupplier2.get();
}

引用數組和引用構造器很像,格式為類型[]::new,等價於lambda 表達式 x -> new int[x]。其中類型可以為基本類型也可以是類。

public static void main(String[] args) {
    
    Function<Integer, Student[]> studentFunction = Student[]::new;
    Student[] students = studentFunction.apply(10);
}

四 總結

方法引用就是通過方法的名字來指向一個方法。它可以使語言的構造更緊湊簡潔,減少冗餘代碼。方法引用的操作符是雙冒號 ::

雖然方法引用能帶來一些好處,不過也要注意場景的使用,沒必要刻意去使用方法引用。因為有時Lambda表達式可能比方法引用更讓人理解閱讀,也方便必要時修改代碼。

歡迎關注微信公眾號:「Java之言」技術文章持續更新,請持續關注……

  • 第一時間學習最新技術文章
  • 領取最新技術學習資料視頻
  • 最新互聯網資訊和面試經驗