欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java8學習教程之lambda表達式的使用方法

 更新時間:2017年09月18日 10:55:46   投稿:daisy  
Java8最值得學習的特性就是Lambda表達式,下面這篇文章主要給大家介紹了關(guān)于java8學習教程之lambda表達式使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。

前言

我們在 上一篇文章 中介紹了 lambda 表達式的語法,引入了 lambda 表達式的使用場景,以及使用 lambda 表達式的好處。我們將在這篇文章中,已實例講解如何定義和使用 lambda 表達式,以及與其它語言相比,lambda 表達式在 Java 中的特殊規(guī)范。

使用匿名內(nèi)部類的例子

首先明確一點,在 Java8 出現(xiàn)之前,lambda 表達式能夠做到的,使用內(nèi)部類也能做到,lambda 表達式只是簡化了編程。
下面的例子是從列表中根據(jù)條件挑選出讀者。

定義 TantanitReader:

public class TantanitReader {
 private int age;
 private String loginName;
 private String realName;
 private String career;

 public TantanitReader() {
 }

 public TantanitReader(int age, String loginName, String realName, String career) {
 this.age = age;
 this.loginName = loginName;
 this.realName = realName;
 this.career = career;
 }

 public int getAge() {
 return age;
 }

 public void setAge(int age) {
 this.age = age;
 }

 public String getLoginName() {
 return loginName;
 }

 public void setLoginName(String loginName) {
 this.loginName = loginName;
 }

 public String getRealName() {
 return realName;
 }

 public void setRealName(String realName) {
 this.realName = realName;
 }

 public String getCareer() {
 return career;
 }

 public void setCareer(String career) {
 this.career = career;
 }

 @Override
 public String toString() {
 return "age:"+this.getAge()+",loginName:"+this.loginName
 +",realName:"+this.getRealName()+",career:"+this.getCareer();
 }
}

定義判斷的接口:

public interface Predicate<T> {
 boolean test(T t);
}

定義選擇函數(shù):

public class SelectService<T> {
 public List<T> select(Collection<T> source, Predicate<T> predicate){
 List result = new LinkedList();
 for(T element:source){
  if (predicate.test(element)) {
  result.add(element);
  }
 }
 return result;
 }

}

編寫測試用的例子,分別選擇成年讀者和十多歲(包括 10 歲)的讀者:

public class TantanitReaderPredicateTest {


 public static void main(String[] args) {
 SelectService tantanitReaderSelectSerive
 =new SelectService<TantanitReader>();
 List<TantanitReader> source = new LinkedList<>();
 source.add(new TantanitReader(10,"jack","張三","學生"));
 source.add(new TantanitReader(18,"rose","李四","學生"));
 source.add(new TantanitReader(19,"mike","王五","程序員"));
 source.add(new TantanitReader(20,"jack","趙六","作家"));

 List<TantanitReader> audultReaders
 =tantanitReaderSelectSerive.select(source, new Predicate() {
  @Override
  public boolean test(Object o) {
  TantanitReader tantanitReader=(TantanitReader)o;
  return tantanitReader.getAge()>=18;
  }
 });
 System.out.println("tantanit.com 成年讀者名單如下:");
 printTantanitReaders(audultReaders);

 System.out.println("tantanit.com 十多歲(包含 10 歲)成員如下:");
 List<TantanitReader> teenReaders
 =tantanitReaderSelectSerive.select(source, new Predicate() {
  @Override
  public boolean test(Object o) {
  TantanitReader tantanitReader=(TantanitReader)o;
  return tantanitReader.getAge()>=10 && tantanitReader.getAge()<=19;
  }
 });
 printTantanitReaders(teenReaders);
 }


 public static void printTantanitReaders(List<TantanitReader> tantanitReaders) {
 for (TantanitReader tantanitReader : tantanitReaders) {
  System.out.println(tantanitReader.toString());
 }
 }


}

執(zhí)行后,打印結(jié)果如下:

tantanit.com 成員讀者名單如下:
age:18,loginName:rose,realName: 李四,career: 學生 
age:19,loginName:mike,realName: 王五,career: 程序員
age:20,loginName:jack,realName: 趙六,career: 作家
tantanit.com 十多歲(包含10 歲)成員如下:
age:10,loginName:jack,realName: 張三,career: 學生
age:18,loginName:rose,realName: 李四,career: 學生
age:19,loginName:mike,realName: 王五,career: 程序員

