【发布时间】:2017-12-04 20:46:24
【问题描述】:
我在这里尝试做的是使用 Obj 使用键 Key 填充 HashMap ,当我完成时,我希望根据任何可能的键访问这些值。我编写了以下代码,发生的情况是,虽然第一个节目确实显示了我想要的值,但第二个节目引发了NullPointerException。
import java.util.*;
public class My{
public static void main(String[] args){
Map<Key,Obj> myMap = new HashMap<Key,Obj>();
Obj ob1 = new Obj("Nick",19);
Obj ob2 = new Obj("George",17);
Key key1 = new Key(1,2);
Key key2 = new Key(2,1);
myMap.put(key1,ob1);
myMap.put(key2,ob2);
myMap.get(key1).show();
myMap.get(new Key(1,2)).show();
}
我可以说 Java 无法分辨出 new Key(1,2) 与 key1 相等,但我想不出如何克服这个问题。
public class Obj{
private String name;
private int age;
Obj(String name, int age){
this.name = name;
this.age = age;
}
public void show(){
System.out.println(name + " " + age);
}
}
这些是我使用的类
import java.* ;
public class Key{
public int x,y;
Key(int x, int y){
this.x = x;
this.y = y;
}
public boolean equals(Key d){
if ((this.x == d.x)&&(this.y == d.y)){
return true;
}
else{
return false;
}
}
}
【问题讨论】:
-
你需要在
Key中实现hashCode -
同样值得学习的模式是,任何时候看到
if (condition) return true; else return false;,都可以用return condition;替换它——所以你的equals方法可以是return this.x == d.x && this.y == d.y;。 -
无论何时覆盖
equals,您都应该使用相同的功能覆盖hashCode,正是出于这个原因。 -
此外,您没有覆盖从
Object继承的equals方法 - 参数类型是错误的。 -
@janos 和
equals()
标签: java dictionary hashmap