Java 8方法引用使用指南

【編者按】本文做者爲擁有15年 Java 開發經驗的資深程序員 Per-Åke Minborg,主要介紹如何靈活地解析 Java 中的方法引用。文章系國內 ITOM 管理平臺 OneAPM 編譯呈現。html

##方法引用 衆所周知,在Java 8中咱們可使用方法引用。譬如,在咱們須要遍歷流元素時,可使用 String::isEmpty 來引用isEmpty方法。試看下面這段代碼:java

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

運行的結果爲1(由於在這個流中只有一個空元素)。可是,若是咱們要過濾出非空字符串,咱們得寫成.filter(s -> !s.isEmpty())。這是一個Lambda表達式。顯然,這兒有個討厭的不對稱想象。咱們可使用方法引用,但卻不能用它的反式。咱們能夠寫predicate.negate()卻不能寫Stream::isEmpty.negate()!Stream::isEmptygit

爲何呢?這是由於方法引用並不是Lambda表達式或者函數接口。不過,使用Java的類型推導能夠將方法引用解析爲一個或多個函數接口。上例中的String::isEmpty至少能夠解析爲:程序員

Predicate<String>
Function<String, Boolean>

因此,咱們要排除其餘可能性,肯定到底將方法引用轉換爲哪一個函數接口。本文在必定程度上解決了該問題。文中的代碼來自開源項目Speedment,它讓數據庫看起來像Java 8的流。github

##解析方法引用 其實,以靜態方法爲「管道」,能夠部分地解決這個問題——該靜態方法以一個方法引用爲輸入,以特定的函數接口爲其返回。試考慮下面這個簡短的靜態方法:數據庫

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

如今,若是靜態地導入這個方法,事實上,咱們就能更簡單地使用方法引用。以下例所示:服務器

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

這段代碼返回的結果爲2,即流中非空元素的數量。有關方法引用的使用方式,咱們又向前邁進了一步。另外一個好處是,這個解決方案讓咱們更輕鬆地編寫predicates接口,例如:框架

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

##解析全部方法引用 可是,如今仍有一個問題亟待解決。咱們不能隨隨便便地建立一大堆靜態as()函數,由於一個方法引用可能解析爲多種as()方法,正如本文開頭提到的那樣。因此,一個更妙的解決方案,是把函數接口類型名添加至每一個靜態方法,這樣咱們就能夠程序化地爲每一個函數接口轉換方法選擇一個特定的方法引用。咱們有一個工具類,可讓每一個方法引用都轉換爲Java標準包 `java.util.function中任意匹配的函數接口。函數

直接在GitHub下載最新版本工具

import java.util.function.*;
/**
 *
 * @author Per Minborg
 */
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();

##一個更好的解決方案 若是函數接口自己就包含一個接收方法引用並將其轉換爲某類函數接口的靜態方法,那就更好了。舉例來講,標準的Java Predicated函數接口就會變成這樣:

@FunctionalInterface
public interface Predicate<T> {
    boolean test(T t);
    default Predicate<T> and(Predicate<? super T> other) {...}
    default Predicate<T> negate() {...}
    default Predicate<T> or(Predicate<? super T> other) {...}
    static <T> Predicate<T> isEqual(Object targetRef) {...}
    // New proposed support method to return a 
    // Predicate view of a Functional Reference 
    public static <T> Predicate<T> of(Predicate<T> predicate) {
        return predicate;
    }
}

所以,咱們能夠這樣寫:

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

筆者以爲這樣看起來好極了!

快聯繫離你最近的Open JDK開發人員,提出你的修改建議吧!

OneAPM 能爲您提供端到端的 Java 應用性能解決方案,咱們支持全部常見的 Java 框架及應用服務器,助您快速發現系統瓶頸,定位異常根本緣由。分鐘級部署,即刻體驗,Java 監控歷來沒有如此簡單。想閱讀更多技術文章,請訪問 OneAPM 官方技術博客

本文轉自 OneAPM 官方博客

原帖地址:https://dzone.com/articles/put-your-java-8-method-references-to-work

相關文章
相關標籤/搜索