---
title: "equals() 和 hashCode()"
author: "dr2tr"
date: "2007-02-28T13:19:00Z"
tags: []
slug: "equals-hashcode"
draft: false
lastmod: "2007-02-28T13:19:00Z"
id: 233433
url: "https://dr2tr.iteye.com/blog/233433"
---

我们知道，equals()函数是用来做比较的。java中的比较有两种：一种是内存地址的比较，一种是内容的比较。而比较个体也有两种：一种是简单类型（这类简单说来无所谓内存地址的比较或者内容比较的区别）；还有一种是对象的比较，本文中说的主要是后者

在java中，（对象）内存地址的比较，是通过==完成的。比如 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)if(obj1 == obj2)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    ...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

这样的语句中，我们认为，如果obj1和obj2的内存地址相同，则返回true

而equals()通常是比较内容的。这里说“通常” ，是因为在最根本的Object类中，equal()函数做的是地址的比较。而在其他几乎所有的类中，equals()都经过重载，进行内容的比较。

而在说equals()的时候我们还涉及hashCode()是因为在有些应用中（比如，HashMap的key是对象），必须在重载equals()的同时重载hashCode()。**因为java中默认(Object)的hashCode是根据对象的地址计算得到的。**

我们通常不会注意到这个问题，因为我们通常所使用的*key*都是简单类型，或者是String, Long等一些特殊的对象（其特殊性请参看笔者在写java 浅拷贝和深拷贝时的讨论），这时候，这个问题被我们无意间绕过了

有人已经概括了这种我们忽略了的情况：“如果你想将一个对象A放入另一个收集（集合）对象B里，或者使用这个对象A为查找一个元对象在收集对 象B里位置的钥匙（key），并支持是否容纳（isContains()），删除收集对象B里的元对象（remove()?）这样的操作，那么，equals()和hashCode()函数必须开发者自己定义。” （括号为笔者添加）

为了便于理解，举一段程序为例：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.\*;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class Person...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)int id;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)String name;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//define getter and setter here, omited  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public Person(int id, String name)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     this.id = id;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     this.name = name;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public boolean equals(Object o)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       if(this==o) return true;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    if(o instanceof Person)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    return (this.id == ((Person)o).id) && (this.name.equals(((Person)o).name));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*//*           
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)public int hashCode(){  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    return id\*37;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public static void main(String args\[]) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    Person p1 = new Person(1,"aaa");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    Person p2 = new Person(1,"aaa");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    Map map = new HashMap();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    map.put(p2,p1);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    Person value = (Person)map.get(p1);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    System.out.println(value.name);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)  }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

这段代码的结果是什么？答案是nullPointerExcetpion.

而把hashCode()的注释去除，程序就可以返回正确的结果了。为什么呢？因为：

Map.put(key,value)时根据key.hashCode生成一个内部hash值，根据这个hash值将对象存放在一个table中

Map.get(key)会比较key.hashCode和equals方法，当且仅当这两者相等时，才能正确定位到table。而我们说过，默认的java是对地址进行比较的。

 

参考：

[http://blog.csdn.net/RichardSundusky/archive/2007/02/12/1508028.aspx](http://blog.csdn.net/RichardSundusky/archive/2007/02/12/1508028.aspx)

[http://blog.csdn.net/pbnow/archive/2006/04/25/677253.aspx](http://blog.csdn.net/pbnow/archive/2006/04/25/677253.aspx)

[http://blog.csdn.net/jery\_lee/archive/2004/10/13/135484.aspx](http://blog.csdn.net/jery_lee/archive/2004/10/13/135484.aspx)

[http://blog.csdn.net/ngqzmjmj/archive/2005/04/27/365149.aspx](http://blog.csdn.net/ngqzmjmj/archive/2005/04/27/365149.aspx)