[英]How to assign lambda expression to a variable?
I have a kind of calculator: 我有一种计算器:
sum = value1 + value2;
multiplication = value1 * value2;
division = (value1 / value2);
average = (sum / 2);
if (operation.compareToIgnoreCase("add") == 0)
result = sum;
else if (operation.compareToIgnoreCase("mult") == 0)
result = multiplication;
else if (operation.compareToIgnoreCase("div") == 0)
result = division;
else if (operation.compareToIgnoreCase("avg") == 0)
result = average;
And i have to turn this to lambda expressions: 我必须将其转换为lambda表达式:
result = (request.getValue1(), request.getValue2()) -> {
if (request.getOperation().compareToIgnoreCase("add") == 0) return request.getValue1() + request.getValue2();
else if (request.getOperation().compareToIgnoreCase("mult") == 0) return request.getValue1() * request.getValue2();
else if (request.getOperation().compareToIgnoreCase("div") == 0) return request.getValue1() / request.getValue2();
else if (request.getOperation().compareToIgnoreCase("avg") == 0) return (request.getValue1() + request.getValue2())/2;
}
But it doesnt work. 但它不起作用。 Can you guys help?
你们可以帮忙吗? Thanks
谢谢
For example: 例如:
BiFunction<Number,Number,Number> sum = (value1, value2) -> value1 + value2;
and you can use it like this: 您可以像这样使用它:
sum.apply(value1,value2)
Also you can store your set of BiFunction
in a Map
: 您也可以在
Map
存储您的BiFunction
集:
Map<String,BiFunction<Number, Number, Number>> operations = new HashMap<>();
operations.put("sum", sum);
If you do the same for all operations, you call them as it follows: 如果对所有操作都执行相同的操作,请按以下方式调用它们:
operations.get("sum").apply(value1, value2);
So, the result can be computed as is follows: 因此,可以如下计算结果:
result = (value1, value2, operation) -> operations.get(operation).apply(value1, value2);
You can create a map with a String and DoubleBinaryOperator
or an IntBinaryOperator
depends on the type you want like so : 您可以使用String和
DoubleBinaryOperator
或IntBinaryOperator
创建地图, IntBinaryOperator
取决于您想要的类型,如下所示:
Map<String, DoubleBinaryOperator> map = new HashMap<>();
map.put("add", (v1, v2) -> v1 + v2);
map.put("mult", (v1, v2) -> v1 * v2);
map.put("div", (v1, v2) -> v1 / v2);
map.put("avg", (v1, v2) -> (v1 * v2) / 2);
Then you can call that map with the operation you want for example : 然后,您可以使用所需的操作来调用该地图,例如:
// an example
String operation = "add";
Double reslt = map.get(operation).applyAsDouble(3, 5);
from the example it's not very clear, what result
should be. 从示例中还不清楚,
result
应该是什么。 So let me rewrite it including types: 因此,让我重写它,包括类型:
Function<Request, Double> calculator = (request) -> {
if (request.getOperation().compareToIgnoreCase("add") == 0) return request.getValue1() + request.getValue2();
else if (request.getOperation().compareToIgnoreCase("mult") == 0) return request.getValue1() * request.getValue2();
else if (request.getOperation().compareToIgnoreCase("div") == 0) return request.getValue1() / request.getValue2();
else if (request.getOperation().compareToIgnoreCase("avg") == 0) return (request.getValue1() + request.getValue2())/2;
else throw new IllegalArgumentException();
};
double result = calculator.apply(new Request("add", 2, 4));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.