Why we should overwrite the hashCode() when we overwrite the equals()
Preface
Though I have used Java programme language for almost a year, I'm not familiar with a notion 'hash'. I have gotten a little knowledge about hash recently because I am reading a book of algorithm, which introduce the hash function and so on. So I follow the example in the book to write a sample hash routine. During the process, I'm baffling what is hashCode of the Java. As a consequence, I turn to the omnipotent internet. I will share my acquisition. In the following Articles, I will summarize some knowledge of hashCode in the java. In this article, I will concenterate on the topic why we need overwrite the hashCode() when we overwrite the equals()
What is the feature of equals()
In the java, each object have its own equals function. The function is defined in its parent java.lang.Object. when we want to compare two object if they are 'the same', we prefer to use it. I think many programmers have ever used it, and must be familiar with its features.
What is hashCode()
It is by design useful for only one thing: putting an object in a hash table. Hence the name. As we all known, we put an object into a hash table accroding the hash code. The code is a identity of a object in the hash table. when we want to achieve the object from the table, we should provide the hash code of the object. Then we can calculate the position of the object in the table. I will not introduce much knowledge about the mechanism of a hash function.
Why we should overwrite the hashCode() when we overwrite the equals()
In the java, the hashCode() function is closely associated with HashMap and HashSet. From the source code of the two collections, we can figure out the relationship. From the put method and the get method of HashMap,
public V put(K key, V value)
{
//if the key is null, invoke the putForNullKey function
if (key == null)
return putForNullKey(value);
//derive the key's hashCode and invoke the hash method to calculate its hash value
int hash = hash(key.hashCode());
//search the index of the hash value in the hash table
int i = indexFor(hash, table.length);
//if the Entry of the 'i' index is not null, loop the e
for (Entry<K,V> e = table[i]; e != null; e = e.next)
{
Object k;
//if find a key and its hash value is the same with the target object's hash value and the key is equals the target object,
//return the key's value
if (e.hash == hash && ((k = e.key) == key
|| key.equals(k)))
{
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
//if the Entry of the 'i' index is null,
modCount++;
//add the target key and its value to the table
addEntry(hash, key, value, i);
return null;
}
public V get(Object key) {
if (key == null)
return getForNullKey();
int hash = hash(key.hashCode());
for (Entry<K,V> e = table[indexFor(hash, table.length)];
e != null;
e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
return e.value;
}
return null;
}
From the code above, it is obvious that when we employ a HashMap or HashSet and use their put and get, the collection will compare both of the objects's hash value .
So the key point of overwriting the hashCode() is that the same hashCode should be returned no matter when we invoke the hashCode() of a object.
If the hashCode() does not return the same hash value, the 'put' and the 'get' method will not get the same hash value, which will lead to a terrible result that we can not get the desired object. As a conquence, if we use the equals and HashMap or HashSet, we should take care of the hashCode(). That is when we overwrite the equals(), we need overwrite the hashCode().
To explain the reason in deatil, I prefer to quote a example from a bloger named 'fhuan123' to illustrate my point.
我们看一个简单的例子,就能更加清楚的理解上面的意思。假定我们写了一个类:Person (人),我们判断一个对象“人”是否指向同一个人,只要知道这个人的身份证号一直就可以了。
先来个没有重写Code类的hashcode()的例子吧,看看是什么效果:
package com.fit; import java.util.HashMap; /**
* 身份证类
*
* @author ZYD
*
*/
public class Code { /**
* 身份证号码,一旦确定就不能更改
*/
private final int id; public int getId() {
return id;
} /**
* 通过构造方法确定身份证号码
*
* @param id
*/
public Code(int id) {
this.id = id;
} /**
* 重写equals()方法
*/
public boolean equals(Object o) {
// 如果地址一样,则两个对象相同
if (this == o) {
return true;
}
// 如果两个对象是同一类型,则比较其属性值是否都相同。如果都相同,则说明两个对象也相同;否则,说明这两个对象不相同。
if (o instanceof Code) {
Code co = (Code) o;
boolean b = (co.id == this.id);
return b;
}
return false;
} /**
* 重写toString()方法
*/
public String toString() {
return "【身份证】:" + id;
} /**
* 测试
* @param args
*/
public static void main(String[] args) { HashMap<Code, Person> map = new HashMap<Code, Person>(); Person p1 = new Person(new Code(10001),"张三");
Person p2 = new Person(new Code(10002),"李四"); map.put(p1.getCode(), p1);
map.put(p2.getCode(), p2); System.out.println("HashMap 中存放的人员信息:\n"+map); //张三改名为张山,身份证号不变。
Person p3 = new Person(new Code(10001),"张山");
map.put(p3.getCode(), p3); System.out.println("张三改名为张山后 HashMap 中存放的人员信息:\n"+map); //查找身份证为10001 的人员信息
System.out.println("查找身份证为:10001 的人员信息:"+map.get(new Code(10001)));
}
} /**
* 人类
* @author Administrator
*
*/
class Person { /**
* 每一个成人都有一个身份证
*/
private Code code; /**
* 姓名
*/
private String name; public Code getCode() {
return code;
} public void setCode(Code code) {
this.code = code;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Person() { } public Person(Code code, String name) {
this.code = code;
this.name = name;
} /**
* 重写equals()方法 当两个人得身份证号相同以及姓名相同时,表示这两个人是同一个人。
*/
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o instanceof Person) {
Person p = (Person) o;
boolean b = this.code.equals(p.code) && this.name.equals(p.name);
return b;
}
return false;
} /**
* 重写toString()方法
*/
public String toString() {
return "【姓名】:" + name + " ";
}
}
运行结果:
HashMap 中存放的人员信息:
{【身份证】:10002=【姓名】:李四 ,
【身份证】:10001=【姓名】:张三 }
张三改名为张山后 HashMap 中存放的人员信息:
{【身份证】:10002=【姓名】:李四 ,
【身份证】:10001=【姓名】:张三 , 【身份证】:10001=【姓名】:张山 }
查找身份证为:10001 的人员信息:null
从上面的结果可以看出:
我们所做的更新和查找操作都失败了。失败的原因就是我们的身份证类: Code 没有覆写 hashCode()
方法。这个时候,当查找一样的身份证号码的键值对的时候,使用的是默认的对象的内存地址来进行定位。这样,后面的所有的身份证号对象
new
Code(10001) 产生的 hashCode () 值都是不一样的,所以导致操作失败。
重写Code类的hashcode(),代码上:
package com.fit; import java.util.HashMap; /**
* 身份证类
*
* @author ZYD
*
*/
public class Code { /**
* 身份证号码,一旦确定就不能更改
*/
private final int id; public int getId() {
return id;
} /**
* 通过构造方法确定身份证号码
*
* @param id
*/
public Code(int id) {
this.id = id;
} /**
* 重写equals()方法
*/
public boolean equals(Object o) {
// 如果地址一样,则两个对象相同
if (this == o) {
return true;
}
// 如果两个对象是同一类型,则比较其属性值是否都相同。如果都相同,则说明两个对象也相同;否则,说明这两个对象不相同。
if (o instanceof Code) {
Code co = (Code) o;
boolean b = (co.id == this.id);
return b;
}
return false;
} /**
* 重写hashcode()方法,以身份证号码作为hash码。
*
* @return
*/
public int hashCode() {
return id;
} /**
* 重写toString()方法
*/
public String toString() {
return "【身份证】:" + id;
} /**
* 测试
* @param args
*/
public static void main(String[] args) { HashMap<Code, Person> map = new HashMap<Code, Person>(); Person p1 = new Person(new Code(10001),"张三");
Person p2 = new Person(new Code(10002),"李四"); map.put(p1.getCode(), p1);
map.put(p2.getCode(), p2); System.out.println("HashMap 中存放的人员信息:\n"+map); //张三改名为张山,身份证号不变。
Person p3 = new Person(new Code(10001),"张山");
map.put(p3.getCode(), p3); System.out.println("张三改名为张山后 HashMap 中存放的人员信息:\n"+map); //查找身份证为10001 的人员信息
System.out.println("查找身份证为:10001 的人员信息:"+map.get(new Code(10001)));
}
} /**
* 人类
* @author Administrator
*
*/
class Person { /**
* 每一个成人都有一个身份证
*/
private Code code; /**
* 姓名
*/
private String name; public Code getCode() {
return code;
} public void setCode(Code code) {
this.code = code;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Person() { } public Person(Code code, String name) {
this.code = code;
this.name = name;
} /**
* 重写equals()方法 当两个人得身份证号相同以及姓名相同时,表示这两个人是同一个人。
*/
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o instanceof Person) {
Person p = (Person) o;
boolean b = this.code.equals(p.code) && this.name.equals(p.name);
return b;
}
return false;
} /**
* 重写toString()方法
*/
public String toString() {
return "【姓名】:" + name + " ";
}
}
运行效果:
HashMap 中存放的人员信息:
{【身份证】:10001=【姓名】:张三 , 【身份证】:10002=【姓名】:李四
}
张三改名为张山后 HashMap 中存放的人员信息:
{【身份证】:10001=【姓名】:张山 , 【身份证】:10002=【姓名】:李四
}
查找身份证为:10001 的人员信息:【姓名】:张山
Summary
if we will put the target class into a colleciton of hash, such as HashMap or HashSet, we would better to overwrite the hashCode() method. As a result of overwriting , if a object equals the other, they should have the same hash value.
In the next chapter, I will summary some rules and guidlines when overwrite a hashCode() method.
References
Guidelines and rules for GetHashCode
JAVA中重写equals()方法为什么要重写hashcode()方法说明
解析Java对象的equals()和hashCode()的使用
Why we should overwrite the hashCode() when we overwrite the equals()的更多相关文章
- Java中==、equals、hashcode的区别与重写equals以及hashcode方法实例(转)
Java中==.equals.hashcode的区别与重写equals以及hashcode方法实例 原文地址:http://www.cnblogs.com/luankun0214/p/4421770 ...
- hashCode之二--Java:重写equals()和hashCode()
以下内容总结自<Effective Java>. 1.何时需要重写equals() 当一个类有自己特有的“逻辑相等”概念(不同于对象身份的概念). 2.设计equals() [1]使用in ...
- 【转】Java中==、equals、hashcode的区别与重写equals以及hashcode方法实例
原文地址:http://www.cnblogs.com/luankun0214/p/4421770.html 感谢网友的分享,记录下来只为学习. 1.重写equals方法实例 部分代码参考http ...
- Java中equals()和hashCode()的关系以及重写equals()和hashCode()的重要性
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6580647.html 一:关系 如果两个对象相等(equal),它们的hashcode一定相同: 如果两个对 ...
- Java中==、equals、hashcode的区别与重写equals以及hashcode方法实例
1.重写equals方法实例 部分代码参考http://blog.csdn.net/wangloveall/article/details/7899948 重写equals方法的目的是判断两个对象 ...
- How to implement equals() and hashCode() methods in Java[reproduced]
Part I:equals() (javadoc) must define an equivalence relation (it must be reflexive, symmetric, and ...
- hashCode的作用
在一般的应用中你不需要了解hashCode的用法,但当你用到HashMap,HashSet等集合类时要注意下hashCode. 你想通过一个object的key来拿HashMap的value, ...
- JAVA中用堆和栈的概念来理解equals() "=="和hashcode()
在学习java基本数据类型和复杂数据类型的时候,特别是equals()"=="和hashcode()部分时,不是很懂,也停留了很长时间,最后终于有点眉目了. 要理解equals() ...
- equals()和hashCode()隐式调用时的约定
package com.hash; import java.util.HashMap; public class Apple { private String color; public Apple( ...
随机推荐
- day 69 ORM 多表增删改查操作
http://www.cnblogs.com/liwenzhou/p/8660826.html 下面的代码是在 python console中配置的. 关闭pycharm会消失. from app01 ...
- CEPH 创建 RPM 安装包
1.安装依赖环境 yum install gcc make python-devel openssl-devel graphviz autoconf automake rpm-build redhat ...
- Highcharts纯Javascript图表使用讲解
Highcharts提供大量的选项配置参数,您可以轻松定制符合用户要求的图表,目前官网只提供英文版的开发配置说明文档,而中文版的文档网上甚少,且零散不全.这里,我把Highcharts常用的最核心的参 ...
- 逆向工程生成的Mapper.xml以及*Example.java详解
逆向工程生成的接口中的方法详解 在我上一篇的博客中讲解了如何使用Mybayis逆向工程针对单表自动生成mapper.java.mapper.xml.实体类,今天我们先针对mapper.java接口中的 ...
- POJ 2377
#include<stdio.h> #define MAXN 1005 #include<iostream> #include<algorithm> #define ...
- Newtonsoft.Json 序列化 排除指定字段或只序列化指定字段
using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System; using System.Collections.G ...
- redis集群配置及运行命令(windows和centos)附Python测试范例代码
表示配置主服务器器的IP和端口 slaveof <masterip> <masterport> # 设置slave是否是只读的.从2.6版起,slave默认是只读的. slav ...
- 删除Open with Atom右键菜单
特别特别讨厌Atom的右键菜单,叕没有设置项可以去掉,烦!安装完RegScanner v2.15汉化版之后,开始搜索删除Atom的右键菜单 1.打开E:\RegScanner2.加载配置文件 E:\R ...
- Fiddler Web Debugger是什么?(图文详解)
不多说,直接上干货! 1.为什么是Fiddler? 抓包工具有很多,小到最常用的web调试工具firebug,达到通用的强大的抓包工具wireshark. 见 Windows里安装wireshark或 ...
- 软件魔方制作系统启动盘并安装win10系统
不多说,直接上干货! 推荐软件:软件魔方 http://mofang.ruanmei.com/ 这里,我想说的是,这个软件来制作系统盘,是真的方便和好处多多.具体我不多说,本人也是用过其他的如大白菜 ...