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

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

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.

  1. import java.util.Objects;
  2. public interface Function<T, R> {
  3. R apply(T t);
  4. default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
  5. Objects.requireNonNull(before);
  6. return (V v) -> apply(before.apply(v)); // lambda expression
  7. }
  8. default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
  9. Objects.requireNonNull(after);
  10. return (T t) -> after.apply(apply(t)); // lambda expression
  11. }
  12. static <T> Function<T, T> identity() {
  13. return t -> t; // lambda expression
  14. }
  15. }

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

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

  1. public interface Function<T, R> {
  2. R apply(T t);
  3. public static <T, V, R> Function<V, R> compose(Function<? super V, ? extends T> before, Function<? super T, ? super R> after) {
  4. return new CombiningFunction<T, V, R>(before, after);
  5. }
  6. public static <T, R, V> Function<T, V> andThen(Function<? super T, ? super R> before, Function<? super R, ? extends V> after) {
  7. return new CombiningFunction<T, V, R>(before, after);
  8. }
  9. static <T> Function<T, T> identity() {
  10. return new Function<T, T> {
  11. T apply(T t) { return t; }
  12. };
  13. }
  14. }
  15. class CombiningFunction<T, V, R> implements Function<T, R> {
  16. Function<T, V> first;
  17. Function<V, R> second;
  18. public R apply(T t) {
  19. V intermediate = first.apply(t);
  20. return second.apply(intermediate);
  21. }
  22. }

但正如KarelG在评论中提到的那样,这确实不是一个明智的做法;更不用说这远不及Java 8中的优雅;毕竟,函数接口基本上是为了Lambda而设计的,如果你没有Lambda,所有的用法都会像上面的实现一样笨拙。

英文:

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

  1. public interface Function&lt;T, R&gt; {
  2. R apply(T t);
  3. 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) {
  4. return new CombiningFunction&lt;T, V, R&gt;(before, after);
  5. }
  6. 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) {
  7. return new CombiningFunction&lt;T, V, R&gt;(before, after);
  8. }
  9. static &lt;T&gt; Function&lt;T, T&gt; identity() {
  10. return new Function&lt;T, T&gt; {
  11. T apply(T t) { return t; }
  12. }
  13. }
  14. }
  15. class CombiningFunction&lt;T, V, R&gt; implements Function&lt;T, R&gt; {
  16. Function&lt;T, V&gt; first;
  17. Function&lt;V, R&gt; second;
  18. public R apply(T t) {
  19. V intermediate = first.apply(t);
  20. return second.apply(intermediate);
  21. }
  22. }

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 的结果:

  1. import java.util.Objects;
  2. public interface Function<T, R> {
  3. R apply(T t);
  4. default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
  5. Objects.requireNonNull(before);
  6. return new Function<V, R>() {
  7. @Override
  8. public R apply(V v) {
  9. return Function.this.apply(before.apply(v));
  10. }
  11. }; // lambda expression
  12. }
  13. default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
  14. Objects.requireNonNull(after);
  15. Function<T, V> tvFunction = new Function<T, V>() {
  16. @Override
  17. public V apply(T t) {
  18. return after.apply(Function.this.apply(t));
  19. }
  20. };
  21. return tvFunction; // lambda expression
  22. }
  23. static <T> Function<T, T> identity() {
  24. return new Function<T, T>() {
  25. @Override
  26. public T apply(T t) {
  27. return t;
  28. }
  29. }; // lambda expression
  30. }
  31. }

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

英文:

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

  1. import java.util.Objects;
  2. public interface Function&lt;T, R&gt; {
  3. R apply(T t);
  4. default &lt;V&gt; Function&lt;V, R&gt; compose(Function&lt;? super V, ? extends T&gt; before) {
  5. Objects.requireNonNull(before);
  6. return new Function&lt;V, R&gt;() {
  7. @Override
  8. public R apply(V v) {
  9. return Function.this.apply(before.apply(v));
  10. }
  11. }; // lambda expression
  12. }
  13. default &lt;V&gt; Function&lt;T, V&gt; andThen(Function&lt;? super R, ? extends V&gt; after) {
  14. Objects.requireNonNull(after);
  15. Function&lt;T, V&gt; tvFunction = new Function&lt;T, V&gt;() {
  16. @Override
  17. public V apply(T t) {
  18. return after.apply(Function.this.apply(t));
  19. }
  20. };
  21. return tvFunction; // lambda expression
  22. }
  23. static &lt;T&gt; Function&lt;T, T&gt; identity() {
  24. return new Function&lt;T, T&gt;() {
  25. @Override
  26. public T apply(T t) {
  27. return t;
  28. }
  29. }; // lambda expression
  30. }
  31. }

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:

确定