av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

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

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

java 8引入了lambda表達(dá)式,lambda表達(dá)式實(shí)際上表示的就是一個(gè)匿名的function。

在java 8之前,如果需要使用到匿名function需要new一個(gè)類的實(shí)現(xiàn),但是有了lambda表達(dá)式之后,一切都變的非常簡介。

我們看一個(gè)之前講線程池的時(shí)候的一個(gè)例子:

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

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

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

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

看起是不是很簡單,使用lambda表達(dá)式就可以省略匿名類的構(gòu)造,并且可讀性更強(qiáng)。

那么是不是所有的匿名類都可以用lambda表達(dá)式來重構(gòu)呢?也不是。

我們看下Runnable類有什么特點(diǎn):

@FunctionalInterfacepublic interface Runnable

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

Functional Interface

Functional Interface是指帶有 @FunctionalInterface 注解的interface。它的特點(diǎn)是其中只有一個(gè)子類必須要實(shí)現(xiàn)的abstract方法。如果abstract方法前面帶有default關(guān)鍵字,則不做計(jì)算。

其實(shí)這個(gè)也很好理解,因?yàn)镕unctional Interface改寫成為lambda表達(dá)式之后,并沒有指定實(shí)現(xiàn)的哪個(gè)方法,如果有多個(gè)方法需要實(shí)現(xiàn)的話,就會(huì)有問題。

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

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

根據(jù)要實(shí)現(xiàn)的方法參數(shù)和返回值的不同,F(xiàn)unctional Interface可以分為很多種,下面我們分別來介紹。

Function:一個(gè)參數(shù)一個(gè)返回值

Function接口定義了一個(gè)方法,接收一個(gè)參數(shù),返回一個(gè)參數(shù)。

@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);

一般我們在對集合類進(jìn)行處理的時(shí)候,會(huì)用到Function。

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

上面的例子中我們調(diào)用了map的computeIfAbsent方法,傳入一個(gè)Function。

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

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

Function沒有指明參數(shù)和返回值的類型,如果需要傳入特定的參數(shù),則可以使用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);}

如果需要返回特定的參數(shù),則可以使用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);}

如果要同時(shí)指定參數(shù)和返回值,則可以使用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:接收兩個(gè)參數(shù),一個(gè)返回值

如果需要接受兩個(gè)參數(shù),一個(gè)返回值的話,可以使用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);

我們看一個(gè)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

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

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

Consumer:接收一個(gè)參數(shù),不返回值

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

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

看一個(gè)Consumer的具體應(yīng)用:

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

Predicate:接收一個(gè)參數(shù),返回boolean

Predicate接收一個(gè)參數(shù),返回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);

我們看一個(gè)BinaryOperator的例子:

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

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

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

總結(jié)

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

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 午夜一区二区三区视频 | 五月天综合影院 | 三级成人在线 | 激情的网站| 欧美电影在线观看网站 | 99在线精品视频 | 盗摄精品av一区二区三区 | 国产性网 | 日日操视频 | 一区二区小视频 | 中文字幕福利视频 | 日韩视频精品在线 | 不卡的av在线 | 色综合视频 | 在线视频一区二区三区 | 婷婷综合 | 国产精品久久久久久久久久免费看 | 国产亚洲黄色片 | 久久大陆 | 日韩欧美国产一区二区三区 | 91亚洲国产成人久久精品网站 | 成人精品鲁一区一区二区 | 久久久久久综合 | 日韩中文字幕一区 | 国产综合精品 | 欧美日韩精品免费 | 99精品欧美一区二区三区 | 一区二区三区日韩 | 欧美在线综合 | 高清国产午夜精品久久久久久 | 国产免费一区二区三区网站免费 | 成人av在线播放 | 日韩黄| 中文字幕国产精品 | 永久av| 狠狠的干狠狠的操 | 91精产国品一二三区 | 国产精品一区二区在线 | 男人天堂99 | 九九久久免费视频 | 人人爽人人爽 |