Java equals 方法与hashcode 方法的深入解析

 更新时间:2013年07月09日 18:37:16   作者:  
面试时经常会问起字符串比较相关的问题,比如:字符串比较时用的什么方法,内部实现如何?hashcode的作用,以及重写equal方法,为什么要重写hashcode方法?以下就为大家解答,需要的朋友可以参考下

PS:本文使用jdk1.7
解析
1.Object类 的equals 方法

复制代码 代码如下:

   /**
     * Indicates whether some other object is "equal to" this one.
     * <p>
     * The {@code equals} method implements an equivalence relation
     * on non-null object references:
     * <ul>
     * <li>It is <i>reflexive</i>: for any non-null reference value
     *     {@code x}, {@code x.equals(x)} should return
     *     {@code true}.
     * <li>It is <i>symmetric</i>: for any non-null reference values
     *     {@code x} and {@code y}, {@code x.equals(y)}
     *     should return {@code true} if and only if
     *     {@code y.equals(x)} returns {@code true}.
     * <li>It is <i>transitive</i>: for any non-null reference values
     *     {@code x}, {@code y}, and {@code z}, if
     *     {@code x.equals(y)} returns {@code true} and
     *     {@code y.equals(z)} returns {@code true}, then
     *     {@code x.equals(z)} should return {@code true}.
     * <li>It is <i>consistent</i>: for any non-null reference values
     *     {@code x} and {@code y}, multiple invocations of
     *     {@code x.equals(y)} consistently return {@code true}
     *     or consistently return {@code false}, provided no
     *     information used in {@code equals} comparisons on the
     *     objects is modified.
     * <li>For any non-null reference value {@code x},
     *     {@code x.equals(null)} should return {@code false}.
     * </ul>
     * <p>
     * The {@code equals} method for class {@code Object} implements
     * the most discriminating possible equivalence relation on objects;
     * that is, for any non-null reference values {@code x} and
     * {@code y}, this method returns {@code true} if and only
     * if {@code x} and {@code y} refer to the same object
     * ({@code x == y} has the value {@code true}).
     * <p>
     * Note that it is generally necessary to override the {@code hashCode}
     * method whenever this method is overridden, so as to maintain the
     * general contract for the {@code hashCode} method, which states
     * that equal objects must have equal hash codes.
     *
     * @param   obj   the reference object with which to compare.
     * @return  {@code true} if this object is the same as the obj
     *          argument; {@code false} otherwise.
     * @see     #hashCode()
     * @see     java.util.HashMap
     */
    public boolean equals(Object obj) {
        return (this == obj);
    }

看代码,Object的equals方法,采用== 进行比较,只是比较对象的引用,如果引用的对象相同,那么就返回true.
看注释,Object的equals方法,具有如下特性
1.reflexive-自反性 
 x.equals(x)  return true
2.symmetric-对称性
x.equals(y)  return true
y.equals(x)  return true
3.transitive-传递性
x.equals(y)  return true
y.equals(z)  return true
x.equals(z)  return true
4.consistent-一致性
x.equals(y)  return true //那么不管调用多少次,肯定都是返回true
5.与null的比较
x.equals(null) return false //对于none-null的x对象,每次必然返回false
6.于hashcode的关系
     * Note that it is generally necessary to override the {@code hashCode}
     * method whenever this method is overridden, so as to maintain the
     * general contract for the {@code hashCode} method, which states
     * that equal objects must have equal hash codes.
需要注意的是,一般来说,如果重写了equals方法,都必须要重写hashcode方法,
来确保具有相同引用的对象,能够具有同样的hashcode值
好了,看到这里,我们就明白了,为什么重写了equals方法,一般来说就需要重写hashcode方法,
虽然这个不是强制性的,但是如果不能保证相同的引用对象,没有相同的hashcode,会对系统留下很大隐患
2.String类的equals方法
复制代码 代码如下:

   /**
     * Compares this string to the specified object.  The result is {@code
     * true} if and only if the argument is not {@code null} and is a {@code
     * String} object that represents the same sequence of characters as this
     * object.
     *
     * @param  anObject
     *         The object to compare this {@code String} against
     *
     * @return  {@code true} if the given object represents a {@code String}
     *          equivalent to this string, {@code false} otherwise
     *
     * @see  #compareTo(String)
     * @see  #equalsIgnoreCase(String)
     */
    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String) anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                            return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

