如何在 MAP 上正确添加 BiFunction

How to add BiFunction on a MAP properly

private static final Map<String, BiFunction<String, Player, Player>> SELF_FUNCTION = Map.of
(
    "pAtk", BiFunction<Integer, Player, Player> pAtk = (k, v) -> k.getPAtk(v),
    "mAtk", BiFunction<Integer, Player, Player> mAtk = (k, v) -> k.getMAtk(v),
);

我无法使这段代码正常工作。我想做一个输入并获取密钥并计算 BiFunction 给出的统计数据,但我无法使其工作。

BiFunction<T, U, R>定义中T是第一个参数的类型,U是第二个参数的类型,R是return类型。

因此,假设 class Player 包含方法 Player getPAtk(String v)Player getMAtk(String v)BiFunction<Player, String, Player> 的映射可以创建为:

private static final Map<String, BiFunction<Player, String, Player>> SELF_FUNCTION = Map.of (
        "pAtk", (player, value) -> player.getPAtk(value), // or Player::getPAtk
        "mAtk", (player, value) -> player.getMAtk(value) // or Player::getMAtk
);

或切换 lambda 中的参数以匹配 BiFunction 定义:

private static final Map<String, BiFunction<String, Player, Player>> SELF_FUNCTION = Map.of (
        "pAtk", (value, player) -> player.getPAtk(value),
        "mAtk", (value, player) -> player.getMAtk(value)
);