本帖最后由 shaluoshuangshu 于 2010-06-13 03:44:48 编辑

解决方案 »

  1.   

    参考Java文档
    public boolean equals(Object obj)
    当此方法被重写时,通常有必要重写 hashCode 方法,以维护 hashCode 方法的常规协定,该协定声明相等对象必须具有相等的哈希码。 只是个约定而已。你算里 X.equals(X) 返回 false都是可以的,只是不符合一般人的正常思维而已
      

  2.   

    hashCode 的常规协定是:
    如果根据 equals(Object) 方法,两个对象是相等的,那么在两个对象中的每个对象上调用 hashCode 方法都必须生成相同的整数结果。
      

  3.   

    set存储的是不重复的值,即obj1.equals(obj2)==true话,则只存一份。
    虽然上面相等时,能够加入,但是并未真正加入,加入后set.size()不便的
    比如加入N个相等的值,size()仍为1.至于使hashCode()和equals尽可能一致是性能考虑。
      

  4.   

    重写equals 只有在使用equals操作符时菜体现用处 你可以不从写hashCode 但是如果你要把这个对象放到hashmap等散列集合中为了保持不冲突 所以你要重写hashCode方法
      

  5.   

    hashCode在不运用在hash算法的集合下面是不需要重写的.. 这个无所谓.!
    但是重写equals的时候顺带重写下hashCode没什么不好,  如果我没记错的话, 规范上面应该有如果两个对象相等,则他们必须要有相同的hashCode, 不知道记错没
      

  6.   

    楼上都提到了如果使用了equals来判断两个对象是否相等,那么条件是:equals为true且它们hashcode值也相等反之判断不等equals为false之后,不强制hashcode值须如何,虽然Object的hashCode方法的默认实现是不是同一个对象就返回不同整数(因为是根据对象的内部地址)
      

  7.   

    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也不等)
    下面还有个什么典型实现举例,不是必须的
      

  8.   

    上面说的没错,这个方法就是为了支持hashTable(supported for the benefit of hashtables )
      

  9.   

    重写equals方法一定要重写hashCode方法,这个是我们老师反复强调的。虽然对于一个容器里存放比较少的对象来说,重写hashCode方法好像并不怎么有用,但是,当你的某个容器里存放了许许多多的对象时,重写hashCode方法的优势是明显的。因为当你要比较两个对象时,首先会调用hashCode方法,当两个对象的哈希码不同时,不用再调用equals方法也能肯定这两个对象不同,也就是说不用把你拿到的那个对象和你容器里的每个对象都equals一遍,当两个对象的哈希码相同时才会调用equals方法,而这时这个哈希桶里的对象可能只有那么几个,只需要equals那么几下就能很快的得出结果。这样能大大加快你的检索速度,提高你程序的效率。