[Java]hashCode和equals方法的比较

【转载于305-小疯Blog(http://blog.csdn.net/qq598535550/article/details/40407855)】

一、equals

首先我们要明白equals和hashcode方法都是从object类中继承过来的。

equals()方法在object类中定义如下:

<pre name="code" class="java">public boolean equals(Object obj) { 
	return (this == obj); 
}

 

很明显是对两个对象的地址值进行的比较(即比较引用是否相同)。但是我们必需清楚,String 、Integer、Double等这些封装类在使用equals()方法时,已经覆盖了object类的equals()方法。

<pre name="code" class="java">public boolean equals(Object anObject) { 
	if (this == anObject) { 
		return true; 
<span style="white-space:pre">	</span>} 
<span style="white-space:pre">	</span>if (anObject instanceof String) { 
<span style="white-space:pre">		</span>String anotherString = (String)anObject; 
    <span style="white-space:pre">		</span>int n = count; 
    <span style="white-space:pre">		</span>if (n == anotherString.count) { 
<span style="white-space:pre">			</span>char v1[] = value; 
<span style="white-space:pre">			</span>char v2[] = anotherString.value; 
<span style="white-space:pre">			</span>int i = offset; 
<span style="white-space:pre">			</span>int j = anotherString.offset; 
<span style="white-space:pre">			</span>while (n-- != 0) { 
    <span style="white-space:pre">				</span>if (v1[i++] != v2[j++]) 
<span style="white-space:pre">					</span>return false; 
<span style="white-space:pre">			</span>} 
<span style="white-space:pre">			</span>return true; 
<span style="white-space:pre">		</span> } 
<span style="white-space:pre">	</span>} 
<span style="white-space:pre">	</span>return false; 
} 

 

很明显,这是进行的内容比较,而已经不再是地址的比较,这也是我们程序设计者想要的。


二、hasdcode

其实是hasdCode方法,在object类中定义如下:

public native int hashCode();

说明是一个本地方法,它的实现是根据本地机器相关的。当然我们可以在自己写的类中覆盖hashcode()方法,比如StringIntegerDouble等等这些类都是覆盖了hashcode()方法的。

例如在String类中定义的hashcode()方法如下:

<pre name="code" class="java">public int hashCode() { 
<span style="white-space:pre">	</span>int h = hash; 
<span style="white-space:pre">	</span>if (h == 0) { 
<span style="white-space:pre">		</span>int off = offset; 
<span style="white-space:pre">		</span>char val[] = value; 
<span style="white-space:pre">		</span>int len = count; 
<span style="white-space:pre">		</span>for (int i = 0; i < len; i++) { 
<span style="white-space:pre">			</span>h = 31*h + val[off++]; 
<span style="white-space:pre">		</span>} 
<span style="white-space:pre">		</span>hash = h; 
 <span style="white-space:pre">	</span>} 
        return h; 
} 

 
三、equals和hashcode方法的区别 

这里我们首先要明白一个问题:

equals()相等的两个对象,hashcode()一定相等;equals()不相等的两个对象,却并不能证明他们的hashcode()不相等。换句话说,equals()方法不相等的两个对象,hashcode()有可能相等。(我的理解是由于哈希码在生成的时候产生冲突造成的)。 

反过来:hashcode()不等,一定能推出equals()也不等;hashcode()相等,equals()可能相等,也可能不等。


四、总结

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

1、判断两个对象的hashCode是否相等 
①如果不相等,认为两个对象也不相等,完毕 
②如果相等,转入2
(这一点只是为了提高存储效率而要求的,其实理论上没有也可以,但如果没有,实际使用时效率会大大降低,所以我们这里将其做为必需的。后面会重点讲到这个问题。) 

2、判断两个对象用equals运算是否相等 
①如果不相等,认为两个对象也不相等 
②如果相等,认为两个对象相等(equals()是判断两个对象是否相等的关键) 

为什么是两条准则,难道用第一条不行吗?不行,因为前面已经说了,hashcode()相等时,equals()方法也可能不等,所以必须用第2条准则进行限制,才能保证加入的为非重复元素。 


以上内容是结合网上资料加上本人理解所写的,希望对你有帮助!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值