欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何更好的使用Java8中方法引用詳解

 更新時間:2017年09月08日 11:14:28   作者:颯然Hang  
在Java8中,我們可以直接通過方法引用來簡寫lambda表達式中已經(jīng)存在的方法,這種特性就叫做方法引用(Method Reference)。下面這篇文章主要給大家介紹了關(guān)于如何更好的使用Java8中方法引用的相關(guān)資料,需要的朋友可以參考下。

前言

方法引用是用來直接訪問類或者實例的已經(jīng)存在的方法或者構(gòu)造方法。方法引用提供了一種引用而不執(zhí)行方法的方式,它需要由兼容的函數(shù)式接口構(gòu)成的目標(biāo)類型上下文。計算時,方法引用會創(chuàng)建函數(shù)式接口的一個實例。

當(dāng)Lambda表達式中只是執(zhí)行一個方法調(diào)用時,不用Lambda表達式,直接通過方法引用的形式可讀性更高一些。方法引用是一種更簡潔易懂的Lambda表達式。

注意:方法引用是一個Lambda表達式,其中方法引用的操作符是雙冒號"::"。

在Java8中,使用方法引用非常簡單,如String::isEmpty,但無法使用它否定的方法引用。本文內(nèi)容即如何解決此問題使得我們能夠更加全面地使用方法引用。

首先看一個使用方法引用的例子:

Stream.of("A", "", "B").filter(String::isEmpty).count()

上面代碼的輸出為1,即空字符串的數(shù)目。如果我們想要獲取非空字符串的數(shù)目,就不能直接使用方法引用了。

Stream.of("A", "", "B").filter(s -> !s.isEmpty()).count()

Java8中的Predicate,有predicate.negate()可以轉(zhuǎn)換為斷言的否定形式,但String::isEmpty卻無法這么做(String::isEmpty.negate()或者!String::isEmpty )。因為方法引用并不是一個lambda或者函數(shù)接口,它能夠被解析為一個或者多個函數(shù)接口。如,String::isEmpty至少可以被解析如下:

Predicate<String>
Function<String, Boolean>

為了解決上述的問題,我們可以通過某種機制顯式地將方法引用轉(zhuǎn)換為一個函數(shù)接口:

public static <T> Predicate<T> as(Predicate<T> predicate) {
 return predicate;
}

通過使用一個靜態(tài)方法,接受方法引用參數(shù),返回一個函數(shù)接口,即可實現(xiàn)方法引用到函數(shù)接口的轉(zhuǎn)換。接著,我們就可以使用方法引用來實現(xiàn)上面例子中的獲取非空字符串的數(shù)目。

Stream.of("A", "", "B").filter(as(String::isEmpty).negate()).count();

進一步還能使用各種組合的Predicate。

.filter(as(String::isEmpty).negate().and("A"::equals))

由于一個方法引用可能會被解析為多種函數(shù)接口,因此如果我們實現(xiàn)很多參數(shù)不同的as方法,那么很容易造成混淆。更好的方式則是在方法名中加入函數(shù)參數(shù)的類型來區(qū)分。

import java.util.function.*;

