Java8 常用函式式介面

2021-08-15 04:54:57 字數 2725 閱讀 7755

本文主要參考: 在此感謝

接收乙個引數t,沒有返回值

原始碼:

@functionalinte***ce

public

inte***ce consumer;

}}

示例

//講述乙個學生在學習過程中,需要買學習用品。買什麼東西是具體的實現,我們在呼叫study()方法的時候再指定。

@test

public

void

testconsumer()

public

void

study(double money, consumercon)

傳入引數t做一定的操作,返回r型的資料

原始碼:

@functionalinte***ce

public

inte***ce

function

default

function

andthen

(function<? super r, ? extends v> after)

static

function

identity

() }

示例:

//這裡的studentupdate()接受乙個student引數,做一定的處理之後,返回乙個string型別的值。

//例如更改乙個學生的名字,列印更改以後的名字。當然最好可以返回學生物件,這樣就可以更改年齡、得分之類的資訊。

/** * 對學生改名字,將aim改為anny

*/@test

public

void

testfunction());

system.out.println(newname);

}/**

* *@param 學生物件

*@param 要做什麼處理,在呼叫該方法的時候指定

*@return

*/public string studentupdate(student s, functionfun)

不接受引數,返回特定型別引數t

原始碼:

@functionalinte***ce

public

inte***ce supplier

示例

//模擬老師隨機點名,這次需要三位同學打掃衛生,使用supplier來生成對應的學生資訊。

public

static liststudents = arrays.aslist(

new student(1, "aim", 12, 78),

new student(2, "bob", 15, 45),

new student(3, "cicy", 13, 89),

new student(4, "deny", 11, 91),

new student(5, "emiy", 14, 32)

);@test

public

void

testsupplier()

/***

*@param num 需要多少學生資訊

*@param sup 怎麼去篩選需要的學生資訊

*@return list 學生集合

*/public listgetstulist(int num, suppliersup)

return list;

}

輸入乙個引數,返回boolean型別

原始碼:

@functionalinte***ce

public inte***ce predicate

default predicatenegate()

default predicateor(predicate<? super t> other)

static predicateisequal(object targetref)

}

示例

/**

* 把學生成績大於60的學生資訊列印輸出

*/@test

public

void

testpredicate()

/***

*@param 學生集合list

*@param 需要做何種判斷

*@return 符合條件的學生集合list

*/public listfilterstudent(listlist, predicatepre)

}return l;

}

介面

引數型別

返回型別

用途bifunction< t, u, r >

t, u

r對型別t,u引數操作,返回r型別的結果

unaryoperator< t >tt

對型別為t的物件進行一元運算,返回t型別的結果

binaryoperator< t>

t, t

t對型別為t的物件進行二元運算,返回t型別的結果

biconsumer< t, u>

t, u

void

對型別為t,u引數應用操作

tointfunction< t>

tint

計算int值的計算

intfunction< r>

intr

引數為int型別的函式

java 8 函式式介面

functionalinte ce public inte ce personsearch functionalinte ce public inte ce personsearch1 functionalinte ce public inte ce personsearch2 方法的預設實現 介面...

Java8常用的函式式介面

1 predicate 斷言型介面 傳入的字串是否以 sql 結尾 predicateisendwithsql s s.endswith sql 傳入的字串非 sql 結尾 predicatenotendwithsql isendwithsql.negate boolean test isendwi...

Java8 中的常用函式式介面

函式式介面 predicate t 函式描述符 t boolean 原始型別特化 intpredicate,longpredicate,doublepredicate 判斷是否是正數 intpredicate predicate i i 0 true predicate.test 5 函式式介面 c...