gpt4 book ai didi

java - 检索 map 中的对象

转载 作者:行者123 更新时间:2023-11-29 07:11:20 26 4
gpt4 key购买 nike

当我更改了一个对象的实例变量时,我试图检索一个对象作为 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

但是,如果我不更改权重值(省略行: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

它实际上找到了对象。它是否以两种方式达到预期的输出,或者它是否也应该在第一个版本中找到并检索到对象?

最佳答案

将可变对象作为键存储在哈希表中时应该非常小心。哈希码仅在对象插入时计算一次。如果您稍后以影响其哈希码的方式更改对象,则很可能无法找到它。

每当进行此类更改时,您都需要删除该对象并重新插入它。

关于java - 检索 map 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13661699/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com