可以看到,兩次選擇讀者,都需要 new Predicate(),并且重寫(Override)test 方法,而真正的差異其實只在于判斷語句:

tantanitReader.getAge()>=18

tantanitReader.getAge()>=10 && tantanitReader.getAge()<=19

但是在 Java8 之前,由于沒有 lambda 表達式,只能忍受這種冗余。如何用 lambda 表達式來簡化代碼呢?

為了照顧 Java 開發(fā)人員既有的編程習慣,與其它語言不同,Java8 在設(shè)計 lambda 表達式的使用機制時,規(guī)定仍然需要使用接口,并且要求所使用的接口必須是函數(shù)式接口,在這個例子中,我們?nèi)匀豢梢允褂茫?/p>

public interface Predicate<T> {
 boolean test(T t);
}

因為這個接口只有一個抽象方法(java8 引入了 default 方法,default 方法有具體實現(xiàn),不算抽象方法),所以它是函數(shù)式接口(functional interface)。函數(shù)式接口可以加上 @FunctionalInterface 聲明,也可以不加。但是加上之后,編譯器在編譯階段就會檢查這個接口是否符合函數(shù)式接口的定義,所以這里我們定義一個新的接口,并且加上 @FunctionalInterface 聲明:

@FunctionalInterface
public interface PredicateFunction<T> {
 boolean test(T t);
}

并且給 SelectService 添加一個以 PredicateFunction 為參數(shù)的方法:

public List<T> select(Collection<T> source, PredicateFunction<T> predicate){
 List result = new LinkedList();
 for(T element:source){
 if (predicate.test(element)) {
  result.add(element);
 }
 }
 return result;
}

再修改測試的例子:

public class TantanitReaderPredicateFunctionTest {

 public static void main(String[] args) {
 SelectService tantanitReaderSelectSerive
 =new SelectService<TantanitReader>();
 List<TantanitReader> source = new LinkedList<>();
 source.add(new TantanitReader(10,"jack","張三","學生"));
 source.add(new TantanitReader(18,"rose","李四","學生"));
 source.add(new TantanitReader(19,"mike","王五","程序員"));
 source.add(new TantanitReader(20,"jack","趙六","作家"));

 PredicateFunction<TantanitReader> predicateFunction
  = (TantanitReader tantanitReader) -> tantanitReader.getAge() >= 18;
 List<TantanitReader> audultReaders
 =tantanitReaderSelectSerive.select(source,predicateFunction);

 System.out.println("tantanit.com 成員讀者名單如下:");
 printTantanitReaders(audultReaders);

 System.out.println("tantanit.com 十多歲(包含 10 歲)成員如下:");
 PredicateFunction<TantanitReader> predicateFunction2
 = (TantanitReader tantanitReader)
 -> tantanitReader.getAge()>=10 && tantanitReader.getAge()<=19;
 List<TantanitReader> teenReaders
 =tantanitReaderSelectSerive.select(source,predicateFunction2);
 printTantanitReaders(teenReaders);
 }


 public static void printTantanitReaders(List<TantanitReader> tantanitReaders) {
 for (TantanitReader tantanitReader : tantanitReaders) {
  System.out.println(tantanitReader.toString());
 }
 }

}

下面我們分析一下這段代碼是如何生效的:

PredicateFunction<TantanitReader> predicateFunction
 = (TantanitReader tantanitReader) -> tantanitReader.getAge() >= 18;
List<TantanitReader> audultReaders
=tantanitReaderSelectSerive.select(source,predicateFunction);

這段代碼,生成了一個 PredicateFunction 類型的實例,并且將該實例的引用作為參數(shù)傳給 tantanitReaderSelectSerive 的 select 方法,并且執(zhí)行 select 方法。select 在執(zhí)行過程中,調(diào)用 predicateFunction 的 test 方法,而 test 方法的內(nèi)容就是我們傳入的 lambda 表達式,最終按照 lambda 表達式,選擇出讀者。

再進一步,一般可以不定義 predicateFunction 這個變量,而直接將 lambda 表達式作為參數(shù)傳給 tantanitReaderSelectSerive 的 select 方法,像這樣:

List<TantanitReader> audultReaders
=tantanitReaderSelectSerive.select(
 source,(TantanitReader tantanitReader) -> tantanitReader.getAge() >= 18
);