看源码,我们可以发现,这个比较分为两部分
1.先比较是否引用同一对象
2.如果引用对象不同,是否两个String的content相同
3,String 类的hashcode 方法
复制代码 代码如下:

    /**
     * Returns a hash code for this string. The hash code for a
     * <code>String</code> object is computed as
     * <blockquote><pre>
     * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
     * </pre></blockquote>
     * using <code>int</code> arithmetic, where <code>s[i]</code> is the
     * <i>i</i>th character of the string, <code>n</code> is the length of
     * the string, and <code>^</code> indicates exponentiation.
     * (The hash value of the empty string is zero.)
     *
     * @return  a hash code value for this object.
     */
    public int hashCode() {
        int h = hash;
        if (h == 0 && value.length > 0) {
            char val[] = value;
            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            hash = h;
        }
        return h;
    }

可以看到hashcode的计算公式为:s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
因此,对于同一个String,得出的hashcode必然是一致的
另外,对于空的字符串,hashcode的值是0

小结
至此,我们可以对本文开头的疑问做一个小结.
1.字符串比较时用的什么方法,内部实现如何?
使用equals方法,先比较引用是否相同,后比较内容是否一致.

2.hashcode的作用,以及重写equal方法,为什么要重写hashcode方法?
hashcode是系统用来快速检索对象而使用,equals方法是用来判断引用的对象是否一致,所以,当引用对象一致时,必须要确保其hashcode也一致,因此需要重写hashcode方法来确保这个一致性

相关文章

  • 详解Java实现多线程的三种方式

    详解Java实现多线程的三种方式

    这篇文章主要为大家详细介绍了Java实现多线程的三种方式,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • Java多线程之同步工具类CyclicBarrier

    Java多线程之同步工具类CyclicBarrier

    这篇文章主要介绍Java多线程之同步工具类CyclicBarrier,它是一个同步工具类,它允许一组线程互相等待,直到达到某个公共屏障点,支持一个可选的Runnable命令,在一组线程中的最后一个线程到达之后,该命令只在每个屏障点运行一次。下面来看文章具体内容
    2021-10-10
  • java8升级到java17的兼容性分析与迁移指南

    java8升级到java17的兼容性分析与迁移指南

    这篇文章主要为大家详细介绍了从 Java 8 升级到 Java 17 的详细分析和迁移步骤,包括代码修改建议,依赖更新和配置调整,有需要的小伙伴可以参考一下
    2025-04-04
  • Java List按照某字段去重的使用示例

    Java List按照某字段去重的使用示例

    在Java开发中,我们经常会面临对List中对象属性去重的需求,本文主要介绍了Java List按照某字段去重的使用示例,具有一定的参考价值,感兴趣的可以了解一下
    2023-12-12
  • Java代码编译和反编译的那些事儿

    Java代码编译和反编译的那些事儿

    这篇文章主要给大家介绍了关于Java代码编译和反编译的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-05-05
  • 在IDEA中如何设置最多显示文件标签个数

    在IDEA中如何设置最多显示文件标签个数

    在使用IDEA进行编程时,可能会同时打开多个文件,当文件过多时,文件标签会占据大部分的IDEA界面,影响我们的编程效率,因此,我们可以通过设置IDEA的文件标签显示个数,来优化我们的编程环境,具体的设置方法如下
    2024-10-10
  • SpringBoot如何优雅地使用Swagger2

    SpringBoot如何优雅地使用Swagger2

    这篇文章主要介绍了SpringBoot如何优雅地使用Swagger2,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • SpringCloud链路追踪组件Sleuth配置方法解析

    SpringCloud链路追踪组件Sleuth配置方法解析

    这篇文章主要介绍了SpringCloud链路追踪组件Sleuth配置方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • SpringBoot基于Redis实现生成全局唯一ID的方法

    SpringBoot基于Redis实现生成全局唯一ID的方法

    在项目中生成全局唯一ID有很多好处,生成全局唯一ID有助于提高系统的可用性、数据的完整性和安全性,同时也方便数据的管理和分析,所以本文给大家介绍了SpringBoot基于Redis实现生成全局唯一ID的方法,文中有详细的代码讲解,需要的朋友可以参考下
    2023-12-12
  • spring aop之链式调用的实现

    spring aop之链式调用的实现

    这篇文章主要介绍了spring aop之链式调用的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-02-02

最新评论