可能重复:
Hashcode 和 equals
我读过很多关于hashcode和平等及其关系的文章。到目前为止,我的理解是默认情况下每个object都有平等和hashcode功能。因为 javaclass有这些功能。现在hashcode意味着它返回一个对象的内存地址。hashcodes默认情况下是唯一的。创建对象时,您将获得唯一的代码。
- 现在我的问题是,当您按照规则
override使用equals函数时,我们需要覆盖hashcode函数..? - 那么是不是我们
hashcode与equals方法一起实现了该功能,但hascode实现没有用..? 以及如何在
hashcode框架中使用.. ?hashmaphashtablecollectionclass person{ string name; string employer; boolean equals(Object o){ person per=(person)o; if(per.employer==this.employeer){ return true } return false; } int hashcode(){ return 0;//what ever i do in hashcode does it really effect any thing..as the equals does //the comparison for me and gave me the result } }
以及它们如何hascodes用于hashmap和hashtable
谢谢,