在Java中,是否可以让lambda接受多种不同的类型?
即:单变量工作:
Function <Integer, Integer> adder = i -> i + 1;
System.out.println (adder.apply (10));
Varargs也有效:
Function <Integer [], Integer> multiAdder = ints -> {
int sum = 0;
for (Integer i : ints) {
sum += i;
}
return sum;
};
//....
System.out.println ((multiAdder.apply (new Integer [] { 1, 2, 3, 4 })));
但是我想要一些可以接受许多不同类型参数的东西,例如:
Function <String, Integer, Double, Person, String> myLambda = a , b, c, d-> {
[DO STUFF]
return "done stuff"
};
主要用途是在函数内部使用小内联函数以方便使用。
我查看了谷歌并检查了Java的功能包,但找不到。这可能吗?
如果您使用多个类型参数定义这样的功能接口,则可能。没有这种内置类型。 (有一些有限的类型有多个参数。)
@FunctionalInterface
interface Function<One, Two, Three, Four, Five, Six> {
public Six apply(One one, Two two, Three three, Four four, Five five);
}
public static void main(String[] args) throws Exception {
Function<String, Integer, Double, Void, List<Float>, Character> func = (a, b, c, d, e) -> 'z';
}
如果这就是您所询问的,那么也无法定义可变数量的类型参数。
某些语言(如Scala)定义了许多内置的类型,包括1,2,3,4,5,6等类型参数。
对于具有2个参数的东西,您可以使用BiFunction
。如果您需要更多,可以定义自己的功能界面,如下所示:
@FunctionalInterface
public interface FourParameterFunction<T, U, V, W, R> {
public R apply(T t, U u, V v, W w);
}
如果有多个参数,则需要在参数列表周围加上括号,如下所示:
FourParameterFunction<String, Integer, Double, Person, String> myLambda = (a, b, c, d) -> {
// do something
return "done something";
};
对于这种情况,您可以使用默认库(java 1.8)中的接口:
java.util.function.BiConsumer
java.util.function.BiFunction
接口中有一个小的(不是最好的)默认方法示例:
default BiFunction<File, String, String> getFolderFileReader() {
return (directory, fileName) -> {
try {
return FileUtils.readFile(directory, fileName);
} catch (IOException e) {
LOG.error("Unable to read file {} in {}.", fileName, directory.getAbsolutePath(), e);
}
return "";
};
}}
另一种选择,不确定这是否适用于您的特定问题,但对于某些可能适用的是在java.util.function库中使用UnaryOperator
。它返回您指定的相同类型,因此您将所有变量放在一个类中,并将其作为参数:
public class FunctionsLibraryUse {
public static void main(String[] args){
UnaryOperator<People> personsBirthday = (p) ->{
System.out.println("it's " + p.getName() + " birthday!");
p.setAge(p.getAge() + 1);
return p;
};
People mel = new People();
mel.setName("mel");
mel.setAge(27);
mel = personsBirthday.apply(mel);
System.out.println("he is now : " + mel.getAge());
}
}
class People{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
因此,您拥有的类(在本例中为Person
)可以包含许多实例变量,而不必更改lambda表达式的参数。
对于那些感兴趣的人,我写了关于如何使用java.util.function库的说明:http://sysdotoutdotprint.com/index.php/2017/04/28/java-util-function-library/
你也可以使用jOOL库 - https://github.com/jOOQ/jOOL
它已经准备好了具有不同数量参数的功能接口。例如,你可以使用从org.jooq.lambda.function.Function3
到Function0
的Function16
等。
要使用lambda:有三种类型的操作: 1.接受参数 - >消费者 2.测试参数return boolean - > Predicate 3.操纵参数和返回值 - >功能
Java Functional接口最多有两个参数: 单参数界面 消费者 谓语 功能 两个参数界面 BiConsumer BiPredicate 双功能
对于两个以上,您必须创建如下的功能接口(消费者类型):
@FunctionalInterface
public interface FiveParameterConsumer<T, U, V, W, X> {
public void accept(T t, U u, V v, W w, X x);
}