Java8中一個(gè)極其強(qiáng)悍的新接口,炸裂!很多人沒用過(非常實(shí)用)
點(diǎn)擊關(guān)注下方公眾號(hào),架構(gòu)師全套資料 都在這里
今天給大家推薦一個(gè)牛逼的接口Function,徹底消滅代碼中過多的if...else...,讓你的代碼更加美觀,現(xiàn)在一起來學(xué)習(xí)下吧。
if...else...進(jìn)行判斷拋出異常、分支處理等操作。這些if...else...充斥在代碼中嚴(yán)重影響了代碼代碼的美觀,這時(shí)我們可以利用Java 8的Function接口來消滅if...else...。if?(...){
????throw?new?RuntimeException("出現(xiàn)異常了");
}?
if?(...){
????doSomething();
}?else?{
????doOther();
}
Function 函數(shù)式接口
@FunctionalInterface標(biāo)識(shí),并且只包含一個(gè)抽象方法的接口是函數(shù)式接口。函數(shù)式接口主要分為Supplier供給型函數(shù)、Consumer消費(fèi)型函數(shù)、Runnable無參無返回型函數(shù)和Function有參有返回型函數(shù)。Function可以看作轉(zhuǎn)換型函數(shù)
Supplier供給型函數(shù)
Supplier的表現(xiàn)形式為不接受參數(shù)、只返回?cái)?shù)據(jù)

Consumer消費(fèi)型函數(shù)

Runnable無參無返回型函數(shù)
Runnable的表現(xiàn)形式為即沒有參數(shù)也沒有返回值

Function函數(shù)的表現(xiàn)形式為接收一個(gè)參數(shù),并返回一個(gè)值。Supplier、Consumer和Runnable可以看作Function的一種特殊表現(xiàn)形式

使用小技巧:處理拋出異常的if
1.定義函數(shù)
定義一個(gè)拋出異常的形式的函數(shù)式接口, 這個(gè)接口只有參數(shù)沒有返回值是個(gè)消費(fèi)型接口
/**
?*?拋異常接口
?**/
@FunctionalInterface
public?interface?ThrowExceptionFunction?{
????/**
?????*?拋出異常信息
?????*
?????*?@param?message?異常信息
?????*?@return?void
?????**/
????void?throwMessage(String?message);
}
ThrowExceptionFunction。ThrowExceptionFunction的接口實(shí)現(xiàn)邏輯為當(dāng)參數(shù)b為true時(shí)拋出異常/**
?*??如果參數(shù)為true拋出異常
?*?
?*?@param?b?
?*?@return?com.example.demo.func.ThrowExceptionFunction
?**/
public?static?ThrowExceptionFunction?isTure(boolean?b){
????return?(errorMessage)?->?{
????????if?(b){
????????????throw?new?RuntimeException(errorMessage);
????????}
????};
}
throwMessage方法傳入異常信息。當(dāng)出入的參數(shù)為false時(shí)正常執(zhí)行
當(dāng)出入的參數(shù)為true時(shí)拋出異常

處理if分支操作
1.定義函數(shù)式接口
BranchHandle的函數(shù)式接口,接口的參數(shù)為兩個(gè)Runnable接口。這兩個(gè)兩個(gè)Runnable接口分別代表了為true或false時(shí)要進(jìn)行的操作/**
?*?分支處理接口
?**/
@FunctionalInterface
public?interface?BranchHandle?{
????/**
?????*?分支操作
?????*
?????*?@param?trueHandle?為true時(shí)要進(jìn)行的操作
?????*?@param?falseHandle?為false時(shí)要進(jìn)行的操作
?????*?@return?void
?????**/
????void?trueOrFalseHandle(Runnable?trueHandle,?Runnable?falseHandle);
}
2.編寫判斷方法
BranchHandle。/**
?*?參數(shù)為true或false時(shí),分別進(jìn)行不同的操作?
?*?
?*?@param?b?
?*?@return?com.example.demo.func.BranchHandle?????
?**/
public?static?BranchHandle?isTureOrFalse(boolean?b){
????
????return?(trueHandle,?falseHandle)?->?{
????????if?(b){
????????????trueHandle.run();
????????}?else?{
????????????falseHandle.run();
????????}
????};
}
3.使用方式
參數(shù)為true時(shí),執(zhí)行trueHandle

參數(shù)為false時(shí),執(zhí)行falseHandle

如果存在值執(zhí)行消費(fèi)操作,否則執(zhí)行基于空的操作
1.定義函數(shù)
PresentOrElseHandler的函數(shù)式接口,接口的參數(shù)一個(gè)為Consumer接口。一個(gè)為Runnable,分別代表值不為空時(shí)執(zhí)行消費(fèi)操作和值為空時(shí)執(zhí)行的其他操作。另外,搜索公眾號(hào)互聯(lián)網(wǎng)架構(gòu)師后臺(tái)回復(fù)“面試”,獲取一份驚喜禮包。/**
?*?空值與非空值分支處理
?*/
public?interface?PresentOrElseHandler<T?extends?Object>?{
????/**
?????*?值不為空時(shí)執(zhí)行消費(fèi)操作
?????*?值為空時(shí)執(zhí)行其他的操作
?????*?
?????*?@param?action?值不為空時(shí),執(zhí)行的消費(fèi)操作
?????*?@param?emptyAction?值為空時(shí),執(zhí)行的操作
?????*?@return?void????
?????**/
???void?presentOrElseHandle(Consumer?super?T>?action,?Runnable?emptyAction);
???
}
2.編寫判斷方法
isBlankOrNoBlank的方法,方法的返回值為剛才定義的函數(shù)式接口-PresentOrElseHandler。/**
?*?參數(shù)為true或false時(shí),分別進(jìn)行不同的操作
?*
?*?@param?b
?*?@return?com.example.demo.func.BranchHandle
?**/
public?static?PresentOrElseHandler>?isBlankOrNoBlank(String?str){
????return?(consumer,?runnable)?->?{
????????if?(str?==?null?||?str.length()?==?0){
????????????runnable.run();
????????}?else?{
????????????consumer.accept(str);
????????}
????};
}
3.使用方式
presentOrElseHandle方法傳入一個(gè)Consumer和Runnable
參數(shù)不為空時(shí)

來源:juejin.cn/post/7011435192803917831
