Java8新特性之方法引用的實踐指南
一 前言
日常開發(fā)中,經(jīng)常使用到Lambda表達式,例如:
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 5, 10, 4, 2);
// 打印列表中的每一個數(shù)字
list.forEach((x) -> System.out.println(x));
}
其中(x) -> System.out.println(x)就是使用的Lambda表達式。Lambda表達式可以分為三部分:
- 左括號:Lambda的形參列表,對應(yīng)接口的抽象方法的形參列表。
- 箭頭:Lambda的操作符,可以理解為參數(shù)列表和Lambda體的分隔符。
- Lambda體:即對應(yīng)接口中的抽象方法的實現(xiàn)方法體。
你是否發(fā)現(xiàn),上述例子的Lambda表達式的Lambda體僅僅調(diào)用一個已存在的方法,而不做任何其它事。對于這種情況,通過一個方法名字來引用這個已存在的方法會更加清晰。所以,方法引用應(yīng)運而生,方法引用是一個更加緊湊,易讀的Lambda表達式,它是Lambda表達式的另外一種表現(xiàn)形式,方法引用的操作符是雙冒號 :: 。
使用了方法引用,上述例子編寫如下,變得更加緊湊,易讀了。
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 5, 10, 4, 2);
// 打印列表中的每一個數(shù)字
list.forEach(System.out::println);
}
二 方法引用
方法引用就是通過方法的名字來指向一個方法。它可以使語言的構(gòu)造更緊湊簡潔,減少冗余代碼。方法引用的操作符是雙冒號 :: 。方法引用有如下幾種分類:
| 類型 | 語法 | Lambda表達式 |
|---|---|---|
| 靜態(tài)方法引用 | 類名::靜態(tài)方法名 | (args) -> 類名.靜態(tài)方法名(args) |
| 實例方法引用 | 實例::實例方法名 | (args) -> 實例.實例方法名(args) |
| 對象方法引用 | 類名::對象方法名 | (inst,args) -> 類名.對象方法名(args) |
| 構(gòu)建方法引用 | 類名::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 靜態(tài)方法引用
現(xiàn)假設(shè)有50個學生,存放在一個list列表中,現(xiàn)需要對年齡進行從小到大排序。我們一般會寫一個比較器進行排序,如下:
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());
}
}
}
我們發(fā)現(xiàn),List的sort方法接受的參數(shù)Comparator是一個函數(shù)式接口,則可以用Lambda表達式改為如下形式:
list.sort((s1, s2) -> s1.getAge().compareTo(s2.getAge()));
我們又發(fā)現(xiàn),Student類有個靜態(tài)方法compareByAge,其功能和上述Lambda表達式一樣,所以我們可以將以上Lambda表達式改為如下形式:
list.sort((s1, s2) -> Student.compareByAge(s1, s2));
可以看出,最終的Lambda表達式是調(diào)用Student類的一個方法,所以,根據(jù)靜態(tài)方法引用規(guī)則,可改為如下形式:
list.sort(Student::compareByAge);
3.2 實例方法引用
即引用已經(jīng)存在的實例的方法。靜態(tài)方法引用類無需實例化,直接用類名來調(diào)用,而實例方法引用是要先實例化對象。
如果將Student類的靜態(tài)方法compareByAge改為非靜態(tài)方法,即:
public int compareByAge(Student s1, Student s2) {
return s1.age.compareTo(s2.age);
}
則可通過如下方式對學生數(shù)組進行排序:
list.sort(new Student()::compareByAge);
3.3 對象方法引用
如果Lambda表達式的參數(shù)列表中,第一個參數(shù)是實例方法的調(diào)用者對象,第二個參數(shù)是實例方法的參數(shù)時,可使用對象方法引用。例如,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 構(gòu)造方法引用
注意,引用的構(gòu)造方法的參數(shù)列表要和函數(shù)式接口中抽象方法的參數(shù)列表保持一致。
public static void main(String[] args) {
Supplier<Student> studentSupplier1 = () -> new Student();
Student student1 = studentSupplier1.get();
// 構(gòu)造方法引用
Supplier<Student> studentSupplier2 = Student::new;
Student student2 = studentSupplier2.get();
}
引用數(shù)組和引用構(gòu)造器很像,格式為類型[]::new,等價于lambda 表達式 x -> new int[x]。其中類型可以為基本類型也可以是類。
public static void main(String[] args) {
Function<Integer, Student[]> studentFunction = Student[]::new;
Student[] students = studentFunction.apply(10);
}
四 總結(jié)
方法引用就是通過方法的名字來指向一個方法。它可以使語言的構(gòu)造更緊湊簡潔,減少冗余代碼。方法引用的操作符是雙冒號 ::
雖然方法引用能帶來一些好處,不過也要注意場景的使用,沒必要刻意去使用方法引用。因為有時Lambda表達式可能比方法引用更讓人理解閱讀,也方便必要時修改代碼。
到此這篇關(guān)于Java8新特性之方法引用的文章就介紹到這了,更多相關(guān)Java8新特性方法引用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 日期與時間API相關(guān)用法總結(jié)
這篇文章主要介紹了Java 日期與時間API相關(guān)用法總結(jié),幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2021-02-02
關(guān)于Java Guava ImmutableMap不可變集合源碼分析
這篇文章主要介紹Java Guava不可變集合ImmutableMap的源碼分析的相關(guān)資料,需要的朋友可以參考下面具體的文章內(nèi)容2021-09-09
Spring中的ThreadPoolTaskExecutor線程池使用詳解
這篇文章主要介紹了Spring中的ThreadPoolTaskExecutor線程池使用詳解,ThreadPoolTaskExecutor 是 Spring框架提供的一個線程池實現(xiàn),用于管理和執(zhí)行多線程任務(wù),它是TaskExecutor接口的實現(xiàn),提供了在 Spring 應(yīng)用程序中創(chuàng)建和配置線程池的便捷方式,需要的朋友可以參考下2024-01-01
詳解Spring Cloud 跨服務(wù)數(shù)據(jù)聚合框架
這篇文章主要介紹了詳解Spring Cloud 跨服務(wù)數(shù)據(jù)聚合框架,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
Spring處理@Async導致的循環(huán)依賴失敗問題的方案詳解
這篇文章主要為大家詳細介紹了SpringBoot中的@Async導致循環(huán)依賴失敗的原因及其解決方案,文中的示例代碼講解詳細,感興趣的可以學習一下2022-07-07

