我正在使用自定义类Vec作为键HashMap.
但是Vec具有相同的两个相等对象hashCode()将不会映射到相同的键.
我究竟做错了什么?
import java.util.HashMap;
/**
* A 2-element float Vector
*/
class Vec {
public float x;
public float y;
public Vec(float x, float y) {
this.x = x;
this.y = y;
}
public Vec(Vec v) {
this.x = v.x;
this.y = v.y;
}
public boolean equals(Vec v) {
System.out.println("equals called");
return (x == v.x &&
y == v.y);
}
@Override
public int hashCode() {
final int prime = 31;
int result = (Float.floatToIntBits(x)
+ prime * Float.floatToIntBits(y));
System.out.println("hash called: " + result);
return result;
}
}
public class Debug {
public static final void main(String[] args) {
Vec v1 = new Vec(3, 5);
Vec v2 = new Vec(v1);
System.out.println("vecs equal: " + v1.equals(v2));
System.out.println("hashcodes: " + v1.hashCode() + ", " + v2.hashCode());
System.out.println("\nuse map");
HashMap<Vec, Object> map = new HashMap<>();
map.put(v1, new Object());
Object o1 = map.get(v1);
Object o2 = map.get(v2);
System.out.println(o1);
System.out.println(o2);
if (o2 == null) {
throw new RuntimeException("expected o2 not to be null");
}
};
}
Run Code Online (Sandbox Code Playgroud)
产量
equals called
vecs equal: true
hash called: 329252864
hash called: 329252864
hashcodes: 329252864, 329252864
use map
hash called: 329252864
hash called: 329252864
hash called: 329252864
java.lang.Object@2a139a55
null
Exception in thread "main" java.lang.RuntimeException: expected o2 not to be null
Run Code Online (Sandbox Code Playgroud)
这是你的问题:
public boolean equals(Vec v) {
^^^
Run Code Online (Sandbox Code Playgroud)
我建议总是使用@Override注释 - 在这种情况下,它会给你一个编译错误,因为你没有覆盖Object::equals.
将签名更改为:
public boolean equals(Object v) {
^^^^^^
Run Code Online (Sandbox Code Playgroud)
并修改实现,你应该得到你期望的行为.
| 归档时间: |
|
| 查看次数: |
101 次 |
| 最近记录: |