Java如何計算hashcode值


在設計一個類的時候,很可能需要重寫類的hashCode()方法,此外,在集合HashSet的使用上,我們也需要重寫hashCode方法來判斷集合元素是否相等。

下面給出重寫hashCode()方法的基本規則:

· 在程序運行過程中,同一個對象多次調用hashCode()方法應該返回相同的值。

· 當兩個對象通過equals()方法比較返回true時,則兩個對象的hashCode()方法返回相等的值。

· 對象用作equals()方法比較標准的Field,都應該用來計算hashCode值。


下面給出hashCode()方法的一般規則:

1) 把對象內每個有意義的Field計算出一個int類型的hashCode值:

Boolean hashCode=(f?0:1)
整數類型(byte  short   int  char) hashCode=(int)f
long hashCode=(int)(f^(f>>>32))
float hashCode=Float.floatToIntBits(f)
double long l = Double.doubleToLongBits(f);
hashCode=(int)(l^(l>>>32))
普通引用類型 hashCode=f.hashCode()

2) 用第一步計算出來的多個hashCode值組合計算出一個hashCode值返回:

return f1.hashCode()+(int)f2;
為了避免直接相加產生偶然相等,可以通過為各個Field乘以任意一個質數后再相加。

return f1.hashCode()*17+(int)f2*13;

示例: 向HashSet集合中添加數據:

class R
{
	int count;
	public R(int count)
	{
		this.count = count;
	}
	public String toString()
	{
		return "R[count:" + count + "]";
	}
	public boolean equals(Object obj)
	{
		if(this == obj)
			return true;
		if (obj != null && obj.getClass() == R.class)
		{
			R r = (R)obj;
			if (r.count == this.count)
			{
				return true;
			}
		}
		return false;
	}
	public int hashCode()
	{
		return this.count;
	}
}
public class HashSetTest2
{
	public static void main(String[] args) 
	{
		HashSet hs = new HashSet();
		hs.add(new R(5));
		hs.add(new R(-3));
		hs.add(new R(9));
		hs.add(new R(-2));
		//打印HashSet集合,集合元素沒有重復
		System.out.println(hs);
		//取出第一個元素
		Iterator it = hs.iterator();
		R first = (R)it.next();
		//為第一個元素的count實例變量賦值
		first.count = -3;     //①
		//再次輸出HashSet集合,集合元素有重復元素
		System.out.println(hs);
		//刪除count為-3的R對象
		hs.remove(new R(-3));   //
		//可以看到被刪除了一個R元素
		System.out.println(hs);
		//輸出false
		System.out.println("hs是否包含count為-3的R對象?"
			+ hs.contains(new R(-3)));
		//輸出false
		System.out.println("hs是否包含count為5的R對象?"
			+ hs.contains(new R(5)));
	}
}


通過以上方式,我們可以輕松的去重寫一個類的hashCode()方法。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM