在java中的Object类中的一个方法:
public native int hashCode();
一、hashCode方法作用
对于包含容器类型程序设计语言,基本上都会涉及到hashCode。Java中主要配合散列集合一起正常的运行:HashSet,HashMap和HashTable.
当向量向集合中插入对象时,需要判断集合中是否已经存在该对象。如果使用equals方法逐个比较,效率太低,这时候想到hashCode方法。当集合需要添加新的对象时候,先调用这个对象的hashCode方法得到hashCode值。在HashMap实现中会有专门table保存现有对象hashcode值,这样分为两种情况:
- 不存在新增加的hashcode值,直接加入到集合当中就可以。
- 存在相同hashcode值,调用equals方法与新元素进行比较,相同的话就存在,不相同散列其他地址。
下面代码就是java.util.HashMap中put具体实现:
public V put(K key,V value){
if(key==null){
return putForNullKey(value);
}
//得到hashCode值
int hash=hash(key.hashCode());
//是否存在hashCode的值,返回i;
int i=indexFor(hash,table.length);
for(Entry<K,V> e=table[i];e!=null;e=e.next){
Object k;
if(e.hash==hash&&(k=e.key)==key||key.equals(k)){
/*值类型是存储在内存中的堆栈(以后简称栈),而引用类型的变量在栈中仅仅是存储引用类型变量的地址,而其本身则存储在堆中。
==操作比较的是两个变量的值是否相等,对于引用型变量表示的是两个变量在堆中存储的地址是否相同,即栈中的内容是否相同。
equals操作表示的两个变量是否是对同一个对象的引用,即堆中的内容是否相同。
==比较的是2个对象的地址,而equals比较的是2个对象的内容。*/
V oldValue=e.value;
e.value=vaule;
e.recordAccess(this);
return oldValue;//返回已有的,更新。
}
}
modCount++;//map长度加1
addEntry(hash,key,value,i);
return null;//返回空表示成功??
}
有些人误认为默认情况下,hashCode返回的就是对象的存储地址。事实上这种说法不全面,确实有些JVM实现时候,直接返回对象的储存地址,但是大多数情况不是这样的,只能说和存储地址有一定关联。
//HotSpot JVM中生成hash散列值的实现
static inline inptr_t get_next_hash(Thread *self,oop obj){
inptr_t value=0;
if(hashCode==0){
//this form users an unguarded globabl Park-Miller RNG, ON MP system we'll have lots of RW access TO A GLOBAL,so the mechnaism induces lots of coherency traffic.
value=os::random();
}else if(hashCode==1){
//this variation has the property of being stable(idempotent) between STW operations.this can be userful in some of the 1-0 synchrnoization schemes.
intptr_t addrBits=intptr_t(obj)>>3;
value=addrBits^(addrBits>>5)^GVars.stwRandom;
}else if(hashCode==2){
value=1;//for sensitivity testing
}else if(hashCode==3){
value=++GVars.hcSequence;
}else if(hashCode==4){
value=intptr_t(obj);
}else{
//Marsaglia's xor-shift scheme with thread-specifi state This is probably the best overall imlmentation we'll likely make this the defautl in future releases.
unsigned t =Self->hashStateX;
t^=(t<<11);
Self->_hashStateX=Self->_hashStateY;
Self->_hashStateY=Self->_hashStateZ;
Self->_hashStateZ=Self->_hashStateW;
unsigned v=Self->_hasStateW;
v=(v^(t>>19))^(t^(t>>8));
Self->_hashStateW=v;
value=v;
}
value&=markOopDesc::hash_mask;
if(value==0){
value=0xBAD;
}
assert (value!=mkarkOopDesc::no_hash,"invariant");
TEVENT(hashCode:GENERATE);
return value;
}
所以判断两个对象是否相等情况:
- hashCode值不相等,那么就是两个不同的对象
- hashCode相等,那么需要通过equals方法来判断。
- 如果调用equals方法得到结果为true,则两个对象的hashCode值必定相等
- 如果equals方法得到结果为false,则两个对象hashcode值不一定不同。
- 如果两个对象的hashcode值不等,则equals方法得到结果必定为false;
- 如果两个对象hashcode值相等,则equals方法得到的结果未知.
二、equals方法和hashcode方法
有些情况下,程序设计一个类时候需要重写equals方法,比如String,注意:在重写equals方法时候,必须重写hashCode方法。
package com.test;
import java.util.*
class People{
private String name;
private int age;
public People(String name,int age){
this.name=name;
this.age=age;
}
public boolean equals(Object obj){
return this.name.equals((People)obj.name)&&this.age==((People)obj).age;
}
}
public class Main{
public void static main(String[] args){
People p1=new People("Jack",21);
System.out.println(p1.hashCode());//1340465859
}
HashMap<People,Integer> hashMap=new HashMap<People,Integer>();
hashMap.put(p1,1);
System.out.println(hashMap.get(new People("Jack",12)));//null
//返回null,但是理论上将对象姓名和年龄相同的对象看做同一个人,但是new一个对象出来,导致分配了不同的地址,导致hashCode值不同,所以返回null.这就要求对equals判断要同样重写hashCode。
System.out.println(hashMap.get(p1));//返回1
}
其中HashMap的get方法具体实现如下:
public V get(Object key){
if(key==null){
return getForNullKey();
}
int hash=hash(key.hashCode());//上面的hash值不同,就不会替换操作,所以返回null;
for(Entry<K,V> e=table[indexFor(hash,table.length)];
e!=null;e=e.next){
Objeck k;
if(e.hash==hash&&((k=e.key)==key||key.equals(k))){
return e.value;
}
}
return null;
}
只需要事先将equals和hashCode方法始终在逻辑上保持一致性。
package com.test;
import java.util.*
class People{
private String name;
private int age;
public People(String name,int age){
this.name=name;
this.age=age;
}
public int hashCode(){
return name.hashCode()*37+age;
}
public boolean equals(Object obj){
return this.name.equals((People)obj.name)&&this.age==((People)obj).age;
}
}
public class Main{
public void static main(String[] args){
People p1=new People("Jack",21);
System.out.println(p1.hashCode());//1340465859
}
HashMap<People,Integer> hashMap=new HashMap<People,Integer>();
hashMap.put(p1,1);
System.out.println(hashMap.get(new People("Jack",12)));//返回1
}
摘自Effective Java
- 程序执行期间,只要equals方法比较操作做过的信息没有被修改,那么对这个同一个对象调用多次,hashCode方法必须始终如一的返回同一个整数。
- 如果两个对象根据equals方法比较是相等的,那么调用两个对象的hashCode方法必须返回相同的整数结果。
- 如果两个对象根据equals方法比较是不等的,则hashCode不一定返回不同的整数。
《Java编程思想》
设计hashCode()是最重要因素就是:无论何时,对同一对象调用hashCode()都应该产生同样的值。如果在讲一个对象用put()添加HashMap时产生一个hashCode值,而用get()取出却产生另外一个值,那么无法获取这个对象。所以如果你hashCode方法依赖于对象中易变得数据,用户需当心,此数据变化时候hashCode()会产生一个不同的散列码。例如:
package com.test;
import java.util.*
class People{
private String name;
private int age;
public People(String name,int age){
this.name=name;
this.age=age;
}
public void setAge(int age){
this.age=age;
}
public int hashCode(){
return name.hashCode()*37+age;
}
public boolean equals(Object obj){
return this.name.equals((People)obj.name)&&this.age==((People)obj).age;
}
}
public class Main{
public void static main(String[] args){
People p1=new People("Jack",21);
System.out.println(p1.hashCode());//1340465859
}
HashMap<People,Integer> hashMap=new HashMap<People,Integer>();
hashMap.put(p1,1);
p1.setAge(13);
System.out.println(hashMap.get(p1));//null
}