java lambda 可以有 1 个以上的参数吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27872387/
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
Can a java lambda have more than 1 parameter?
提问by Leo Ufimtsev
In Java, is it possible to have a lambda accept multiple different types?
在 Java 中,是否可以让 lambda 接受多种不同的类型?
I.e: Single variable works:
即:单变量工作:
Function <Integer, Integer> adder = i -> i + 1;
System.out.println (adder.apply (10));
Varargs also work:
可变参数也有效:
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 })));
But I want something that can accept many different types of arguments, e.g:
但我想要一些可以接受许多不同类型参数的东西,例如:
Function <String, Integer, Double, Person, String> myLambda = a , b, c, d-> {
[DO STUFF]
return "done stuff"
};
The main use is to have small inline functions inside functions for convenience.
主要用途是为了方便起见,在函数内部有小的内联函数。
I've looked around google and inspected Java's Function Package, but could not find. Is this possible?
我环顾谷歌并检查了Java的功能包,但找不到。这可能吗?
采纳答案by Sotirios Delimanolis
It's possible if you define such a functional interface with multiple type parameters. There is no such built in type. (There are a few limited types with multiple parameters.)
如果您使用多个类型参数定义这样的功能接口,则是可能的。没有这样的内置类型。(有几个具有多个参数的有限类型。)
@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';
}
There's also no way to define a variable number of type parameters, if that's what you were asking about.
如果您要问的是,也无法定义可变数量的类型参数。
Some languages, like Scala, define a number of built in such types, with 1, 2, 3, 4, 5, 6, etc. type parameters.
一些语言,如 Scala,定义了许多内置的类型,具有 1、2、3、4、5、6 等类型参数。
回答by tbodt
For something with 2 parameters, you could use BiFunction
. If you need more, you can define your own function interface, like so:
对于有 2 个参数的东西,你可以使用BiFunction
. 如果需要更多,可以定义自己的函数接口,如下所示:
@FunctionalInterface
public interface FourParameterFunction<T, U, V, W, R> {
public R apply(T t, U u, V v, W w);
}
If there is more than one parameter, you need to put parentheses around the argument list, like so:
如果有多个参数,则需要在参数列表周围加上括号,如下所示:
FourParameterFunction<String, Integer, Double, Person, String> myLambda = (a, b, c, d) -> {
// do something
return "done something";
};
回答by ayurchuk
For this case you could use interfaces from default library (java 1.8):
对于这种情况,您可以使用默认库(java 1.8)中的接口:
java.util.function.BiConsumer
java.util.function.BiFunction
There is a small (not the best) example of default method in interface:
接口中有一个默认方法的小(不是最好的)示例:
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 "";
};
}}
回答by mel3kings
Another alternative, not sure if this applies to your particular problem but to some it may be applicable is to use UnaryOperator
in java.util.function library.
where it returns same type you specify, so you put all your variables in one class and is it as a parameter:
另一种选择,不确定这是否适用于您的特定问题,但对某些可能适用的是UnaryOperator
在 java.util.function 库中使用。它返回您指定的相同类型,因此您将所有变量放在一个类中并将其作为参数:
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;
}
}
So the class you have, in this case Person
, can have numerous instance variables and won't have to change the parameter of your lambda expression.
因此,在这种情况下Person
,您拥有的类可以具有多个实例变量,并且不必更改 lambda 表达式的参数。
For those interested, I've written notes on how to use java.util.function library: http://sysdotoutdotprint.com/index.php/2017/04/28/java-util-function-library/
对于那些感兴趣的人,我已经写了关于如何使用 java.util.function 库的笔记:http: //sysdotoutdotprint.com/index.php/2017/04/28/java-util-function-library/
回答by PetroCliff
You could also use jOOL library - https://github.com/jOOQ/jOOL
您也可以使用 jOOL 库 - https://github.com/jOOQ/jOOL
It has already prepared function interfaces with different number of parameters. For instance, you could use org.jooq.lambda.function.Function3
, etc from Function0
up to Function16
.
它已经准备了不同数量参数的函数接口。例如,您可以使用org.jooq.lambda.function.Function3
, etc 从Function0
最多到Function16
.
回答by amoljdv06
To make the use of lambda : There are three type of operation:
1. Accept parameter --> Consumer
2. Test parameter return boolean --> Predicate
3. Manipulate parameter and return value --> Function
使用 lambda : 有三种类型的操作:
1. 接受参数 --> 消费者
2. 测试参数返回布尔值 --> 谓词
3. 操作参数和返回值 --> 函数
Java Functional interface upto two parameter:
Single parameter interface
Consumer
Predicate
Function
Two parameter interface
BiConsumer
BiPredicate
BiFunction
Java 函数接口最多两个参数:
单参数接口
Consumer
Predicate
Function
二参数接口
BiConsumer
BiPredicate
BiFunction
For more than two, you have to create functional interface as follow(Consumer type):
对于两个以上,您必须创建如下功能接口(消费者类型):
@FunctionalInterface
public interface FiveParameterConsumer<T, U, V, W, X> {
public void accept(T t, U u, V v, W w, X x);
}