java8-lambda-函數(shù)式接口及四大類型函數(shù)接口

函數(shù)式接口@FunctionalInterface


作用:

該接口標(biāo)記了引用的接口是一個(gè)
函數(shù)式接口(SAM:Single Abstract Interface)

即接口內(nèi)只能有一個(gè)抽象方法、可以有一個(gè)默認(rèn)方法、多個(gè)靜態(tài)方法

方法可以使用lambda表達(dá)式來傳入方法進(jìn)行調(diào)用。

示例:
@FunctionalInterface
public interface TestFunctionalInterface {


    int accept(int t);

    default int defaultAccept(int t) {
        return t + 1;
    }

    static void staticMethod1() {
        System.out.println("test============");
    }

    static void staticMethod2() {
        System.out.println("test1===========");
    }
}
class Test {
    public static void main(String[] args) {
        int result = getResult(10, a -> a + 2);
        System.out.println(result);
        result = getDefaultResult(10, a -> a);
        System.out.println(result);
        TestFunctionalInterface.test();
    }


    private static int getResult(int i, TestFunctionalInterface functionalInterface) {
        return functionalInterface.accept(i);
    }

    private static int getDefaultResult(int i, TestFunctionalInterface functionalInterface) {
        return functionalInterface.defaultAccept(i);
    }
}
總結(jié):

@FunctionalInterface接口充當(dāng)標(biāo)記作用,顯式的聲明了一個(gè)接口只允許一個(gè)抽象方法

Consumer接口

(java.util.function.Consumer)

定義了一個(gè)accept方法,1個(gè)入?yún)o出參。

引申的Consumer接口也是沒有出參的。

接口 入?yún)㈩愋?/th>
Consumer<T> T
IntConsumer int
LongConsumer long
DoubleConsumer double
ObjDoubleConsumer<T> T,double
ObjIntConsumer<T> T,int
ObjLongConsumer<T> T,long
BiConsumer<T,U> T,U
Consumer的使用

Consumer意味著消費(fèi),java這里定義的方法是沒有返回值的,只有一個(gè)入?yún)ⅰ?br> 所以在實(shí)際使用的過程中,consumer接口可以用來提交一個(gè)無需結(jié)果的一個(gè)方法。

  1. 首先定義一個(gè)方法,無返回值,一個(gè)對(duì)象參數(shù),一個(gè)Consumer接口參數(shù)。
    private static void testConsumer(int a , Consumer<Integer> action){
        action.accept(a);
    }
  1. 在main方法里調(diào)用這個(gè)方法,傳一個(gè)int參數(shù),即lambda表達(dá)式作為入?yún)?。
    public static void main(String[] args) {
        testConsumer(30 , p->{
            System.out.println(p);
        });
    }

其中 p->{},這一塊代碼為Consumer接口的里accept方法的具體實(shí)現(xiàn),這里將方法作為參數(shù)傳入到
testConsumer(int a , Consumer<Integer> action)方法內(nèi)。
當(dāng)然,傳入的方法只是做了一個(gè)簡(jiǎn)單的打印。

Function接口

(java.util.function.Function)

定義了一個(gè)apply方法,1個(gè)入?yún)?個(gè)出參。

引申的Function接口也是有出參的。

接口 入?yún)㈩愋?/th> 出參類型
Function<T,R> T R
DoubleFunction<R> double R
IntFunction<R> int R
LongFunction<R> long R
ToDoubleFunction<T> T double
ToIntFunction<T> T int
ToLongFunction<T> T long
DoubleToIntFunction double int
DoubleToLongFunction double long
IntToDoubleFunction int double
IntToLongFunction int long
LongToDoubleFunction long double
LongToIntFunction long int
BiFunction<T,U,R> T,U R
ToDoubleBiFunction<T,U> T,U double
ToIntBiFunction<T,U> T,U int
ToLongBiFunction<T,U> T,U long

Predicate接口

(java.util.function.Predicate)

定義了一個(gè)test方法,1個(gè)入?yún)?一個(gè)boolean出參

引申的Predicate接口也是boolean出參

接口 入?yún)㈩愋?/th>
Predicate<T> T
DoublePredicate double
IntPredicate int
LongPredicate long
BiPredicate<T,U> T,U

Supplier接口

(java.util.function.Supplier)

定義了一個(gè)get方法,無入?yún)?個(gè)出參。

引申的Supplier接口也是有出參的。

接口 出參類型
Supplier<T> T
BooleanSupplier boolean
DoubleSupplier double
IntSupplier int
LongSupplier long
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容