您的位置:首页 > 编程语言 > Java开发

Java基础——hashcode()与equals()

2016-01-31 19:44 411 查看

对象怎样才算相等:

1、引用相等性:堆上同一对象 的两个引用。

2、对象相等性:堆上的俩个不同 的对象在意义上是相同的(必须覆盖equals()与hashcode()方法)


hashcode()与equals()的相关规定:


(1)如果两个对象相等,则hashcode必须也是相等的。

Song类

<span style="font-size:18px;">	@Override
public int hashCode() {
System.out.println("songName.hashCode()="+songName.hashCode());
System.out.println("artistName.hashCode()="+artistName.hashCode());
System.out.println("mData.hashCode()="+mData.hashCode());
return super.hashCode();
}

public Song(String songName, String artistName, String mData) {
this.songName = songName;
this.artistName = artistName;
this.mData = mData;
}</span>
测试:
<span style="font-size:18px;">                List<Song>songList = new ArrayList<>();
</span>
<span style="font-size:18px;">                Song song = new Song("天未亮心微凉", "夏婉安","32" );</span>
<span style="font-size:18px;">		//songList加入同一个对象,输出该对象的实例变量的hashCode以及对象 hashcode
songList.add(song);
songList.add(song);</span>
<span style="font-size:18px;">		System.out.println(songList.get(0).hashCode());
System.out.println(songList.get(1).hashCode());</span>

输出
<span style="font-size:18px;">songName.hashCode()=-129882095
artistName.hashCode()=22649775
mData.hashCode()=1631
38139054
songName.hashCode()=-129882095
artistName.hashCode()=22649775
mData.hashCode()=1631
38139054</span>

(2)如果两个对象相等,对其中一个对象调用equals()必须返回true。

也就是说,若a.equals(b)则b.equals(a)。

<span style="font-size:18px;">System.out.println(songList.get(1).equals(songList.get(0)));</span>
<span style="font-size:18px;">System.out.println(songList.get(0).equals(songList.get(1)));</span>
输出 true

true

(3)如果俩个对象有相同的hashcode值,他们也不一定是相等的,但若是两个对象相等,

则hashcode值一定是相等的

/**

*不同的对象会有相同的hashcode的值?:

*hashcode()所使用的杂凑算法也许刚好回让多个对象传回相同的杂凑值。

*越糟糕的杂凑算法越容易碰撞,但这也与数据值域分布的特性有关。(哎,重学数据结构吧)

*例:hashset是通过hashcode来缩小查询的元素,在通过euqals()来认定是否找到相同的项目添加与否

*/


若默认通过杂凑算法得到的hashcode值为2132919775,来比较两个对象

<span style="font-size:18px;">	@Override
public int hashCode() {
return 2132919775;
}</span>
测试
<span style="font-size:18px;">                Song song = new Song("天未亮心微凉", "夏婉安","32" );
songList.add(song);
song = new Song("天未亮心微凉", "夏婉安","32" );
songList.add(song);
System.out.println(songList.get(0).hashCode());
System.out.println(songList.get(1).hashCode());
System.out.println(songList.get(1).equals(songList.get(0)));
System.out.println(songList.get(0).equals(songList.get(1)));</span>
输出
<span style="font-size:18px;">2132919775
2132919775
false
false
</span>

(4)因此若equals()被覆盖过,则hashcode也必须覆盖。

。。。

(5)hashcode()的默认行为是对在heap上的对象产生独特的值。如果你没有override过hashCode(),

则该class的两个对象怎样都不会被认为是相同的 。

<span style="font-size:18px;">while(true){
Song song = new Song("天未亮心微凉", "夏婉安","32" );
System.out.println(song.hashCode());
}</span>
输出:
1399890426
127553250
598793044
806320666
1209620892
2018129342
1343130276
.......

(6)equals()的默认行为是执行==的比较。也就是说去测试两个引用是否对上heap上同一个对象。

如果equals()没有被覆盖过,两个对象永远不会被视为相同的,因为不同的对象有不同的字节组合。

a.equal(b)则:a.hashCode() == b.hashCode();

但a.hashCode() == b.hashCode();不一定a.equal(b)





java基础看完了,浅层次的还差反射,注解吧(有人指点就好了)。看完《HeadFirst》收获还是挺多的,对android帮助挺大,接下来除了练习,就看一本javaWeb轻量级开发,骚年坚持!

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: