国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

java中functional interface的分類和使用詳解

瀏覽:60日期:2022-09-03 08:49:01

java 8引入了lambda表達式,lambda表達式實際上表示的就是一個匿名的function。

在java 8之前,如果需要使用到匿名function需要new一個類的實現,但是有了lambda表達式之后,一切都變的非常簡介。

我們看一個之前講線程池的時候的一個例子:

//ExecutorService using class ExecutorService executorService = Executors.newSingleThreadExecutor(); executorService.submit(new Runnable() { @Override public void run() { log.info('new runnable'); } });

executorService.submit需要接收一個Runnable類,上面的例子中我們new了一個Runnable類,并實現了它的run()方法。

上面的例子如果用lambda表達式來重寫,則如下所示:

//ExecutorService using lambda executorService.submit(()->log.info('new runnable'));

看起是不是很簡單,使用lambda表達式就可以省略匿名類的構造,并且可讀性更強。

那么是不是所有的匿名類都可以用lambda表達式來重構呢?也不是。

我們看下Runnable類有什么特點:

@FunctionalInterfacepublic interface Runnable

Runnable類上面有一個@FunctionalInterface注解。這個注解就是我們今天要講到的Functional Interface。

Functional Interface

Functional Interface是指帶有 @FunctionalInterface 注解的interface。它的特點是其中只有一個子類必須要實現的abstract方法。如果abstract方法前面帶有default關鍵字,則不做計算。

其實這個也很好理解,因為Functional Interface改寫成為lambda表達式之后,并沒有指定實現的哪個方法,如果有多個方法需要實現的話,就會有問題。

@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE)public @interface FunctionalInterface {}

Functional Interface一般都在java.util.function包中。

根據要實現的方法參數和返回值的不同,Functional Interface可以分為很多種,下面我們分別來介紹。

Function:一個參數一個返回值

Function接口定義了一個方法,接收一個參數,返回一個參數。

@FunctionalInterfacepublic interface Function<T, R> { /** * Applies this function to the given argument. * * @param t the function argument * @return the function result */ R apply(T t);

一般我們在對集合類進行處理的時候,會用到Function。

Map<String, Integer> nameMap = new HashMap<>(); Integer value = nameMap.computeIfAbsent('name', s -> s.length());

上面的例子中我們調用了map的computeIfAbsent方法,傳入一個Function。

上面的例子還可以改寫成更短的:

Integer value1 = nameMap.computeIfAbsent('name', String::length);

Function沒有指明參數和返回值的類型,如果需要傳入特定的參數,則可以使用IntFunction, LongFunction, DoubleFunction:

@FunctionalInterfacepublic interface IntFunction<R> { /** * Applies this function to the given argument. * * @param value the function argument * @return the function result */ R apply(int value);}

如果需要返回特定的參數,則可以使用ToIntFunction, ToLongFunction, ToDoubleFunction:

@FunctionalInterfacepublic interface ToDoubleFunction<T> { /** * Applies this function to the given argument. * * @param value the function argument * @return the function result */ double applyAsDouble(T value);}

如果要同時指定參數和返回值,則可以使用DoubleToIntFunction, DoubleToLongFunction, IntToDoubleFunction, IntToLongFunction, LongToIntFunction, LongToDoubleFunction:

@FunctionalInterfacepublic interface LongToIntFunction { /** * Applies this function to the given argument. * * @param value the function argument * @return the function result */ int applyAsInt(long value);}

BiFunction:接收兩個參數,一個返回值

如果需要接受兩個參數,一個返回值的話,可以使用BiFunction:BiFunction, ToDoubleBiFunction, ToIntBiFunction, ToLongBiFunction等。

@FunctionalInterfacepublic interface BiFunction<T, U, R> { /** * Applies this function to the given arguments. * * @param t the first function argument * @param u the second function argument * @return the function result */ R apply(T t, U u);

我們看一個BiFunction的例子:

//BiFunction Map<String, Integer> salaries = new HashMap<>(); salaries.put('alice', 100); salaries.put('jack', 200); salaries.put('mark', 300); salaries.replaceAll((name, oldValue) -> name.equals('alice') ? oldValue : oldValue + 200);

Supplier:無參的Function

如果什么參數都不需要,則可以使用Supplier:

@FunctionalInterfacepublic interface Supplier<T> { /** * Gets a result. * * @return a result */ T get();}

Consumer:接收一個參數,不返回值

Consumer接收一個參數,但是不返回任何值,我們看下Consumer的定義:

@FunctionalInterfacepublic interface Consumer<T> { /** * Performs this operation on the given argument. * * @param t the input argument */ void accept(T t);

看一個Consumer的具體應用:

//ConsumernameMap.forEach((name, age) -> System.out.println(name + ' is ' + age + ' years old'));

Predicate:接收一個參數,返回boolean

Predicate接收一個參數,返回boolean值:

@FunctionalInterfacepublic interface Predicate<T> { /** * Evaluates this predicate on the given argument. * * @param t the input argument * @return {@code true} if the input argument matches the predicate, * otherwise {@code false} */ boolean test(T t);

如果用在集合類的過濾上面那是極好的:

//Predicate List<String> names = Arrays.asList('A', 'B', 'C', 'D', 'E'); List<String> namesWithA = names.stream() .filter(name -> name.startsWith('A')) .collect(Collectors.toList());

Operator:接收和返回同樣的類型

Operator接收和返回同樣的類型,有很多種Operator:UnaryOperator BinaryOperator ,DoubleUnaryOperator, IntUnaryOperator, LongUnaryOperator, DoubleBinaryOperator, IntBinaryOperator, LongBinaryOperator等。

@FunctionalInterfacepublic interface IntUnaryOperator { /** * Applies this operator to the given operand. * * @param operand the operand * @return the operator result */ int applyAsInt(int operand);

我們看一個BinaryOperator的例子:

//Operator List<Integer> values = Arrays.asList(1, 2, 3, 4, 5); int sum = values.stream() .reduce(0, (i1, i2) -> i1 + i2);

Functional Interface是一個非常有用的新特性,希望大家能夠掌握。

本文的例子:https://github.com/ddean2009/learn-java-streams/tree/master/functional-interface

總結

到此這篇關于java中functional interface的分類和使用詳解的文章就介紹到這了,更多相關java中functional interface的分類和使用內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 色一伦一情一区二区三区 | 欧美大狠狠大臿蕉香蕉大视频 | 欧美69视频在线 | 97在线免费视频 | 日本韩国三级在线 | 香蕉tv亚洲专区在线观看 | 国产成人精品男人免费 | 美女张开大腿让男人桶 | 成人三级毛片 | 韩国19禁主播裸免费福利 | 久草国产在线视频 | 国产日韩欧美 | 中文字幕一区二区三 | 国产在线观看xxxx免费 | 91精品成人福利在线播放 | 国产手机免费视频 | 欧美成人午夜影院 | 国产三级日本三级日产三 | 成人全黄三级视频在线观看 | 久久怡红院亚欧成人影院 | 亚洲国语在线视频手机在线 | 看片亚洲 | 不卡一区二区在线 | 九九国产精品 | aaaa欧美高清免费 | 成人综合婷婷国产精品久久免费 | 国产精品久久久久久久久久直 | 午夜三级a三点 | 久久久久综合一本久道 | 久久91精品国产91久久小草 | 亚洲视频在线免费观看 | 中国女警察一级毛片视频 | 欧美成人乱弄视频 | 特及毛片 | 欧美一区二区三区男人的天堂 | 91青草久久久久久清纯 | 亚洲综合色视频在线观看 | 高清日本无a区 | 亚洲三级黄 | 国产露脸3p普通话 | 亚洲国产激情一区二区三区 |