重写equals和hashCode

shaluoshuangshu 2010-06-13 03:11:59
如果X.equals(Y),那么它们的X和Y的hashCode值一定相同,why? 如果重写了equals,但没有重写hashCode,两个不是也可能不同吗?初学者。

import java.util.*;
public class test2{
public static void main(String args[]){
HashSet<Demo> hSet=new HashSet<Demo>();
Demo d1=new Demo(1,"abc");
Demo d2=new Demo(1,"abc");
hSet.add(d1);
hSet.add(d2);
System.out.println(hSet.size());//输出2
System.out.println(d1.equals(d2)); //true
}
}

class Demo{
int a;
String b;
public Demo(int a,String b){
this.a=a;
this.b=b;
}
// public int hashCode(){
// return a*(b.hashCode());
// }

public boolean equals(Object o){
Demo d=(Demo)o;
return (this.a==d.a)&&(this.b.equals(d.b));
}
}

d1.equals(d2)返回ture,为什么还能加入?不是比较的equals吗?这个必须hashCode也相等,然后再判断equals()?
...全文
304 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sheh伟伟 2012-03-01
  • 打赏
  • 举报
回复
重写equals方法一定要重写hashCode方法,这个是我们老师反复强调的。虽然对于一个容器里存放比较少的对象来说,重写hashCode方法好像并不怎么有用,但是,当你的某个容器里存放了许许多多的对象时,重写hashCode方法的优势是明显的。因为当你要比较两个对象时,首先会调用hashCode方法,当两个对象的哈希码不同时,不用再调用equals方法也能肯定这两个对象不同,也就是说不用把你拿到的那个对象和你容器里的每个对象都equals一遍,当两个对象的哈希码相同时才会调用equals方法,而这时这个哈希桶里的对象可能只有那么几个,只需要equals那么几下就能很快的得出结果。这样能大大加快你的检索速度,提高你程序的效率。
l6801567 2010-06-13
  • 打赏
  • 举报
回复
hashtables
l6801567 2010-06-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 izard999 的回复:]

hashCode在不运用在hash算法的集合下面是不需要重写的.. 这个无所谓.!
但是重写equals的时候顺带重写下hashCode没什么不好, 如果我没记错的话, 规范上面应该有如果两个对象相等,则他们必须要有相同的hashCode, 不知道记错没
[/Quote]
上面说的没错,这个方法就是为了支持hashTable(supported for the benefit of hashtables )
l6801567 2010-06-13
  • 打赏
  • 举报
回复
jdk1.5 Object的hashCode()方法原型

/**
* Returns a hash code value for the object. This method is
* supported for the benefit of hashtables such as those provided by
* <code>java.util.Hashtable</code>.
* <p>
* The general contract of <code>hashCode</code> is:
* <ul>
* <li>Whenever it is invoked on the same object more than once during
* an execution of a Java application, the <tt>hashCode</tt> method
* must consistently return the same integer, provided no information
* used in <tt>equals</tt> comparisons on the object is modified.
* This integer need not remain consistent from one execution of an
* application to another execution of the same application.
* <li>If two objects are equal according to the <tt>equals(Object)</tt>
* method, then calling the <code>hashCode</code> method on each of
* the two objects must produce the same integer result.
* <li>It is <em>not</em> required that if two objects are unequal
* according to the {@link java.lang.Object#equals(java.lang.Object)}
* method, then calling the <tt>hashCode</tt> method on each of the
* two objects must produce distinct integer results. However, the
* programmer should be aware that producing distinct integer results
* for unequal objects may improve the performance of hashtables.
* </ul>
* <p>
* As much as is reasonably practical, the hashCode method defined by
* class <tt>Object</tt> does return distinct integers for distinct
* objects. (This is typically implemented by converting the internal
* address of the object into an integer, but this implementation
* technique is not required by the
* Java<font size="-2"><sup>TM</sup></font> programming language.)
*
* @return a hash code value for this object.
* @see java.lang.Object#equals(java.lang.Object)
* @see java.util.Hashtable
*/
public native int hashCode();

上面的每个li都是一个重要的规则(由实现者实现)
1.结果(返回值)必须一致(对象属性改变后)
2.如果两个对象相等,他们hashCode比等
3.如果两个对象不等,他们的hashCode未必一定不同(程序要区分,最好不等,hashCode也不等)
下面还有个什么典型实现举例,不是必须的
dracularking 2010-06-13
  • 打赏
  • 举报
回复
楼上都提到了

如果使用了equals来判断两个对象是否相等,那么条件是:equals为true且它们hashcode值也相等

反之判断不等equals为false之后,不强制hashcode值须如何,虽然Object的hashCode方法的默认实现是不是同一个对象就返回不同整数(因为是根据对象的内部地址)
izard999 2010-06-13
  • 打赏
  • 举报
回复
hashCode在不运用在hash算法的集合下面是不需要重写的.. 这个无所谓.!
但是重写equals的时候顺带重写下hashCode没什么不好, 如果我没记错的话, 规范上面应该有如果两个对象相等,则他们必须要有相同的hashCode, 不知道记错没
圣诞老人123 2010-06-13
  • 打赏
  • 举报
回复
重写equals 只有在使用equals操作符时菜体现用处 你可以不从写hashCode 但是如果你要把这个对象放到hashmap等散列集合中为了保持不冲突 所以你要重写hashCode方法
Headsen 2010-06-13
  • 打赏
  • 举报
回复
set存储的是不重复的值,即obj1.equals(obj2)==true话,则只存一份。
虽然上面相等时,能够加入,但是并未真正加入,加入后set.size()不便的
比如加入N个相等的值,size()仍为1.

至于使hashCode()和equals尽可能一致是性能考虑。
Headsen 2010-06-13
  • 打赏
  • 举报
回复
hashCode 的常规协定是:
如果根据 equals(Object) 方法,两个对象是相等的,那么在两个对象中的每个对象上调用 hashCode 方法都必须生成相同的整数结果。
「已注销」 2010-06-13
  • 打赏
  • 举报
回复
参考Java文档
public boolean equals(Object obj)
当此方法被重写时,通常有必要重写 hashCode 方法,以维护 hashCode 方法的常规协定,该协定声明相等对象必须具有相等的哈希码。

只是个约定而已。你算里 X.equals(X) 返回 false都是可以的,只是不符合一般人的正常思维而已

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