簡體   English   中英

Java Function in 1.7(轉換lambda表達式)

[英]Java Function in 1.7 (convert lambda expressions)

我正在處理一個舊的 java 應用程序,它使用 Java 1.7。 我想使用java.util.function.Function接口。 但是,這僅在 Java 1.8+ 中受支持。

所以我想在Java 1.7寫自己的實現。

import java.util.Objects;

public interface Function<T, R> {
    
    R apply(T t);
    
    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v)); // lambda expression
    }
    
    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t)); // lambda expression
    }
    
    static <T> Function<T, T> identity() {
        return t -> t; // lambda expression
    }
}

以上給出了 lambda 表達式的編譯錯誤。

Java 1.7 上面的lambda 表達式怎么寫?

由於您實際上無法使用默認方法創建接口,因此我認為您最好的機會是 static 方法。

public interface Function<T, R> {
    R apply(T t);

    public static <T, V, R> Function<V, R> compose(Function<? super V, ? extends T> before, Function<? super T, ? super R> after) {
        return new CombiningFunction<T, V, R>(before, after);
    }
    
    public static <T, R, V> Function<T, V> andThen(Function<? super T, ? super R> before, Function<? super R, ? extends V> after) {
        return new CombiningFunction<T, V, R>(before, after);
    }
    
    static <T> Function<T, T> identity() {
        return new Function<T, T> {
            T apply(T t) { return t; }
        }
    }
}

class CombiningFunction<T, V, R> implements Function<T, R> {
    Function<T, V> first;
    Function<V, R> second;

    public R apply(T t) {
        V intermediate = first.apply(t);
        return second.apply(intermediate);
    }
}

但是就像 KarelG 在評論中提到的那樣,這樣做真的不可取; 更不用說這不像 Java 8 那樣優雅; 畢竟,函數式接口非常適合 lambda,如果你沒有它們,所有的使用都會像上面的實現一樣笨拙。

您可以使用IntellijIdea自動更改它, IntellijIdea的結果是:

import java.util.Objects;

public interface Function<T, R> {

    R apply(T t);

    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return new Function<V, R>() {
            @Override
            public R apply(V v) {
                return Function.this.apply(before.apply(v));
            }
        }; // lambda expression
    }

    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        Function<T, V> tvFunction = new Function<T, V>() {
            @Override
            public V apply(T t) {
                return after.apply(Function.this.apply(t));
            }
        };
        return tvFunction; // lambda expression
    }

    static <T> Function<T, T> identity() {
        return new Function<T, T>() {
            @Override
            public T apply(T t) {
                return t;
            }
        }; // lambda expression
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM