java中equals方法和hashcode方法的区别和联系,以及为什么要重写这两个方法,不重写会怎样
一、在Object类中的定义为:
public native int hashCode();
是一个本地方法,返回的对象的地址值。
但是,同样的思路,在String等封装类中对此方法进行了重写。方法调用得到一个计算公式得到的 int值。 二、在重写任何类得hashcode方法时必须遵循以下几点:
1、在Java应用的同一次执行过程中,同一对象被多次调用,则他们的hashcode值必然相同。
而对于同一个应用的两次不同的调用,它们的Hashcode值可以相同,也有可能不同。
2、对于两个对象来说,如果他们的equals方法比较返回true,那么这两个对象的hashcode必然相同。
这也解释了为什么String类中,如果两个对象的equals方法相同,则他们的hashcode值一定相同。
3、对于两个对象来说,如果使用equals方法返回为false,则他们的hashcode的值有可能相等也可能不等,
(如果不同会提高性能,因为在集合中类判断两个对象是否相等,如果其hashcode不等就直接不用判断equals方法了)
4、对于Object对象来说,不同的Object对象的hashcode是不同的,它们返回的是对象的地址,
equals返回的也是对象的地址。所以在自己定义的类中如果要添加到集合对象中,
最好是要重写hashcode和equals方法,不然会自动继承自Object类中的两个方法根据对象地址来判断。
在重写自己定义的类时,通常是在类中的根据某个值如name.hashcode();来进行判断。 三、以HashSet 为例:
当我们使用HashSet时,hashCode()方法就会被得到调用,判断已经存储在集合中的对象的hashCode值是否与所增加。
对象的hashCode值一致,如果“不一致”则直接加进去(不用比较equals()提高效率),如果一致,则进行equals方法的比较,如果返回true,表明集合里面已经有这个对象,不能添加进去了。如果是false表是集合里面没有这个对象,则可以加进去。所以在重写hashcode()或者equals() 方法的任何一个方法时,必须重写另外一个。
示例代码:
/**
* People 手工重写hashcode方法和equals方法 根据name来判断 两个对象是否相等。
*/
class People {
private String name;
public People(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
//如果是自己
if(this==obj){
return true ;
}
//如果是空
if(obj==null ){
return false;
}
//比较两个People的名字是否相同
if(obj!=null && obj instanceof People){
if(((People)obj).name.equals(this.name))
return true ;
}
return false;
}
@Override
public int hashCode() {
// String的hashcode本来就是用来比较两个字符是否相等
return name.hashCode();
}
}
从Object类的hashCode()和equals()方法讲起:
最近看了Object类的源码,对hashCode() 和equals()方法有了更深的认识。重写equals()方法就必须重写hashCode()方法的原因,从源头Object类讲起就更好理解了。
先来看Object关于hashCode()和equals()的源码:
- public native int hashCode();
- public boolean equals(Object obj) {
- return (this == obj);
- }
光从代码中我们可以知道,hashCode()方法是一个本地native方法,返回的是对象引用中存储的对象的内存地址,而equals方法是利用==来比较的也是对象的内存地址。从上边我们可以看出,hashCode方法和equals方法是一致的。还有最关键的一点,我们来看Object类中关于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();
简单的翻译一下就是,hashCode方法一般的规定是:
- 1.在 Java 应用程序执行期间,在对同一对象多次调用 hashCode 方法时,必须一致地返回相同的整数,前提是将对象进行 equals 比较时所用的信息没有被修改。从某一应用程序的一次执行到同一应用程序的另一次执行,该整数无需保持一致。
- 2.如果根据 equals(Object) 方法,两个对象是相等的,那么对这两个对象中的每个对象调用 hashCode 方法都必须生成相同的整数结果。
- 3.如果根据 equals(java.lang.Object) 方法,两个对象不相等,那么对这两个对象中的任一对象上调用 hashCode 方法不 要求一定生成不同的整数结果。但是,程序员应该意识到,为不相等的对象生成不同整数结果可以提高哈希表的性能。
再简单的翻译一下第二三点就是:hashCode()和equals()保持一致,如果equals方法返回true,那么两个对象的hasCode()返回值必须一样。如果equals方法返回false,hashcode可以不一样,但是这样不利于哈希表的性能,一般我们也不要这样做。重写equals()方法就必须重写hashCode()方法的原因也就显而易见了。
假设两个对象,重写了其equals方法,其相等条件是属性相等,就返回true。如果不重写hashcode方法,其返回的依然是两个对象的内存地址值,必然不相等。这就出现了equals方法相等,但是hashcode不相等的情况。这不符合hashcode的规则。下边,会介绍在集合框架中,这种情况会导致的严重问题。
重写的作用:
用HashSet来验证两个需都重写的必要性
class A {
String certificate;
public A(String certificate) {
this.certificate = certificate;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
A other = (A) obj;
if (certificate == null) {
if (other.certificate != null)
return false;
} else if (!certificate.equals(other.certificate))
return false;
return true;
}
}
class B {
String certificate;// 身份证号
public B(String certificate) {
this.certificate = certificate;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((certificate == null) ? 0 : certificate.hashCode());
return result;
}
}
class C {
String certificate;
public C(String certificate) {
this.certificate = certificate;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((certificate == null) ? 0 : certificate.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
C other = (C) obj;
if (certificate == null) {
if (other.certificate != null)
return false;
} else if (!certificate.equals(other.certificate))
return false;
return true;
}
}
重写hashCode()的原则
- //f是Filed属性
- boolean hashCode=(f?0:1)
- (byte,short,char,int) hashCode=(int)f
- long hashCode=(int)(f^(f>>>32))
- float hashCode=Float.floatToIntBits(f)
- double hashCode=(int)(1^(1>>>32))
- 普通引用类型 hashCode=f.hashCode()
将计算出的每个Filed的hashCode值相加返回,为了避免直接相加产生的偶然相等(单个不相等,加起来就相等了),为每个Filed乘以一个质数后再相加,例如有:
- return f1.hashCode()*17+(int)f2.13
- /**
- * 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;
- }
- public class HashSetTest {
- public static void main(String[] args) {
- HashSet hashSet = new HashSet();
- hashSet.add(new A());
- hashSet.add(new A());
- hashSet.add(new B());
- hashSet.add(new B());
- hashSet.add(new C());
- hashSet.add(new C());
- for (Object hs : hashSet) {
- System.out.println(hs);
- }
- //HashSet重写了toString()方法
- // System.out.println(hashSet);
- }
- }
其结果为:
- cn.edu.uestc.collection.B@1
- cn.edu.uestc.collection.B@1
- cn.edu.uestc.collection.C@2
- cn.edu.uestc.collection.A@3f84246a
- cn.edu.uestc.collection.A@18a9fa9c
- Process finished with exit code 0
从上边的程序结果可以看到,必须要同时重写这两个方法,要不然Set的特性就被破坏了。
java中equals方法和hashcode方法的区别和联系,以及为什么要重写这两个方法,不重写会怎样的更多相关文章
- java中equals方法和contentEquals方法区别
java中,String类里提供了两种字符串的比较方式(算上“==”应该是三种) String line1 = new String("0123456789"); String l ...
- Java中sleep方法和wait的详细区别
1.两者的区别 对于sleep()方法,我们首先要知道该方法是属于Thread类中的.而wait()方法,则是属于Object类中的. 这两个方法来自不同的类分别是Thread和Object 最主要是 ...
- equals()方法和hashCode()方法详解
equals()方法和hashCode()方法详解 1. Object类中equals()方法源代码如下所示: /** * Object类中的equals()方法 */ public boolean ...
- Java基础系列-equals方法和hashCode方法
原创文章,转载请标注出处:<Java基础系列-equals方法和hashCode方法> 概述 equals方法和hashCode方法都是有Object类定义的. publi ...
- Java 如何重写对象的 equals 方法和 hashCode 方法
前言:Java 对象如果要比较是否相等,则需要重写 equals 方法,同时重写 hashCode 方法,而且 hashCode 方法里面使用质数 31.接下来看看各种为什么. 一.需求: 对比两个对 ...
- HashSet中存方用户自己定义数据类型数据,重写equals方法和hashCode方法
import java.util.Set; import java.util.HashSet; public class SetTest { public static void main(Strin ...
- 详解equals()方法和hashCode()方法
前言 Java的基类Object提供了一些方法,其中equals()方法用于判断两个对象是否相等,hashCode()方法用于计算对象的哈希码.equals()和hashCode()都不是final方 ...
- 关于Object类的equals方法和hashCode方法
关于Object类的equals的特点,对于非空引用: 1.自反性:x.equals(x) return true : 2.对称性:x.equals(y)为true,那么y.equals(x)也为tr ...
- 详解 equals() 方法和 hashCode() 方法
创建实体类时,最好重写超类(Object)的hashCode()和equals()方法 equals()方法: 通过该实现可以看出,Object类的实现采用了区分度最高的算法,即只要两个对象不是同一个 ...
随机推荐
- 【开源项目】智能电视及电视盒子的控制应用TVRemoteIME的接口说明
一.APP项目介绍: APP名称:TVRemoteIME 功能说明:安卓智能电视或者安卓盒子的控制应用,可跨屏远程输入.远程遥控(代替遥控器)盒子.盒子应用及文件管理.HTTP/RTMP/MMS网络视 ...
- java 集合框架(十五)Deque
一.概述 Deque是Queue的子接口,我们知道Queue是一种队列形式,而Deque则是双向队列,它支持从两个端点方向检索和插入元素,因此Deque既可以支持LIFO形式也可以支持LIFO形式.D ...
- apache配置,禁止ip访问web站点
由于一台服务器上面部署了好几个应用,对应不同的域名,如果用户知道ip地址的话,直接用户ip地址访问,会显示第一个虚拟主机的页面(更改了虚拟主机的顺序,每次都是显示第一个).这样对用户造成不好的印象,所 ...
- bootrom脚本的创建
bootrom脚本的创建 以下以压缩版bootrom 为例,基于Powerpc 平台,详细介绍压缩版bootrom 的生成过程及执行流程,从而使读者对bootrom有一个彻底的了解.这对于Vx ...
- Excel 2010高级应用-条状图(五)
Excel 2010高级应用-条状图(五) 基本操作如下: 1.新建一个Excel空白文档,并命名条状图 2.单击"插入",找到条状图的样图 3.选择其中一种类型的条状图样图,在空 ...
- 使用EFI引导从硬盘(U盘)安装Win7的图文教程
目前仅支持vista后的64位系统 大部分使用EFI引导安装Win7的教程都是采用光盘启动安装,虽然光盘安装比较简单,但是对于没有光驱的朋友来说还是相当不便,更不用说光盘安装的两大缺点了,一速度慢,二 ...
- [Err] 1136 - Column count doesn't match value count at row 1
1 错误描述 [Err] 1136 - Column count doesn't match value count at row 1 Procedure execution failed 1136 ...
- Column 'id' in where clause is ambiguous
1.错误描述 org.hibernate.exception.ConstraintViolationException: error executing work at org.hibernate.e ...
- hibernate学习(一)配置,导包
框架的作用 学过javaWeb基础的已经对web层 jsp servlet ,service 层 ,dao层的jdbc .DBUtils 有了很深的了解 并编写代码实现某种功能 为了提高开发 ...
- android界面设计之布局管理
谈到android界面设计,各种布局样式不得不提!传统的布局方式有6种,我们会一一介绍. 在android studio2.2版本之后出现了一款超棒的布局方式,真正意义上的所见即所得,后面我们也会讲到 ...