我在 Java 中用 Twowords 类封装了两个元素,这两个元素作为 Hashmap 的 Key 共同确定了 Hashmap 中的 Value ,但是我在写代码的过程中,需要判断 Twowords 对象是否在 Hashmap 里从而确定是否对 Value 赋值或者对 Value 进行自增操作。
但是我写的代码并不能判断 Hashmap 是否有 Key 值,问题应该出在 new 上,请问有没有好的解决办法?
HashMap<Twowords, Double> transitions = new HashMap<Twowords, Double>();
Twowords tmp = new Twowords(first, second);
if (transitions.containsKey(tmp)){
transitions.put(tmp, transitions.get(tmp) + 1.0);
}
else{
transitions.put(tmp, 1.0);
}
也就是说 if 判断永远为假,请问怎么解决呢? 谢谢!