java 7中java 8的功能接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22905562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:49:40  来源:igfitidea点击:

functional interfaces of java 8 in java 7

javacompatibilityjava-7java-8

提问by msung

are the functional interfaces of java 8 available somewhere (i.e. a jar) so I can use them in a Java 7 project? that way I could later on easier port the code to idiomatic java 8. if not, is that technically possible or do they make use of new features like default methods?

java 8 的功能接口是否在某处(即 jar)可用,以便我可以在 Java 7 项目中使用它们?这样我以后可以更轻松地将代码移植到惯用的 java 8。如果没有,那在技术上是否可行,或者他们是否使用了默认方法等新功能?

yes, i meant the interfaces in java.util.function. since adding packages with the java prefix seems to be disallowed importing them from somewhere else is not an option.

是的,我指的是 java.util.function 中的接口。因为添加带有 java 前缀的包似乎不允许从其他地方导入它们不是一种选择。

采纳答案by assylias

A functional interface is simply an interface with only one non-default, non-static method. All interfaces that satisfy that definition can be implemented through a lambda in Java 8.

函数式接口只是一个只有一个非默认、非静态方法的接口。所有满足该定义的接口都可以通过 Java 8 中的 lambda 实现。

For example, Runnableis a functional interface and in Java 8 you can write: Runnable r = () -> doSomething();.

例如,Runnable是一个函数式接口,在 Java 8 中你可以这样写:Runnable r = () -> doSomething();.

Many of the functional interfaces brought by Java 8 are in the java.util.functionpackage. The most common are:

许多被Java 8所带来的功能接口都在java.util.function。最常见的是:

  • Consumer<T>which has a void accept(T t)
  • Supplier<T>which has a T get()
  • Function<T, R>which has a R apply(T t)
  • Predicate<T>which as a boolean test(T t)
  • Consumer<T>其中有一个 void accept(T t)
  • Supplier<T>其中有一个 T get()
  • Function<T, R>其中有一个 R apply(T t)
  • Predicate<T>作为 boolean test(T t)


What you could do at this stage is to use single method interfaces wherever it makes sense, if possible with similar signatures. When you migrate to Java 8 you will be able to easily refactor through your IDE from:

在这个阶段你可以做的是在任何有意义的地方使用单一方法接口,如果可能的话,使用类似的签名。当您迁移到 Java 8 时,您将能够通过您的 IDE 从以下位置轻松重构:

someMethod(new MyConsumer<T>() { public void accept(T t) { use(t); } });

into

进入

someMethod(t -> use(t));

Then change the signature of someMethod(MyConsumer<T> mc)into someMethod(Consumer<T> c), get rid of you MyConsumerinterface and you are done.

然后把 的签名someMethod(MyConsumer<T> mc)改成someMethod(Consumer<T> c),去掉你的MyConsumer界面就大功告成了。

回答by mkdev

Here are the signatures of java 8 main functional interfaces as an addition to assylias answer

以下是作为 assylias 答案的补充的 java 8 主要功能接口的签名

public interface Consumer<T> {
    void accept(T t);
}

public interface Supplier<T> {
    T get();
}

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

public interface Predicate<T> {
    boolean test(T t);
}

回答by Mark Perry

The Java 8 functional interfaces are limited. Use the FunctionalJava types P1, F, F2, F3, ..., F8, TryCatch0, TryCatch1, ..., TryCatch8 to do the same thing now with more functionality.

Java 8 功能接口是有限的。现在使用 FunctionalJava 类型 P1、F、F2、F3、...、F8、TryCatch0、TryCatch1、...、TryCatch8 来做同样的事情,功能更多。

https://functionaljava.ci.cloudbees.com/job/master/javadoc/

https://functionaljava.ci.cloudbees.com/job/master/javadoc/

You can use the Retro Lambda project to compile with Java 8 and lambdas, but target the Java 7 VM. This avoids the whole anonymous inner class nonsense. See the FunctionalJava project for an example (http://www.functionaljava.org/).

您可以使用 Retro Lambda 项目使用 Java 8 和 lambdas 进行编译,但目标是 Java 7 VM。这避免了整个匿名内部类的废话。有关示例,请参阅 FunctionalJava 项目 ( http://www.functionaljava.org/)。

回答by Vlasec

In addition to the answer by @assylias which I think solves the problem in most cases, there is one more option, and that is, to make your own @FunctionalInterfaceand keep it that way.

除了@assylias 的答案(我认为在大多数情况下都可以解决问题)之外,还有一种选择,那就是自己制作@FunctionalInterface并保持这种方式。

It depends on where you use the functions. All of the aforementioned interfaces can be used by JDK utility classes. Predicateallows filtering, Supplierallows object creation, Functionallows mapping ... In fact, Predicateand Supplierare rather straighforward, but Functionand Consumercan be often unclear, especially BiFunction. They can also tie your hands in some use cases.

这取决于您在哪里使用这些功能。JDK 实用程序类可以使用上述所有接口。Predicate允许过滤,Supplier允许创建对象,Function允许映射......其实,Predicate并且Supplier是相当简单明了,但FunctionConsumer可以经常不清楚,尤其是BiFunction。在某些用例中,它们还可以束缚您的手。

You can write your own interface that has any amount of inputs, throws checked exceptions, has generics only where you need them and its name says what it should be used for.

您可以编写自己的接口,该接口具有任意数量的输入、抛出已检查的异常、仅在需要的地方具有泛型,并且其名称说明了它的用途。

//@FunctionalInterface
public interface MyCustomInterface {
    <T> MyCustomOutput myCustomAction(MyCustomInput<T> str) throws MyCustomException;
}

So, while the interfaces provided with JDK are useful, sometimes you might prefer to keep your own solution even in Java 8, just with the annotations, and lambdas instead of anonymous classes.

因此,虽然 JDK 提供的接口很有用,但有时您可能更喜欢保留自己的解决方案,即使在 Java 8 中,只需使用注释和 lambdas 而不是匿名类。