[英]Compact equals and hashcode
我有一個有4個屬性的bean:
user
institutionId
groupId
postingDate
我使用Eclipse生成equals和hashcode,但結果代碼並不漂亮。 是否有一種緊湊的方式來做同樣的事情? 假設我希望equals和hashcode使用所有屬性或它們的子集。
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((groupId == null) ? 0 : groupId.hashCode());
result = prime * result + ((institutionId == null) ? 0 : institutionId.hashCode());
result = prime * result + ((postingDate == null) ? 0 : postingDate.hashCode());
result = prime * result + ((user == null) ? 0 : user.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ManGroupKey other = (ManGroupKey) obj;
if (groupId == null) {
if (other.groupId != null)
return false;
} else if (!groupId.equals(other.groupId))
return false;
if (institutionId == null) {
if (other.institutionId != null)
return false;
} else if (!institutionId.equals(other.institutionId))
return false;
if (postingDate == null) {
if (other.postingDate != null)
return false;
} else if (!postingDate.equals(other.postingDate))
return false;
if (user == null) {
if (other.user != null)
return false;
} else if (!user.equals(other.user))
return false;
return true;
}
在Java 7中
public int hashCode() {
return Objects.hash(groupId, institutionId, postingDate, user);
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
// cast to correct class
Target o = (Target)obj;
return Objects.equals(groupId, o.groupId) &&
Objects.equals(institutionId, o.institutionId) &&
Objects.equals(postingDate, o.postingDate) &&
Objects.equals(user, o.user);
}
你可以壓縮代碼,但是你引入錯誤的幾率遠高於你做任何有用的東西。 equals和hash code方法的所有部分都有一個原因。
如果它困擾你,大多數IDE都有折疊編輯器,只需點擊黃色小框(通常),方法的所有內容都會被隱藏起來。
您可以使用Apache-common-langs( http://commons.apache.org/proper/commons-lang/ )類HashCodeBuilder和EqualsBuilder來代替使用eclipse生成的代碼:
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this);
}
hashCode:要么:
@Override
public int hashCode() {
return Objects.hash(user, institutionId, groupId, postingDate);
}
要么:
@Override
public int hashCode() {
int result = 17;
result = 31 * result + Objects.hashCode(user);
result = 31 * result + Objects.hashCode(institutionId);
result = 31 * result + Objects.hashCode(groupId);
result = 31 * result + Objects.hashCode(postingDate);
return result;
}
等於:
public boolean equals(Object obj){
if (obj == this){
return true;
}
if (! (obj instanceof ManGroupKey)){
return false;
}
ManGroupKey other = (ManGroupKey) obj;
return Objects.equals(user, other.user)
&& Objects.equals(institutionId, other.institutionId)
&& Objects.equals(groupId, other.groupId)
&& Objects.equals(postingDate, other.postingDate);
}
您至少可以通過刪除other.x != null
檢查來刪除一級嵌套。
以這種方式比較值:當y
為null
時, x.equals(y)
將始終返回false
。
除此之外: .equals()
方法是一個很好的例子,其中一些反射可以很方便,可能提取到一個通用的實用方法。 您所要做的就是遍歷不同的字段,看看它們在兩個對象中是否相等,可以在幾行中完成。
顯然,這只有在您真正想要比較每個字段時才可行(或者您必須添加一些添加內容以便您選擇字段)。
我認為可以為你提供套件的庫是常見的apache。 它提供了EqualsBuilder
和HashCodeBuilder
類,它們完全符合您的要求。
請仔細考慮這個問題: Apache Commons equals / hashCode builder
以下是一些代碼段:
public class Bean{
private String name;
private int length;
private List<Bean> children;
@Override
public int hashCode(){
return new HashCodeBuilder()
.append(name)
.append(length)
.append(children)
.toHashCode();
}
@Override
public boolean equals(final Object obj){
if(obj instanceof Bean){
final Bean other = (Bean) obj;
return new EqualsBuilder()
.append(name, other.name)
.append(length, other.length)
.append(children, other.children)
.isEquals();
} else{
return false;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.