比較我有同一個實體「社區」兩個對象在Java中
和兩個對象(社區和COM)具有相同的價值觀
Communty.java有以下變量的兩個不同的對象:
private Integer communityId;
private String communityName;
private String description;
// many to many relationship
private Set<Faculty> faculties = new HashSet<Faculty>();
private Set<User> users = new HashSet<User>();
我用相等的方法:
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if(obj==null)
return false;
if(obj==this)
return true;
if(!(obj instanceof Community)) return false;
Community community = (Community)obj;
return community.getCommunityId() == this.getCommunityId();
}
當我檢查community==com
,它返回false ..爲什麼?我做了什麼錯誤?這兩個對象都從數據庫中檢索!
粘貼您的完整代碼。 –
[==運算符和equals()有什麼區別? (與哈希碼()???)](http://stackoverflow.com/questions/4505357/what-is-the-difference-between-operator-and-equals-with-hashcode) – McDowell
是否你已經overrided公共int hashCode() – swamy