当我更改对象的实例变量时,我试图将对象检索为 Map 中的键。就我而言,当我在 Map 结构中搜索关键对象时,它应该在 hashCode 方法返回与最初相同的值时出现。但是,根据以下代码,我无法通过更改了变量权重值的方法 get() 检索对象:
public class Car implements Comparable<Car> {
int weight;
String name;
public Car(int w, String n) {
weight=w;
name=n;
}
public boolean equals(Object o){
if(o instanceof Car){
Car d = (Car)o;
return ((d.name.equals(name)) && (d.weight==weight));
}
return false;
}
public int hashCode(){
return weight/2 + 17;
}
public String toString(){
return "I am " +name+ " with weight: "+weight;
}
public int compareTo(Car d){
if(this.weight>d.weight)
return 1;
else if(this.weight<d.weight)
return -1;
else
return this.name.compareTo(d.name);
}
}
public static void main(String[] args) {
Car d1 = new Car(107, "a");
Car d2 = new Car(110, "b");
Car d3 = new Car(110, "c");
Car d4 = new Car(107, "a");
Map<Car, Integer> m = new HashMap<Car, Integer>();
m.put(d1, 1);
m.put(d2, 2);
m.put(d3, 3);
m.put(d4, 16);
System.out.println(m.get(d1).toString());
d1.weight = 34;
System.out.println(m.get(new Car(34, "a")));
for(Map.Entry<Car, Integer> me : m.entrySet())
System.out.println(me.getKey().toString() + " value: " +me.getValue());
}
输出是:
16
null
I am a with weight: 34 16
I am c with weight: 110 3
I am b with weight: 110 2
但是,如果我不更改 weight 的值(省略行:)d1.weight = 34;
,只需使用以下代码行:
System.out.println(m.get(new Car(107, "a")));
输出是:
16
16
I am a with weight: 107 value: 16
I am c with weight: 110 value: 3
I am b with weight: 110 value: 2
它实际上找到了对象。它是否以两种方式达到预期的输出,或者它是否也应该在第一个版本中找到并检索到该对象?