我能用Python做到这一点,我的Python代码如下:
signs = {"+" : lambda a, b : a + b, "-" : lambda a, b : a - b}
a = 5
b = 3
for i in signs.keys():
print(signs[i](a,b))
输出结果为:
8
2
我该如何通过HashMap在Java中实现相同的操作?
我能用Python做到这一点,我的Python代码如下:
signs = {"+" : lambda a, b : a + b, "-" : lambda a, b : a - b}
a = 5
b = 3
for i in signs.keys():
print(signs[i](a,b))
输出结果为:
8
2
我该如何通过HashMap在Java中实现相同的操作?
在这种情况下,您可以像这样使用BinaryOperator<Integer>
:
BinaryOperator<Integer> add = (a, b) -> a + b;//lambda a, b : a + b
BinaryOperator<Integer> sub = (a, b) -> a - b;//lambda a, b : a - b
// Then create a new Map which take the sign and the corresponding BinaryOperator
// equivalent to signs = {"+" : lambda a, b : a + b, "-" : lambda a, b : a - b}
Map<String, BinaryOperator<Integer>> signs = Map.of("+", add, "-", sub);
int a = 5; // a = 5
int b = 3; // b = 3
// Loop over the sings map and apply the operation
signs.values().forEach(v -> System.out.println(v.apply(a, b)));
输出
8
2
Map.of("+", add, "-", sub);
这段代码是在Java 10中使用的,如果你并没有使用Java 9+版本,那么你可以像下面这样向你的map中添加内容:Map<String, BinaryOperator<Integer>> signs = new HashMap<>();
signs.put("+", add);
signs.put("-", sub);
正如@Boris the Spider和@Holger在评论中所述,最好使用IntBinaryOperator
来避免装箱,这样你的代码最终可以看起来像这样:
// signs = {"+" : lambda a, b : a + b, "-" : lambda a, b : a - b}
Map<String, IntBinaryOperator> signs = Map.of("+", (a, b) -> a + b, "-", (a, b) -> a - b);
int a = 5; // a = 5
int b = 3; // b = 3
// for i in signs.keys(): print(signs[i](a,b))
signs.values().forEach(v -> System.out.println(v.applyAsInt(a, b)));
IntBinaryOperator
,我会学习一下。我看到了你的答案并发现你已经在使用它了,+1。 - Youcef LAIDANI(int, int)
并返回一个int
类型的值,而不是Integer
类型的 - 所以避免了装箱操作。你的回答很好,加一分! - Boris the Spidervar signs = Map.of("+", add, "-", sub);
。 - fpsMap<String, BinaryOperator<Integer>> signs = Map.of("+", (a, b) -> a + b, "-", (a, b) -> a - b);
;这更接近于 OP 的 Python 代码。当然,每当使用案例允许使用“IntBinaryOperator”时,最好使用它,因为它避免了装箱开销(并且比“BinaryOperator<Integer>”甚至更短)... - Holger创建一个漂亮的、类型安全的enum
:
enum Operator implements IntBinaryOperator {
PLUS("+", Integer::sum),
MINUS("-", (a, b) -> a - b);
private final String symbol;
private final IntBinaryOperator op;
Operator(final String symbol, final IntBinaryOperator op) {
this.symbol = symbol;
this.op = op;
}
public String symbol() {
return symbol;
}
@Override
public int applyAsInt(final int left, final int right) {
return op.applyAsInt(left, right);
}
}
对于其他运算符,您可能希望使用返回 double
而不是 int
的 lambda。
现在,只需将其放入 Map
中:
final var operators = Arrays.stream(Operator.values())
.collect(toMap(Operator::symbol, identity()));
不过针对你的例子,你根本不需要使用 Map
:
Arrays.stream(Operator.values())
.mapToInt(op -> op.applyAsInt(a,b))
.forEach(System.out::println);
使用:
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap;
lambda
函数被限制为不允许任何语句。只允许表达式。 - wizzwizz4