Rom*_*rov 8 java binary-operators java-8 functional-interface
我正在尝试通过BinaryOperator
功能接口替换用于算术运算的公共开关.
基本方法是:
private static int computeOne(int res, String operand, String operation) {
int number = Integer.parseInt(operand);
switch (operation) {
case "+":
res += number;
break;
case "-":
res -= number;
break;
case "*":
res *= number;
break;
case "/":
res = (number != 0 ? res / number : Integer.MAX_VALUE);
break;
default:
res = 0;
System.out.println("unknown operation");
}
return res;
}
Run Code Online (Sandbox Code Playgroud)
据我所知,编写类似的内容是必要的:
BinaryOperator<Integer> action = (a,b) -> computeExpression(a + operation + b);
action.apply(res, operand);
Run Code Online (Sandbox Code Playgroud)
但我不明白如何避免switch
与computeExpression
此相同computeOne
.
您可以BinaryOperator<Integer>
为每个算术运算定义一个:
// a = operand 1
// b = operand 2
(a, b) -> a * b;
(a, b) -> a + b;
(a, b) -> a / b;
(a, b) -> a - b;
Run Code Online (Sandbox Code Playgroud)
然后你可以应用一个传递2个参数:
// result = operation.apply(a, b);
int result = ((BinaryOperator<Integer>) ((a, b) -> a * b)).apply(2, 2);
Run Code Online (Sandbox Code Playgroud)
我会使用枚举来枚举这些操作:
class Test {
public static void main(String[] args) {
System.out.println(computeOne(4, "2", "/")); // 2
System.out.println(computeOne(4, "2", "*")); // 8
System.out.println(computeOne(4, "2", "-")); // 2
System.out.println(computeOne(4, "2", "+")); // 6
}
private static int computeOne(int res, String operand, String operation) {
return Operation.getOperationBySymbol(operation)
.getBinaryOperator()
.apply(res, Integer.parseInt(operand));
}
private enum Operation {
// operation = symbol, action
MULTIPLICATION("*", (a, b) -> a * b),
ADDITION("+", (a, b) -> a + b),
SUBTRACTION("-", (a, b) -> a - b),
DIVISION("/", (a, b) -> a / b);
private final BinaryOperator<Integer> binaryOperator;
private final String symbol;
Operation(String symbol, BinaryOperator<Integer> binaryOperator) {
this.symbol = symbol;
this.binaryOperator = binaryOperator;
}
public BinaryOperator<Integer> getBinaryOperator() {
return binaryOperator;
}
public String getSymbol() {
return symbol;
}
public static Operation getOperationBySymbol(String symbol) {
for (Operation operation : values()) {
if (operation.getSymbol().equals(symbol)) {
return operation;
}
}
throw new IllegalArgumentException("Unknown symbol: " + symbol);
}
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以使用以下方式"简化"它BiFunction<BinaryOperator<?>, Pair<?, ?>, ?>
:
// BiFunction<Operator, Operands, Result>
// Operator = BinaryOperator<?>
// Operands = Pair<?, ?>
BiFunction<BinaryOperator<Integer>, Pair<Integer, Integer>, Integer> f =
(operator, operands) ->
operator.apply(operands.getKey(), operands.getValue());
f.apply((a, b) -> a + b, new Pair<>(2, 2)); // 4
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
146 次 |
最近记录: |