我應該如何在Java中爲以下類實現hashCode()
和equals()
?如何實現hashCode和equals方法
class Emp
{
int empid ; // unique across all the departments
String name;
String dept_name ;
String code ; // unique for the department
}
我應該如何在Java中爲以下類實現hashCode()
和equals()
?如何實現hashCode和equals方法
class Emp
{
int empid ; // unique across all the departments
String name;
String dept_name ;
String code ; // unique for the department
}
>來源 - >生成hashCode()和equals()方法給出了這樣的:
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (code == null ? 0 : code.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Emp))
return false;
Emp other = (Emp) obj;
return code == null ? other.code == null : code.equals(other.code);
}
我已經選擇代碼作爲唯一字段
嗯......我通常會比較equals()方法中的所有字段,除非你真的有很強的理由不去... – 2010-01-25 13:04:11
因此,如果來自不同部門的兩個'Emp'具有相同的代碼,它們是相等的嗎?你至少需要在這個實現中添加'dept_name'。 – 2010-01-25 13:21:05
我接受這個答案,因爲我知道需要做什麼。jutky假定代碼作爲唯一標識符。 – Vidya 2010-01-25 13:24:10
如果代碼是唯一的(即自己的業務),最好只使用代碼equals和hashCode - 這是很好的做法,從對象ID單獨的業務密鑰(代碼)(ID)。
這裏是一個不錯的讀取:Hibernate Documentation: Equals and HashCode(不僅適用於Hibernate的本身)
這似乎是一個部門的獨特之處,所以建議部門可以共享相同的「代碼」。無論如何,如果你問我一個相當含糊的問題。 – 2010-01-25 12:59:53
什麼是您在equals中使用的值,以確定兩個對象是否相同,是您需要用來創建哈希碼的值。
public boolean equals(Object o) {
boolean result = false;
if(o instanceof CategoryEnum) {
CategoryEnum ce = (CategoryEnum) o;
result = ce.toString().equals(name);
}
return result;
}
public int hashCode()
{
int hash = 6;
hash += 32 * name.hashCode();
return hash;
}
(從我這裏沒有-1)這不完全正確。只使用hashCode的一個子集是完全合法的(例如代碼和名稱用於equals,只用於hashCode)。使用一個常量hashCode('public int hashCode(){return 42;}')甚至是合法的 - 它破壞了哈希集合(HashMap,HashSet,...)的性能,但是它們保持正常工作。所以它比無效的hashCode方法更好。唯一的規則是:如果兩個對象相等('a.equals(b)'),它們必須具有相同的散列碼('a.hashCode()== b.hashCode()')。如果它們不相等,則散列碼可能仍然相等。 – sfussenegger 2010-01-25 14:40:14
equals()和hashcode(),它們有很多不同的地方。 equals(),如果我們不從Object中覆蓋它,它表示兩個變量是否指向同一個對象堆?
public Class Student(){
private int id;
private name;
public Student(int id,String name){
this.name=name;
this.id=id;
}
public void main(String[] args){
Student A=new Student(20,'Lily');
Student B=new Student(20,'Lily');
boolean flag=A.equals(B)//flag=flase;
/*
*Although they attribute the same, but they are two different objects, they point to different memory
*/
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
if (this.getClass() != obj.getClass()) {
return false;
}
Student s=(Student)obj;
return new Integer(this.id).equals(new Integer(s.id))&&this.name.equals(s.name);
}
/**
*Sometimes even though we Override the equals, but we still can not determine whether the *two objects the same,
*In the collection object, such as HashSet, this time we have to Override the hashoCode()
*/
public int hashCode(){
return id + name.hashCode() ;
}
試試這個代碼,使用org.apache.commons.lang3.builder
public int hashCode() {
return new HashCodeBuilder(17, 31). // two randomly chosen prime numbers
append(empid).
append(name).
append(dept_name).
append(code).
toHashCode();
}
public boolean equals(Object obj) {
if (obj == this)
return true;
if (!(obj instanceof Person))
return false;
Emp rhs = (Emp) obj;
return new EqualsBuilder().
// if deriving: appendSuper(super.equals(obj)).
append(name, rhs.name).
isEquals();
}
番石榴輔助方法來創建它們。您告訴它哪些字段需要考慮,它會爲您處理空值,並對散列碼進行素數計算。
IDE也可以根據您選擇的字段生成它們。
把它委託給這樣的工具的好處是你可以得到一個標準的解決方案,並且不用擔心在你的項目中遍佈各種實現的bug和維護。
下面是使用番石榴和生成一個的IntelliJ插件的示例:https://plugins.jetbrains.com/plugin/7244?pr=
即沒有Java源代碼。或者你創建了自己的叫做'string'的類嗎?你真的應該看看這個:http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html – 2010-01-25 12:57:46
我看到Joachim在你的代碼示例中改變了一些錯誤。不過,我建議查看我之前評論中發佈的鏈接。 – 2010-01-25 13:01:19
我正在使用java的String類 – Vidya 2010-01-25 13:02:22