但是這個例子,實際上會報編譯錯誤,說 TantanitReader 和 tantanitReaderSelectSerive 的 select 方法的定義不匹配,因為 select 方法使用的是泛型。java8 的文檔確實是規(guī)定了在使用泛型的情況下,不能直接將 lambda 表達式作為參數(shù),這個挺無語的。如果不使用泛型的,沒有這個問題。

小結(jié)

下面總結(jié)一下如何使用 lambda 表達式

  • 首先,定義一個函數(shù)式接口(functional interface),并且在接口中定義需要使用的抽象方法。
  • 編寫業(yè)務(wù)方法,并且以該函數(shù)式接口作為參數(shù),并且調(diào)用該接口定義的方法,完成業(yè)務(wù)邏輯。
  • 調(diào)用業(yè)務(wù)方法,并且將 lambda 表達式作為參數(shù)傳入。

如果使用了泛型,最后一步改為先定義一個函數(shù)式接口的實例的引用,再作為參數(shù)傳給業(yè)務(wù)方法。

此外,lambda 表達式還可以繼續(xù)簡化為函數(shù)引用,將在后面的文章中講解。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • IDEA錯誤:找不到或無法加載主類的完美解決方法

    IDEA錯誤:找不到或無法加載主類的完美解決方法

    使用IDEA開始就一直在搭建java環(huán)境,許久沒有使用過java,剛開始有些生疏,先建了一個最簡單的類可是運行的時候出現(xiàn)錯誤:找不到或無法加載主類,下面這篇文章主要給大家介紹了關(guān)于IDEA錯誤:找不到或無法加載主類的完美解決方法,需要的朋友可以參考下
    2022-07-07
  • Maven引入與打包指定目錄下的第三方sdk的幾種方法

    Maven引入與打包指定目錄下的第三方sdk的幾種方法

    本文主要介紹了Maven引入與打包指定目錄下的第三方sdk的幾種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-09-09
  • Java 正確地從類路徑中獲取資源

    Java 正確地從類路徑中獲取資源

    Java 有能力從類路徑中查找獲取資源,可將資源放在 CLASSPATH 里,也可打包到 Jar 中。本文將具體講述獲取資源的步驟,感興趣的朋友可以了解下
    2021-05-05
  • 詳細聊聊SpringBoot中動態(tài)切換數(shù)據(jù)源的方法

    詳細聊聊SpringBoot中動態(tài)切換數(shù)據(jù)源的方法

    在大型分布式項目中,經(jīng)常會出現(xiàn)多數(shù)據(jù)源的情況,下面這篇文章主要給大家介紹了關(guān)于SpringBoot中動態(tài)切換數(shù)據(jù)源的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2021-09-09
  • JVM 的 noverify 啟動參數(shù)問題解析

    JVM 的 noverify 啟動參數(shù)問題解析

    這篇文章主要介紹了JVM 的 noverify 啟動參數(shù)問題解析,從 JDK 13 開始及其后續(xù)版本中,不建議繼續(xù)使用?-Xverify:none?和-noverify?參數(shù),本文給大家介紹的非常詳細,需要的朋友可以參考下
    2023-05-05
  • 關(guān)于Lombok簡化編碼使用及說明

    關(guān)于Lombok簡化編碼使用及說明

    這篇文章主要介紹了關(guān)于Lombok簡化編碼使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • SpringBoot實現(xiàn)自定義注解用于文件驗證的詳細過程(大小、擴展名、MIME類型)

    SpringBoot實現(xiàn)自定義注解用于文件驗證的詳細過程(大小、擴展名、MIME類型)

    SpringBoot,Spring Cloud中經(jīng)常需要處理文件上傳的功能,為了確保上傳的文件滿足特定的要求(如擴展名、MIME類型和文件大?。?我們可以創(chuàng)建一個自定義注解來簡化驗證過程,需要的朋友可以參考下
    2024-08-08
  • Spring基礎(chǔ)篇之初識DI和AOP

    Spring基礎(chǔ)篇之初識DI和AOP

    這篇文章主要為大家詳細介紹了Spring基礎(chǔ)篇之初識DI和AOP,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 詳解Java的Spring框架中的注解的用法

    詳解Java的Spring框架中的注解的用法

    這篇文章主要介紹了Java的Spring框架中的注解的用法,包括對Java bean的定義的作用介紹,需要的朋友可以參考下
    2015-11-11
  • Spring5中SpringWebContext方法過時的解決方案

    Spring5中SpringWebContext方法過時的解決方案

    這篇文章主要介紹了Spring5中SpringWebContext方法過時的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01

最新評論