与Scala的Function1
使用拉姆达的是不是straigt前进,为lambda与只有一个abstract
未实现的功能,这是不正确的Scala的Function1
特质的情况下Interface
的工作。
我们可以用一个变通方法是,
首先让定义的建设者,这使得失业各地可重复使用,
生成器Function1
,
package lambdascala;
import scala.Function1;
public class Function1WithLambdaBuilder<P1, R> {
public static interface Function1LambdaApply<P1, R> {
R apply(P1 p1);
}
private Function1LambdaApply<P1, R> lambda;
private Function1<P1, R> function;
public Function1WithLambdaBuilder(Function1LambdaApply<P1, R> lambda) {
this.lambda = lambda;
this.function = new Function1<P1, R>() {
@Override
public R apply(P1 p1) {
return Function1WithLambdaBuilder.this.lambda.apply(p1);
}
};
}
public Function1<P1, R> getFunction() {
return this.function;
}
}
另一个建设者Function2
package lambdascala;
import scala.Function2;
public class Function2WithLambdaBuilder<P1, P2, R> {
public static interface Function2LambdaApply<P1, P2, R> {
R apply(P1 p1, P2 p2);
}
private Function2LambdaApply<P1, P2, R> lambda;
private Function2<P1, P2, R> function;
public Function2WithLambdaBuilder(Function2LambdaApply<P1, P2, R> lambda) {
this.lambda = lambda;
this.function = new Function2<P1,P2, R>() {
@Override
public R apply(P1 p1, P2 p2) {
return Function2WithLambdaBuilder.this.lambda.apply(p1, p2);
}
};
}
public Function2<P1, P2, R> getFunction() {
return this.function;
}
}
您可以按照相同的模式添加更多FunctionN
的建设者。
现在我们可以使用这些助建Function1
和Function2
import lambdascala.Function1WithLambdaBuilder;
import lambdascala.Function2WithLambdaBuilder;
import scala.Function1;
import scala.Function2;
import java.util.List;
public class LambdaTry {
public static void main() {
Function1<List<Long>, List<Long>> changeNothing =
new Function1WithLambdaBuilder<List<Long>, List<Long>>(
// your lambda
(List<Long> list) -> list
).getFunction();
Function1<Integer, Integer> add2 =
new Function1WithLambdaBuilder<Integer, Integer>(
// your lambda
(Integer i) -> i + 2
).getFunction();
Function2<Integer, Integer, Integer> add =
new Function2WithLambdaBuilder<Integer, Integer, Integer>(
// your lambda
(Integer i, Integer j) -> i + j
).getFunction();
System.out.println(add2.apply(12));
System.out.println(add.apply(12, 24));
}
}
您可以为每种类型的单'static'工厂方法替换这些令人费解的建设者,使用标准的'Function'和'BiFunction'接口作为输入而不是定义其他接口。 – Holger
@Holger OP希望使用lambda函数创建'scala.Function1','scala.Function2','scala.Function3'等实例。因为他正在使用这些实例和其他一些需要'scala.Function1' ...实例的API,所以我没有看到'Function'或'BiFunction'如何提供帮助。你可以使用这些发布答案吗? –
只需将'Function1LambdaApply'替换为'Function '和'Function2LambdaApply ''BiFunction '。对于三参数函数没有内置类型,但是您没有为三个参数发布示例。 –
Holger