Object之hashCode

来源:互联网 发布:分析化学酸碱滴定数据 编辑:程序博客网 时间:2024/06/05 04:38

The original website:http://cjava.iteye.com/blog/441834

public int hashCode()返回该对象的哈希码值。支持该方法是为哈希表提供一些优点,例如,java.util.Hashtable 提供的哈希表。
hashCode 的常规协定是:

在 Java 应用程序执行期间,在同一对象上多次调用 hashCode 方法时,必须一致地返回相同的整数,前提是对象上 equals 比较中所用的信息没有
被修改。从某一应用程序的一次执行到同一应用程序的另一次执行,该整数无需保持一致。
如果根据 equals(Object) 方法,两个对象是相等的,那么在两个对象中的每个对象上调用 hashCode 方法都必须生成相同的整数结果。
以下情况不 是必需的:如果根据 equals(java.lang.Object) 方法,两个对象不相等,那么在两个对象中的任一对象上调用 hashCode 方法必定会生
成不同的整数结果。但是,程序员应该知道,为不相等的对象生成不同整数结果可以提高哈希表的性能。
实际上,由 Object 类定义的 hashCode 方法确实会针对不同的对象返回不同的整数。(这一般是通过将该对象的内部地址转换成一个整数来实现的
,但是 JavaTM 编程语言不需要这种实现技巧。)


返回:
此对象的一个哈希码值。
另请参见:
equals(java.lang.Object), Hashtable

其实这是一个本地方法,并不是用java实现的,这里的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();
   
   很多方法都继承Object,其实这类中的toString()是我们经常用到,其实看源码可以他就是得到自己的类名及
   hashCode的16进制型式。
     /**
     * Returns a string representation of the object. In general, the
     * <code>toString</code> method returns a string that
     * "textually represents" this object. The result should
     * be a concise but informative representation that is easy for a
     * person to read.
     * It is recommended that all subclasses override this method.
     * <p>
     * The <code>toString</code> method for class <code>Object</code>
     * returns a string consisting of the name of the class of which the
     * object is an instance, the at-sign character `<code>@</code>', and
     * the unsigned hexadecimal representation of the hash code of the
     * object. In other words, this method returns a string equal to the
     * value of:
     * <blockquote>
     * <pre>
     * getClass().getName() + '@' + Integer.toHexString(hashCode())
     * </pre></blockquote>
     *
     * @return  a string representation of the object.
     */
    public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
 
 
The five floor'answer:
 
java.lang.Object的约定如下:
1,在程序的同一执行期间,如果equals方法的信息不给修改(前提),那么对同一对象多次调用hashcode得出的值也是必须一样。但是不要求在不同的执行期间保持一致
2,如果两个对象的equals()方法等同,那么两对象任何一个调用hashCode()方法,必须产生同样的结果。
3,如果两对象的equals()方法不相等,那么对两对象的任意对象调用hashCode()方法,不要求产生两个不同的整数结果

hashcode   就仿佛是一个Object的ID,如果说它有什么特别,那就是它是一个int类型的值。在所有对象的运算中,int是最快(当然最快的是char,但是它能表达的信息太有限了)。一个简单的例子是Hashtable.contain(Object)方法,它首先是根据Object的散列值去判断,只有当出现多个相同的散列值的时候才会调用equals(),因为判断int比判断Object.equals()要快很多,这是一个提高效率的策略问题。同样也说明为什么不同的对象(equals()得false)具有不同的hashcode(也就是a.equal(b)==true   <=>   a.hashcode()==b.hashcode())具有更高效率的理由——不需要进行额外的equals()操作。

 

原创粉丝点击