首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

重写equals和hashCode解决思路

2012-03-21 
重写equals和hashCode如果X.equals(Y),那么它们的X和Y的hashCode值一定相同,why? 如果重写了equals,但没有

重写equals和hashCode
如果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()?

[解决办法]
参考Java文档
public boolean equals(Object obj)
当此方法被重写时,通常有必要重写 hashCode 方法,以维护 hashCode 方法的常规协定,该协定声明相等对象必须具有相等的哈希码。 

只是个约定而已。你算里 X.equals(X) 返回 false都是可以的,只是不符合一般人的正常思维而已
[解决办法]
set存储的是不重复的值,即obj1.equals(obj2)==true话,则只存一份。
虽然上面相等时,能够加入,但是并未真正加入,加入后set.size()不便的
比如加入N个相等的值,size()仍为1.

至于使hashCode()和equals尽可能一致是性能考虑。
[解决办法]
重写equals 只有在使用equals操作符时菜体现用处 你可以不从写hashCode 但是如果你要把这个对象放到hashmap等散列集合中为了保持不冲突 所以你要重写hashCode方法
[解决办法]
hashCode在不运用在hash算法的集合下面是不需要重写的.. 这个无所谓.!
但是重写equals的时候顺带重写下hashCode没什么不好, 如果我没记错的话, 规范上面应该有如果两个对象相等,则他们必须要有相同的hashCode, 不知道记错没
[解决办法]
楼上都提到了

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

反之判断不等equals为false之后,不强制hashcode值须如何,虽然Object的hashCode方法的默认实现是不是同一个对象就返回不同整数(因为是根据对象的内部地址)
[解决办法]
jdk1.5 Object的hashCode()方法原型

Java code
    /**     * 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(); 

热点排行