JAVA

HashMap的hashCode() 和equa()的使用

2017-02-18  本文已影响39人  ksjunhao

来自http://www.cnblogs.com/jisheng/archive/2011/09/03/2165536.html

在java的集合中,判断两个对象是否相等的规则是:

1. 判断两个对象的hashCode是否相等

如果不相等,认为两个对象也不相等,完毕

如果相等,转入2

2. 判断两个对象用equals运算是否相等

如果不相等,认为两个对象也不相等

如果相等,认为两个对象相等

importjava.util.HashSet;

importjava.util.Iterator;

importjava.util.Set;

importandroid.app.Activity;

importandroid.os.Bundle;

publicclassTestCollectionActivityextendsActivity {

/**Called when the activity is first created.*/

@Override

publicvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Set hashSet =newHashSet();

hashSet.add(newPerson("张先生", 12121212));

hashSet.add(newPerson("张太太", 12121213));

hashSet.add(newPerson("张先生", 12121210));

hashSet.add(newPerson("网先生", 12121210));

hashSet.add(newPerson("网先生", 12121210));

hashSet.add(newPerson("张先生", 12121210));

/**最后结果输出为:

*       张先生 12121212

张太太 12121213

网先生 12121210

张先生 12121210

如果不添加hashCode()和equals(),所有person成员都将正常输出

添加hashCode和equals之后,若a.hashCode() = b.hashCode且equals返回为true,此时原先的成员将被替代,

导致了上面的结果输出

*/

Iterator it = hashSet.iterator();

while(it.hasNext()) {

Person person = it.next();

System.out.println(person.getName() + " " + person.getId_card());

}

}

publicclassPerson {

privateString name;

privatelongid_card;

publicPerson(String name,longid_card) {

this.name = name;

this.id_card = id_card;

}

publiclonggetId_card(){

returnid_card;

}

publicString getName() {

returnname;

}

publicinthashCode() {

intresult = 1;

result = (int)(id_card ^(id_card>>32));

returnresult;

//final int PRIME = 31;

//int result = 1;

//result = PRIME * result + (int)(id_card ^ (id_card >>32));//long型数据取(int)位

//result = PRIME * result + ((name == null) ? 0 : name.hashCode());

//return result;

}

publicbooleanequals(Object obj) {

if(this== obj)

returntrue;

if(obj ==null)

returnfalse;

if(getClass() != obj.getClass())

returnfalse;

finalPerson other = (Person) obj;

if(id_card != other.id_card)

returnfalse;

if(name ==null) {

if(other.name !=null)

returnfalse;

}elseif(!name.equals(other.name))

returnfalse;

returntrue;

}

}

}

hashCode()和equal()由输出情况可得知:添加hashCode()和equals()之后,若a.hashCode() = b.hashCode且equals返回为true,此时原先的成员将被替代,导致了上面的结果输出。

hashCode用于快速判断两个对象是否相等,如果相等,则根据equal的返回值确定是否覆盖,equal()返回true则覆盖。

下面是补充:

=====================================

1、 为什么要重载equal方法?

答案:因为Object的equal方法默认是两个对象的引用的比较,意思就是指向同一内存,地址则相等,否则不相等;如果你现在需要利用对象里面的值来判断是否相等,则重载equal方法。

2、 为什么重载hashCode方法?

答案:一般的地方不需要重载hashCode,只有当类需要放在HashTable、HashMap、HashSet等等hash结构的集合时才会重载hashCode,那么为什么要重载hashCode呢?就HashMap来说,好比HashMap就是一个大内存块,里面有很多小内存块,小内存块里面是一系列的对象,可以利用hashCode来查找小内存块hashCode%size(小内存块数量),所以当equal相等时,hashCode必须相等,而且如果是object对象,必须重载hashCode和equal方法。

3、 为什么equals()相等,hashCode就一定要相等,而hashCode相等,却不要求equals相等?

答案:1、因为是按照hashCode来访问小内存块,所以hashCode必须相等。

2、HashMap获取一个对象是比较key的hashCode相等和equal为true。

之所以hashCode相等,却可以equal不等,就比如ObjectA和ObjectB他们都有属性name,那么hashCode都以name计算,所以hashCode一样,但是两个对象属于不同类型,所以equal为false。

4、 为什么需要hashCode?

1、 通过hashCode可以很快的查到小内存块。

2、 通过hashCode比较比equal方法快,当get时先比较hashCode,如果hashCode不同,直接返回false。

上一篇 下一篇

猜你喜欢

热点阅读