我有一个班级:
public class Decision {
int leftIndex;
int rightIndex;
String player;
public Decision(int leftIndex, int rightIndex, int player) {
this.leftIndex = leftIndex;
this.rightIndex = rightIndex;
this.player = player;
}
}
和实例化散列映射的类(作为缓存,使用Decision值作为键和整数值)并检查特定Decision值是否已保存结果。如果是这样,我们会返回该结果。否则,我们计算该值并将其存储在hashmap中。我怎样才能让它发挥作用?有一个更好的方法吗?我没有想法,这就是我的想法。
public class Game {
Map<Decision, Integer> hm = new HashMap<Decision, Integer>();
int checkIfExists(int left, int right, String player) {
if (hm.containsKey(new Decision(left, right, player)) {
return hm.get(new Decision(left, right, player));
} else {
// compute value of decision
hm.add(new Decision(left, right, player), result);
}
return result;
}
}