即使您进行了覆盖,在将对象放入哈希映射作为键之前计算哈希时,它仍然会引用Object类中的hashcode方法并给出一些int值。
我已经覆盖了一个方法,该方法将为所有对象返回相同的值以测试hashcode,但每次它都从Object类的hashcode方法中获取值,该方法是用本地语言实现的。
如果它没有执行用户定义类中覆盖的hashcode方法,那么实现hashcode的目的是什么?
理想情况下,所有产品对象的哈希码都应为1,但我得到的是像952115109540724846...这样的数字。为什么会这样?
我已经覆盖了一个方法,该方法将为所有对象返回相同的值以测试hashcode,但每次它都从Object类的hashcode方法中获取值,该方法是用本地语言实现的。
如果它没有执行用户定义类中覆盖的hashcode方法,那么实现hashcode的目的是什么?
public class Product {
public String name ;
public int id;
public int hashcode(){ int result = 1; return result; }
}
理想情况下,所有产品对象的哈希码都应为1,但我得到的是像952115109540724846...这样的数字。为什么会这样?
hashCode()
或者hashcode()
方法? - Andreas Fester