public class FunctionCastUtil {
 public static <T, U> BiConsumer<T, U> asBiConsumer(BiConsumer<T, U> biConsumer) {
 return biConsumer;
 }
 public static <T, U, R> BiFunction<T, U, R> asBiFunction(BiFunction<T, U, R> biFunction) {
 return biFunction;
 }
 public static <T> BinaryOperator<T> asBinaryOperator(BinaryOperator<T> binaryOperator) {
 return binaryOperator;
 }
 public static <T, U> BiPredicate<T, U> asBiPredicate(BiPredicate<T, U> biPredicate) {
 return biPredicate;
 }
 public static BooleanSupplier asBooleanSupplier(BooleanSupplier booleanSupplier) {
 return booleanSupplier;
 }
 public static <T> Consumer<T> asConsumer(Consumer<T> consumer) {
 return consumer;
 }
 public static DoubleBinaryOperator asDoubleBinaryOperator(DoubleBinaryOperator doubleBinaryOperator) {
 return doubleBinaryOperator;
 }
 public static DoubleConsumer asDoubleConsumer(DoubleConsumer doubleConsumer) {
 return doubleConsumer;
 }
 public static <R> DoubleFunction<R> asDoubleFunction(DoubleFunction<R> doubleFunction) {
 return doubleFunction;
 }
 public static DoublePredicate asDoublePredicate(DoublePredicate doublePredicate) {
 return doublePredicate;
 }
 public static DoubleToIntFunction asDoubleToIntFunction(DoubleToIntFunction doubleToIntFunctiontem) {
 return doubleToIntFunctiontem;
 }
 public static DoubleToLongFunction asDoubleToLongFunction(DoubleToLongFunction doubleToLongFunction) {
 return doubleToLongFunction;
 }
 public static DoubleUnaryOperator asDoubleUnaryOperator(DoubleUnaryOperator doubleUnaryOperator) {
 return doubleUnaryOperator;
 }
 public static <T, R> Function<T, R> asFunction(Function<T, R> function) {
 return function;
 }
 public static IntBinaryOperator asIntBinaryOperator(IntBinaryOperator intBinaryOperator) {
 return intBinaryOperator;
 }
 public static IntConsumer asIntConsumer(IntConsumer intConsumer) {
 return intConsumer;
 }
 public static <R> IntFunction<R> asIntFunction(IntFunction<R> intFunction) {
 return intFunction;
 }
 public static IntPredicate asIntPredicate(IntPredicate intPredicate) {
 return intPredicate;
 }
 public static IntSupplier asIntSupplier(IntSupplier intSupplier) {
 return intSupplier;
 }
 public static IntToDoubleFunction asIntToDoubleFunction(IntToDoubleFunction intToDoubleFunction) {
 return intToDoubleFunction;
 }
 public static IntToLongFunction asIntToLongFunction(IntToLongFunction intToLongFunction) {
 return intToLongFunction;
 }
 public static IntUnaryOperator asIntUnaryOperator(IntUnaryOperator intUnaryOperator) {
 return intUnaryOperator;
 }
 public static LongBinaryOperator asLongBinaryOperator(LongBinaryOperator longBinaryOperator) {
 return longBinaryOperator;
 }
 public static LongConsumer asLongConsumer(LongConsumer longConsumer) {
 return longConsumer;
 }
 public static <R> LongFunction<R> asLongFunction(LongFunction<R> longFunction) {
 return longFunction;
 }
 public static LongPredicate asLongPredicate(LongPredicate longPredicate) {
 return longPredicate;
 }
 public static <T> LongSupplier asLongSupplier(LongSupplier longSupplier) {
 return longSupplier;
 }
 public static LongToDoubleFunction asLongToDoubleFunction(LongToDoubleFunction longToDoubleFunction) {
 return longToDoubleFunction;
 }
 public static LongToIntFunction asLongToIntFunction(LongToIntFunction longToIntFunction) {
 return longToIntFunction;
 }
 public static LongUnaryOperator asLongUnaryOperator(LongUnaryOperator longUnaryOperator) {
 return longUnaryOperator;
 }
 public static <T> ObjDoubleConsumer<T> asObjDoubleConsumer(ObjDoubleConsumer<T> objDoubleConsumer) {
 return objDoubleConsumer;
 }
 public static <T> ObjIntConsumer<T> asObjIntConsumer(ObjIntConsumer<T> objIntConsumer) {
 return objIntConsumer;
 }
 public static <T> ObjLongConsumer<T> asObjLongConsumer(ObjLongConsumer<T> objLongConsumer) {
 return objLongConsumer;
 }
 public static <T> Predicate<T> asPredicate(Predicate<T> predicate) {
 return predicate;
 }
 public static <T> Supplier<T> asSupplier(Supplier<T> supplier) {
 return supplier;
 }
 public static <T, U> ToDoubleBiFunction<T, U> asToDoubleBiFunction(ToDoubleBiFunction<T, U> toDoubleBiFunction) {
 return toDoubleBiFunction;
 }
 public static <T> ToDoubleFunction<T> asToDoubleFunction(ToDoubleFunction<T> toDoubleFunction) {
 return toDoubleFunction;
 }
 public static <T, U> ToIntBiFunction<T, U> asToIntBiFunction(ToIntBiFunction<T, U> toIntBiFunction) {
 return toIntBiFunction;
 }
 public static <T> ToIntFunction<T> asToIntFunction(ToIntFunction<T> ioIntFunction) {
 return ioIntFunction;
 }
 public static <T, U> ToLongBiFunction<T, U> asToLongBiFunction(ToLongBiFunction<T, U> toLongBiFunction) {
 return toLongBiFunction;
 }
 public static <T> ToLongFunction<T> asToLongFunction(ToLongFunction<T> toLongFunction) {
 return toLongFunction;
 }
 public static <T> UnaryOperator<T> asUnaryOperator(UnaryOperator<T> unaryOperator) {
 return unaryOperator;
 }
 private FunctionCastUtil() {
 }
}

