equals()方法和hashCode()方法详解
equals()
方法和hashCode()
方法详解
1. Object
类中equals()
方法源代码如下所示:
/**
* Object类中的equals()方法
*/
public boolean equals(Object obj) {
return (this == obj);
}
由以上源代码知,
Object
类中的equals()
方法是直接使用==
运算符来判断两个对象相等的。
- 引用类型变量使用
==
时,比较的是引用类型变量指向的对象的内存地址- 基本类型使用
==
时,比较值
Objcect
类中的hashCode
源代码如下:
/**
* Returns a hash code value for the object. This method is
* supported for the benefit of hash tables such as those provided by
* {@link java.util.HashMap}.
* <p>
* The general contract of {@code hashCode} is:
* <ul>
* <li>Whenever it is invoked on the same object more than once during
* an execution of a Java application, the {@code hashCode} method
* must consistently return the same integer, provided no information
* used in {@code equals} 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 {@code equals(Object)}
* method, then calling the {@code hashCode} 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 {@code hashCode} 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 hash tables.
* </ul>
* <p>
* As much as is reasonably practical, the hashCode method defined by
* class {@code Object} 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™ programming language.)
*
* @return a hash code value for this object.
* @see java.lang.Object#equals(java.lang.Object)
* @see java.lang.System#identityHashCode
*/
public native int hashCode();// java8中的hashCode方法,
上面的注释中有说明如下几点:
- 对象的
hashCode
值通常是根据对象的内存地址计算得来- 两个对象
equals()
结果为true
时,两个对象的hashCode
值一定相等,不同对象的hashCode
不等native
标识此方法不是java
语言实现
hashcode的用处:
hashCode()在哈希表中起作用,如HashSet、HashMap等
Object
类中的toString()
方法源代码如下:
public String toString() {
// 从这里就能看出打印对象时不重写toString()方法时,就会打印出对象的hashCode值
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
2. String
类中equals()
方法和hashCode()
方法
String
类中部分源代码如下所示:
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
/**
* 无参构造方法
*/
public String() {
this.value = "".value;
}
/**
* 有参构造方法
*/
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
/**
*String类重写的equals方法
*/
public boolean equals(Object anObject) {
if (this == anObject) {// 此处的this指向a.equals(b)的a对象,即谁调用指向谁
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;
}
/**
* Returns a hash code for this string. The hash code for a
* {@code String} object is computed as
* <blockquote><pre>
* s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
* </pre></blockquote>
* using {@code int} arithmetic, where {@code s[i]} is the
* <i>i</i>th character of the string, {@code n} is the length of
* the string, and {@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;
}
从上面的源码中,我们不难发现String
类已经重写了equals()
方法和hashCode()
方法。
String
类重写的equals()
方法判断流程如下:
- 使用
==
来判断两个对象的内存地址是否相同,相同返回true;
- 如果两个对象的内存地址不同,程序继续往下走,判断另一个对象是否是
String
类型的;- 如果比较对象不是
String
类型,直接返回false
;- 如果是
String
类型的,进行类型强转;- 比较两个
String
的字符数组长度,如果长度不同,返回false
;- 利用
while
循环来逐位比较字符是否相等,直到循环结束,所有字符都相等,则返回true
,否则返回false
;
下面来看一下重写的hashCode()
方法。
- 首先
String
类中定义了一个int
类型的变量hash
用来缓存String
对象的hash
值;- 如果当前调用
hashCode()
方法的String
对象在常量池没有找到,并且该对象的length
长度大于0
,则继续往下走,否则返回0
;即String
类默认""
字符串的hashCode()
值为0
;- 遍历字符数组,获取每一个字符的
ASCII
码表对应的值 和之前的hash
值相加,这样就保证了相同的字符串的hashCode()
返回值相同,计算公式在注释里已经写出来了:s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
- 将计算出来的结果保存到
hash
变量中,并返回该值;
这里为什么要乘以31
呢?原因是为了性能,不仅仅指降低了计算速度,也降低了哈希冲突的概率。
哈希冲突:此处指不同的字符串生成了相同的
hashCode
值。
31
是一个奇素数。如果乘数是偶数,并且乘法溢出的话,信息就会丢失,因为与2相乘等价于移位运算(低位补0
)。使用素数的好处并不很明显,但是习惯上使用素数来计算散列结果。 31
有个很好的性能,即用移位和减法来代替乘法,可以得到更好的性能: 31 * i == (i << 5)- i
, 现代的 VM
可以自动完成这种优化。这个公式可以很简单的推导出来。 ---- 《Effective Java
》
素数:质数又称素数,指在一个大于1的自然数中,除了1和此整数自身外,没法被其他自然数整除的数。
3. 总结
一般会有如下的约定:
- equals()结果为true的两个对象,他们的hashCode()结果要相同
- hashCode()相等的两个对象,他们的equals()结果可以为false
也就是说,equals和hashcode方法是充分不必要的关系。
所以,又有:
- 重写equals方法,必须重写hashcode方法
- 重写hashcode方法,可以不重写equals方法
一般建议都重写。
equals()方法和hashCode()方法详解的更多相关文章
- 详解equals()方法和hashCode()方法
前言 Java的基类Object提供了一些方法,其中equals()方法用于判断两个对象是否相等,hashCode()方法用于计算对象的哈希码.equals()和hashCode()都不是final方 ...
- 详解 equals() 方法和 hashCode() 方法
创建实体类时,最好重写超类(Object)的hashCode()和equals()方法 equals()方法: 通过该实现可以看出,Object类的实现采用了区分度最高的算法,即只要两个对象不是同一个 ...
- Java基础系列-equals方法和hashCode方法
原创文章,转载请标注出处:<Java基础系列-equals方法和hashCode方法> 概述 equals方法和hashCode方法都是有Object类定义的. publi ...
- Java 如何重写对象的 equals 方法和 hashCode 方法
前言:Java 对象如果要比较是否相等,则需要重写 equals 方法,同时重写 hashCode 方法,而且 hashCode 方法里面使用质数 31.接下来看看各种为什么. 一.需求: 对比两个对 ...
- 关于Object类的equals方法和hashCode方法
关于Object类的equals的特点,对于非空引用: 1.自反性:x.equals(x) return true : 2.对称性:x.equals(y)为true,那么y.equals(x)也为tr ...
- HashSet中存方用户自己定义数据类型数据,重写equals方法和hashCode方法
import java.util.Set; import java.util.HashSet; public class SetTest { public static void main(Strin ...
- ThinkPHP 中M方法和D方法详解----转载
转载的地址,http://blog.163.com/litianyichuanqi@126/blog/static/115979441201223043452383/ 自己学到这里的时候,不能清除的分 ...
- java中equals方法和hashcode方法的区别和联系,以及为什么要重写这两个方法,不重写会怎样
一.在Object类中的定义为:public native int hashCode();是一个本地方法,返回的对象的地址值.但是,同样的思路,在String等封装类中对此方法进行了重写.方法调用得到 ...
- Hibernate中为什么要重写equals方法和hashcode方法
1.*为什么要重写equals方法,首先我们来看一下equals源码: public boolean equals(Object anObject) { if (this == anObject) { ...
随机推荐
- Spring事务管理(编码式、配置文件方式、注解方式)
1.事务(https://www.cnblogs.com/zhai1997/p/11710082.html) (1)事务的特性:acdi (2)事务的并发问题:丢失修改,脏读,不可重复读 (3)事务的 ...
- Windows无法安装到GPT格式磁盘的根本解决办法 - 初学者系列 - 学习者系列文章
上次在MSDN系统QQ群里有朋友问到在安装操作系统的时候有个问题:Windows无法安装到GPT格式磁盘,见图: 我在这里说下,使用网上方法的都是小白,就是说网上那些都是小白.下面介绍如何正确安装操作 ...
- Python练习题 038:Project Euler 010:两百万以内所有素数之和
本题来自 Project Euler 第10题:https://projecteuler.net/problem=10 # Project Euler: Problem 10: Summation o ...
- 【Linux】一些文件限制配置
linux资源限制配置文件是/etc/security/limits.conf:限制用户进程的数量对于linux系统的稳定性非常重要. limits.conf文件限制着用户可以使用的最大文件数,最大线 ...
- 074 01 Android 零基础入门 01 Java基础语法 09 综合案例-数组移位 06 综合案例-数组移位-主方法功能3的实现
074 01 Android 零基础入门 01 Java基础语法 09 综合案例-数组移位 06 综合案例-数组移位-主方法功能3的实现 本文知识点:综合案例-数组移位-主方法功能3的实现 说明:因为 ...
- 055 01 Android 零基础入门 01 Java基础语法 06 Java一维数组 02 数组的概念
055 01 Android 零基础入门 01 Java基础语法 06 Java一维数组 02 数组的概念 本文知识点:数组的概念 数组的声明创建.初始化 在学习数组的声明创建.初始化前,我们可以和之 ...
- Apache HttpClient 4.5 在Springboot中使用
ConnectionRequestTimeout httpclient使用连接池来管理连接,这个时间就是从连接池获取连接的超时时间,可以想象下数据库连接池 ConnectTimeout 连接建立时间, ...
- 【Excel技巧】用IF函数进行等级评定
如果下面给出一份"2月份语文成绩考核表",那么如何对成绩进行等级评定呢. 等级评定规则: 总分(100分) A级(91-100) B级(81-90) C级(71-80) D级(70 ...
- spring redis 配置
- 第3天 | 12天搞定Python,用VSCode编写代码
Visual Studio Code (简称 VS Code), 是一款免费并且开源的现代化轻量级代码编辑器,支持语法高亮.智能代码补全.自定义热键.括号匹配.代码片段等特性,并针对网页开发做了优化. ...