Javaで次のクラスをhashCode()
どのように実装する必要がありますか?equals()
class Emp
{
int empid ; // unique across all the departments
String name;
String dept_name ;
String code ; // unique for the department
}
Eclipseでマウスを右クリック->ソース->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);
}
一意のフィールドとしてコードを選択しました
このコードを試して、使用してください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();
}
Guava には、それらを作成するためのヘルパー メソッドがあります。考慮するフィールドを指定すると、ヌルが処理され、ハッシュコードの素数計算が行われます。
IDE は、選択したフィールドに基づいてそれらを生成することもできます。
そのようなツールに委譲する利点は、標準的なソリューションを取得し、プロジェクト全体に広がるさまざまな実装のバグやメンテナンスについて心配する必要がないことです。
Guava を使用し、IntelliJ プラグインによって生成される例を次に示します: https://plugins.jetbrains.com/plugin/7244?pr=
コードが一意である場合(つまり、ビジネスキー)、equalsとhashCodeのコードのみを使用することをお勧めします。ビジネスキー(コード)をオブジェクトID(ID)から分離することをお勧めします。
ここに良い読み物があります:Hibernateドキュメント:EqualsとHashCode(Hibernate自体だけでなく有効)
2つのオブジェクトが同じであるかどうかを判断するために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;
}
equals() と hashcode() には、さまざまな場所があります。equals()、オブジェクトからオーバーライドしない場合、2 つの変数が同じオブジェクト ヒープを指しているかどうかを表します。
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() ;
}