Java 1.7中的函数(转换Lambda表达式)

huangapple go评论72阅读模式
英文:

Java Function in 1.7 (convert lambda expressions)

问题

抱歉,我无法提供代码翻译,因为代码的结构和语法需要精确的转换,而不仅仅是文本的翻译。如果您需要将Java 1.8+的lambda表达式转换为Java 1.7,您可能需要重新编写这些表达式,使用Java 1.7支持的传统的匿名内部类来代替。这需要更详细的代码重构,而不仅仅是翻译。

如果您需要帮助将特定的lambda表达式重写为Java 1.7兼容的代码,请提供特定的lambda表达式,我将尽力为您提供相应的建议。

英文:

I am working on an old java application that uses Java 1.7. I would like to make use of the java.util.function.Function interface. However, this is only supported in Java 1.8+.

So I would like to write my own implementation in 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
    }
}

The above gives me compile errors on the lambda expressions.

Question

How do I write the above lambda expressions in Java 1.7?

答案1

得分: 2

由于你实际上不能创建带有默认方法的接口,我认为你最好的选择是使用静态方法。

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而设计的,如果你没有Lambda,所有的用法都会像上面的实现一样笨拙。

英文:

Since you can't actually create an interface with default methods, I think your best chance are static methods.

public interface Function&lt;T, R&gt; {
    R apply(T t);

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

class CombiningFunction&lt;T, V, R&gt; implements Function&lt;T, R&gt; {
    Function&lt;T, V&gt; first;
    Function&lt;V, R&gt; second;

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

But like KarelG mentions in the comments, it's really not advisable to do this; not to mention that this isn't nearly as elegant as in Java 8; after all, functional interfaces are pretty much geared towards lambdas, and if you don't have them, all usage will be as awkward as the implementation above.

答案2

得分: 1

你可以使用 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
    }
}

请注意,我已经保留了原始的代码格式和标记。

英文:

You can use IntellijIdea to change it automatically, There is a result of IntellijIdea:

import java.util.Objects;

public interface Function&lt;T, R&gt; {

    R apply(T t);

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

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

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

huangapple
  • 本文由 发表于 2020年8月4日 15:40:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63242214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定