国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - java8學(xué)習(xí)教程之lambda表達(dá)式的使用方法

java8學(xué)習(xí)教程之lambda表達(dá)式的使用方法

2021-01-05 10:51tantanit Java教程

Java8最值得學(xué)習(xí)的特性就是Lambda表達(dá)式,下面這篇文章主要給大家介紹了關(guān)于java8學(xué)習(xí)教程之lambda表達(dá)式使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著

前言

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

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

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

定義 TantanitReader:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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();
 }
}

定義判斷的接口:

?
1
2
3
public interface Predicate<T> {
 boolean test(T t);
}

定義選擇函數(shù):

?
1
2
3
4
5
6
7
8
9
10
11
12
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;
 }
 
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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","張三","學(xué)生"));
 source.add(new TantanitReader(18,"rose","李四","學(xué)生"));
 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é)果如下:

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

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

?
1
tantanitReader.getAge()>=18

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

但是在 Java8 之前,由于沒(méi)有 lambda 表達(dá)式,只能忍受這種冗余。如何用 lambda 表達(dá)式來(lái)簡(jiǎn)化代碼呢?

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

?
1
2
3
public interface Predicate<T> {
 boolean test(T t);
}

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

?
1
2
3
4
@FunctionalInterface
public interface PredicateFunction<T> {
 boolean test(T t);
}

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

?
1
2
3
4
5
6
7
8
9
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;
}

再修改測(cè)試的例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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","張三","學(xué)生"));
 source.add(new TantanitReader(18,"rose","李四","學(xué)生"));
 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());
 }
 }
 
}

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

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

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

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

?
1
2
3
4
List<TantanitReader> audultReaders
=tantanitReaderSelectSerive.select(
 source,(TantanitReader tantanitReader) -> tantanitReader.getAge() >= 18
);

但是這個(gè)例子,實(shí)際上會(huì)報(bào)編譯錯(cuò)誤,說(shuō) TantanitReader 和 tantanitReaderSelectSerive 的 select 方法的定義不匹配,因?yàn)?select 方法使用的是泛型。java8 的文檔確實(shí)是規(guī)定了在使用泛型的情況下,不能直接將 lambda 表達(dá)式作為參數(shù),這個(gè)挺無(wú)語(yǔ)的。如果不使用泛型的,沒(méi)有這個(gè)問(wèn)題。

小結(jié)

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

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

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

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

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。

原文鏈接:http://tantanit.com/java-how-to-use-lambda/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品免费视频一区 | 91精品视频导航 | 日韩欧美国产一区二区三区 | 亚洲国产aⅴ成人精品无吗 成人午夜视频在线观看 | 亚洲福利在线观看 | 国产亚洲视频在线 | 亚洲国产精品久久久 | 瑟瑟视频在线观看 | 久久精品视频免费观看 | 日日操操| 精品一区二区三区在线观看 | 九九天堂 | 久久99精品国产99久久6尤 | 天天拍天天草 | 国产欧美精品一区二区色综合 | 亚洲精品成人天堂一二三 | 国产韩国精品一区二区三区 | 97精品一区二区三区 | 美女视频一区二区三区 | 性高湖久久久久久久久aaaaa | 自拍第1页 | 黄色w网站 | 日本久久精品 | 久久久女女女女999久久 | 国产欧美视频一区二区 | 久久久久国产精品免费免费搜索 | 九色porny国模私拍av | 欧美精品1区2区3区 国产中文视频 | 亚洲精品成人 | 日韩欧美视频免费在线观看 | 国产精品对白一区二区三区 | 欧美一级看片a免费观看 | 国产日韩欧美一区二区 | 亚洲国产一二区 | 在线日韩成人 | 一区二区在线视频 | 免费视频一区二区 | 亚洲一区二区免费看 | 日韩精品一区二区在线观看 | 九九热免费观看 | 欧美在线国产 |