如何在Java中使用finagle的Future对象的flatMap方法创建Function1对象?
试过这个:
Function1<String, String> f = new Function1<String, String>() { @Override public String apply(String s) { return null; } };但它不起作用:
错误:(22,73)java:不是抽象的,并且不会覆盖抽象方法而是scala.Function1中的$ mcVJ $ sp(scala.Function1)
How do I create Function1 object for use in flatMap method of finagle's Future object in Java?
Tried this:
Function1<String, String> f = new Function1<String, String>() { @Override public String apply(String s) { return null; } };But it doesn't work:
Error:(22, 73) java: is not abstract and does not override abstract method andThen$mcVJ$sp(scala.Function1) in scala.Function1
最满意答案
为了完整起见,这是我上面两个月的评论的答案。
首先是一些进口:
import scala.Function1; import scala.runtime.AbstractFunction1;现在你只需要定义apply方法:
Function1<String, String> f = new AbstractFunction1<String, String>() { public String apply(String s) { return s; } };但是,如果您正在使用Finagle, Twitter的Util库也提供了类似的帮助类:
import com.twitter.util.Function; import scala.Function1;接着:
Function1<String, String> f = new Function<String, String>() { public String apply(String s) { return s; } };后一种选择可能更好 - 我从未真正喜欢使用scala.runtime东西。
For the sake of completeness, here's the answer from my two month-old comment above.
First for some imports:
import scala.Function1; import scala.runtime.AbstractFunction1;And now you only have to define the apply method:
Function1<String, String> f = new AbstractFunction1<String, String>() { public String apply(String s) { return s; } };If you're using Finagle, though, Twitter's Util library also provides a similar helper class:
import com.twitter.util.Function; import scala.Function1;And then:
Function1<String, String> f = new Function<String, String>() { public String apply(String s) { return s; } };This latter option is probably better—I've never really liked explicitly using stuff from scala.runtime.
更多推荐
发布评论