lambda表達式基本原理

一般來說,任何lambda表達式都可以看做聲明在函數式接口中的單個抽象方法的實現

1. lambda的使用需要 “函數式接口” 的支持

函數式接口:接口中只有一個抽象方法的接口,稱爲函數式接口,

函數式接口(Functional Interface):

就是一個有且僅有一個抽象方法,但是可以有多個非抽象方法的接口。

函數式接口可以被隱式轉換爲lambda表達式。

函數式接口可以現有的函數友好地支持 lambda。

 

2. JAVA 常用函數式接口方法 java.util.function 包下

 

 

3. 接口通常用@FunctionalInterface標識(非必須)

 

可以通過 Lambda 表達式來創建該接口的對象 (若 Lambda表達式拋出一個受檢異常,那麼該異常需要在目標接口的抽象方法上進行聲明)

可以使用註解 @FunctionalInterface 修飾可以檢查是否是函數式接口,同時 javadoc 也會包含一條聲明,說明這個接口是一個函數式接口

*/

 

例:這是一個Java 提供的 函數式接口,作用是返回一個<T>對象 (供給型幾口)

package java.util.function;

 

/**

* Represents a supplier of results.

*

* <p>There is no requirement that a new or distinct result be returned each

* time the supplier is invoked.

*

* <p>This is a <a href="package-summary.html">functional interface</a>

* whose functional method is {@link #get()}.

*

* @param <T> the type of results supplied by this supplier

*

* @since 1.8

*/

@FunctionalInterface

public interface Supplier<T> {

 

/**

* Gets a result.

*

* @return a result

*/

T get();

}

調用如下

Supplier supplier=()->"返回一個object對象";

Object o = supplier.get();

System.out.println(o); //控制檯輸出 "返回一個object對象"

 

 

4. 自定義函數式接口

首先創建一個接口,如下

package simpleLambda;

 

/**

* 自定義函數式接口

* @param: <T> the type of results supplied by this supplier

* @author PangHao

* @date 2018/8/29 18:38

*/

@FunctionalInterface

public interface TestInterface<T> {

 

/**

* 自定義函數式接口的功能方法,返回和入參類型相同的一個值

* @param: <T> the type of results supplied by this supplier

* @return: <T> he type of results supplied by this supplier

* @auther: PangHao

* @date: 2018-08-29 18:39

*/

T testFunction(T t);

然後去聲明這個接口,並調用他的功能性方法

TestInterface testInterface =(x)-> "Hellow "+x;

//控制檯輸出 Hellow PangHao

System.out.println(testInterface.testFunction("PangHao"));

注:由於入參和返回值類型相同,所以不需要規定參數類型,Java可以自行進行推導

 

5.將Lambda 作爲方法參數傳遞

依舊是先創建個函數式接口

package simpleLambda;

 

/**

* @author P H 

* @date 2018/8/24 18:17

*/

@FunctionalInterface

public interface SimpleFilter <Integer>{

 

/**

* 功能性方法,傳入一個Integer 返回一個 boolean

* @param t 一個Integer 值

* @return: boolean

* @auther: P H 

* @date: 2018/8/24 18:19

*/

boolean judge(Integer t);

}

創建一個能夠接受Lambda的方法

//實際對list進行過濾的方法,可以傳遞不同的判定規則

//判斷該list 中滿足判定規則元素的個數

public static int filters(List<Integer> list, SimpleFilter<Integer> fil) {

//SimpleFilter 是自定義的一個函數式接口

int count = 0;

for (int no : list) {

//這裏去調用SimpleFilter 的功能性方法並將值傳入

if (fil.judge(no)) {

count++;

}

}

 

//也可以用stream 來簡化這種操作(推薦)

long count1 = list.stream().filter((s) -> s > 5).count();

System.out.println("count1="+count1);

//輸出true

System.out.println(count1==count);

return count;

}

調用這個方法,並將lambda表達式作爲參數傳入

//以Lambda表達式作爲參數傳遞,簡化遍歷過濾

List<Integer> list = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11).collect(Collectors.toList());

 

//使用雙冒號可以直接在控制檯循環輸出每個元素

list.stream().filter(str->str>5).forEach(System.out::println);

 

//用Stream同樣可以實現,而且更簡單

long count = list.stream().filter(str -> str > 5).count();

System.out.println("The Stream Count Is "+count);

 

//將Lambda 表達式作爲方法參數傳到方法中(上圖的方法)

//傳入一個Integer值 x,如果值 x>5 返回true,否則返回false

int filters = filters(list, x -> x > 5 ? true : false);

 

// 輸出 “The Count Is 6”

System.out.println("The Function Count Is "+filters);

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章