Stream.of("A", "", "B").filter(asPredicate(String::isEmpty).negate()).count();

英文原文:https://dzone.com/articles/put-your-java-8-method-references-to-work

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • mybatis plus表的創(chuàng)建時間和修改時間的操作方法

    mybatis plus表的創(chuàng)建時間和修改時間的操作方法

    這篇文章主要介紹了mybatis plus表的創(chuàng)建時間和修改時間的實現(xiàn)方法,本文給大家分享兩種方法,每種方法通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-09-09
  • Java8的default和static關(guān)鍵字的使用講解

    Java8的default和static關(guān)鍵字的使用講解

    今天小編就為大家分享一篇關(guān)于Java8的default和static關(guān)鍵字的使用講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • httpclient提交json參數(shù)的示例詳解

    httpclient提交json參數(shù)的示例詳解

    httpclient使用post提交json參數(shù),和使用表單提交區(qū)分,本文結(jié)合示例代碼講解的非常詳細(xì),補充介紹了HttpClient請求傳json參數(shù)的案例代碼,感興趣的朋友一起看看吧
    2024-02-02
  • FreeSWITCH跨NAT部署配置詳解

    FreeSWITCH跨NAT部署配置詳解

    這篇文章主要為大家介紹了FreeSWITCH跨NAT部署配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • Java設(shè)計模式之模板方法模式詳解

    Java設(shè)計模式之模板方法模式詳解

    這篇文章主要為大家詳細(xì)介紹了Java設(shè)計模式之模板方法模式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • Java常見基本數(shù)據(jù)結(jié)構(gòu)概覽

    Java常見基本數(shù)據(jù)結(jié)構(gòu)概覽

    這篇文章主要介紹了Java常見的幾種基本數(shù)據(jù)結(jié)構(gòu),包括其含義和用法,,需要的朋友可以參考下
    2017-09-09
  • 帶你快速搞定java多線程(4)

    帶你快速搞定java多線程(4)

    這篇文章主要介紹了java多線程編程實例,分享了幾則多線程的實例代碼,具有一定參考價值,加深多線程編程的理解還是很有幫助的,需要的朋友可以參考下
    2021-07-07
  • java中如何反射獲取一個類

    java中如何反射獲取一個類

    大家說說對java反射的理解,使用反射如何獲取一個類,下面小編給大家整理一些資料,關(guān)于java中如何反射獲取一個類,有需要的朋友可以參考下
    2015-08-08
  • java實現(xiàn)一個桌球小游戲

    java實現(xiàn)一個桌球小游戲

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)一個桌球小游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • SpringBoot對PDF進行模板內(nèi)容填充與電子簽名合并詳解

    SpringBoot對PDF進行模板內(nèi)容填充與電子簽名合并詳解

    這篇文章主要為大家詳細(xì)介紹了SpringBoot對PDF進行模板內(nèi)容填充與電子簽名合并的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考下
    2023-12-12

最新評論