在Java中,是否有可能让一个lambda接受多种不同的类型?
即:
单变量有效:
Function <Integer, Integer> adder = i -> i + 1;
System.out.println (adder.apply (10));
可变参数也可以工作:
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的函数包,但不能找到。这可能吗?
在这种情况下,你可以使用默认库(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 "";
};
}}
对于有两个参数的东西,你可以使用biffunction。如果你需要更多,你可以定义自己的函数接口,如下所示:
@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";
};
某个lambda函数:
import org.junit.Test;
import java.awt.event.ActionListener;
import java.util.function.Function;
public class TestLambda {
@Test
public void testLambda() {
System.out.println("test some lambda function");
////////////////////////////////////////////
//1-any input | any output:
//lambda define:
Runnable lambda1 = () -> System.out.println("no parameter");
//lambda execute:
lambda1.run();
////////////////////////////////////////////
//2-one input(as ActionEvent) | any output:
//lambda define:
ActionListener lambda2 = (p) -> System.out.println("One parameter as action");
//lambda execute:
lambda2.actionPerformed(null);
////////////////////////////////////////////
//3-one input | by output(as Integer):
//lambda define:
Function<String, Integer> lambda3 = (p1) -> {
System.out.println("one parameters: " + p1);
return 10;
};
//lambda execute:
lambda3.apply("test");
////////////////////////////////////////////
//4-two input | any output
//lambda define:
TwoParameterFunctionWithoutReturn<String, Integer> lambda4 = (p1, p2) -> {
System.out.println("two parameters: " + p1 + ", " + p2);
};
//lambda execute:
lambda4.apply("param1", 10);
////////////////////////////////////////////
//5-two input | by output(as Integer)
//lambda define:
TwoParameterFunctionByReturn<Integer, Integer> lambda5 = (p1, p2) -> {
System.out.println("two parameters: " + p1 + ", " + p2);
return p1 + p2;
};
//lambda execute:
lambda5.apply(10, 20);
////////////////////////////////////////////
//6-three input(Integer,Integer,String) | by output(as Integer)
//lambda define:
ThreeParameterFunctionByReturn<Integer, Integer, Integer> lambda6 = (p1, p2, p3) -> {
System.out.println("three parameters: " + p1 + ", " + p2 + ", " + p3);
return p1 + p2 + p3;
};
//lambda execute:
lambda6.apply(10, 20, 30);
}
@FunctionalInterface
public interface TwoParameterFunctionWithoutReturn<T, U> {
public void apply(T t, U u);
}
@FunctionalInterface
public interface TwoParameterFunctionByReturn<T, U> {
public T apply(T t, U u);
}
@FunctionalInterface
public interface ThreeParameterFunctionByReturn<M, N, O> {
public Integer apply(M m, N n, O o);
}
}