Java中 hashCode()方法详解

先来看下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();  

根据上面的注释我们可以看到,在调用equals方法的时候会调用hashcode(),也就是我们重写equals()的时候也要重写hashCode()...

我们在来看下hashTable的实现:

    /**
     * Returns the hash code value for this Map as per the definition in the
     * Map interface.
     *
     * @see Map#hashCode()
     * @since 1.2
     */
   // 计算Hashtable的哈希值

    public synchronized int hashCode() {
        /*
         * This code detects the recursion caused by computing the hash code
         * of a self-referential hash table and prevents the stack overflow
         * that would otherwise result.  This allows certain 1.1-era
         * applets with self-referential hash tables to work.  This code
         * abuses the loadFactor field to do double-duty as a hashCode
         * in progress flag, so as not to worsen the space performance.
         * A negative load factor indicates that hash code computation is
         * in progress.
         */
        int h = 0;
        if (count == 0 || loadFactor < 0)
            return h;  // Returns zero

        loadFactor = -loadFactor;  // Mark hashCode computation in progress
        Entry[] tab = table;
        for (int i = 0; i < tab.length; i++)
            for (Entry e = tab[i]; e != null; e = e.next)
                h += e.key.hashCode() ^ e.value.hashCode();   //否则,返回“Hashtable中的每个Entry的key和value的异或值 的总和”。
        loadFactor = -loadFactor;  // Mark hashCode computation complete

    return h;
    }

 

如果根据 equals(Object) 方法,两个对象是相等的,那么对这两个对象中的每个对象调用 hashCode 方法都必须生成相同的整数结果,注:这里说的equals(Object) 方法是指Object类中未被子类重写过的equals方法。
如果两个hashCode()返回的结果相等,则两个对象的equals方法不一定相等。

 

转载于:https://www.cnblogs.com/xiaoblog/p/4201337.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavahashCode()是Object类的一个方法,返回对象的哈希码。哈希码是由对象的存储地址或者其他信息计算得到的一个int类型的整数。 在Java,哈希码经常被用来实现数据结构的散列表。在散列表,每个对象都有一个对应的哈希码,通过哈希码可以快速定位到对象存储的位置,从而实现快速的查找、插入和删除等操作。 JavahashCode()方法的默认实现是返回对象的存储地址的一个int类型的整数表示,即对象的内存地址。但是,不同的JVM实现可能会有不同的实现方式。 在实际开发,我们可以根据对象的属性计算哈希码,以实现更好的散列表性能。具体来说,我们需要注意以下几点: 1. hashCode()方法的返回值应该尽可能地分散,避免不同的对象产生相同的哈希码。这可以通过使用对象的不同属性进行计算来实现。 2. 如果两个对象的equals()方法返回true,那么它们的hashCode()方法应该返回相同的值。这可以确保这两个对象在散列表的位置是相同的。 3. hashCode()方法的返回值应该在对象的生命周期保持不变。如果对象的属性发生变化,那么它的哈希码也应该发生变化。 总之,hashCode()方法Java是非常重要的一个方法,我们可以根据对象的属性计算哈希码,以实现更好的散列表性能。同时,我们需要注意hashCode()方法的实现要尽可能地分散,避免不同的对象产生相同的哈希码,同时也要保证hashCode()方法的返回值在对象的生命周期保持不变。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值