如何计算java中对象的哈希码? [重复]

时间:2022-07-27 16:47:16

Possible Duplicate:
How is hashCode() calculated in Java

可能重复:hashCode()是如何用Java计算的

I know every objects generated in java has hashcodes and also there can be two different objects with the same hashcode.So, i dont know hashcode generation depends on the object type or something else...

我知道java中生成的每个对象都有哈希码,并且可能有两个不同的对象具有相同的哈希码。所以,我不知道哈希码的生成取决于对象类型或其他东西......

Can you suggest me about how hashcodes are implemented in java or what does it depends(based) on?

你能告诉我一下如何在java中实现哈希码或者它依赖于什么(基于)?

2 个解决方案

#1


2  

hashcodes are usualy implemented for each object and are calculated using the fields that make that object unique and to comply with the hashcode equals contract. If left unimplemented the hashcode of the super class will be used.

通常为每个对象实现哈希码,并使用使该对象唯一且符合哈希码等于契约的字段来计算。如果未实现,将使用超类的哈希码。

The "default implementation" will be objects hashcode which is calculated using it's memory address also known as pointer.

“默认实现”将是对象哈希码,它使用它的内存地址(也称为指针)计算。

#2


1  

there's a lot of resources about this Try

这个尝试有很多资源

#1


2  

hashcodes are usualy implemented for each object and are calculated using the fields that make that object unique and to comply with the hashcode equals contract. If left unimplemented the hashcode of the super class will be used.

通常为每个对象实现哈希码,并使用使该对象唯一且符合哈希码等于契约的字段来计算。如果未实现,将使用超类的哈希码。

The "default implementation" will be objects hashcode which is calculated using it's memory address also known as pointer.

“默认实现”将是对象哈希码,它使用它的内存地址(也称为指针)计算。

#2


1  

there's a lot of resources about this Try

这个尝试有很多资源