Java——重写hashCode()和euqals()方法

package com.chantsoft.app.tc.org.beans;

import com.chantsoft.core.base.AbstractBean;

public class OrgRelationBean extends AbstractBean {

/** 源类型:人员资源类型 */
private Integer sourceCategory;

/** 源ID:人员ID */
private Long sourceId;

/** 目标类型:部门,组等资源类型 */
private Integer targetCategory;

/** 目标ID:部门,组的ID */
private Long targetId;

/** 关系类型:参见OrgRelationCategoryEnum */
private Integer relationCategory;

/** 关系类型描述:未启用 */
private Long relationValue1;

/** 关系类型描述:未启用 */
private String relationValue2;

/** 单位 */
private Long orgCorporationId;

private Integer state;

@Override
  // equals的重写
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof OrgRelationBean) {
if (((OrgRelationBean) obj).sourceCategory.equals(this.sourceCategory)
&& ((OrgRelationBean) obj).sourceId.equals(this.sourceId)
&& ((OrgRelationBean) obj).targetCategory.equals(this.targetCategory)
&& ((OrgRelationBean) obj).targetId.equals(this.targetId)
&& ((OrgRelationBean) obj).relationCategory.equals(this.relationCategory)
&& ((OrgRelationBean) obj).relationValue1.equals(this.relationValue1)
&& ((OrgRelationBean) obj).relationValue2.equals(this.relationValue2)
&& ((OrgRelationBean) obj).orgCorporationId.equals(this.orgCorporationId)
&& ((OrgRelationBean) obj).state.equals(this.state)) {
return true;
}
}

return false;
}

@Override
  //hasCode的重写
public int hashCode() {
StringBuilder sb = new StringBuilder();
sb.append(sourceCategory);
sb.append(sourceId);
sb.append(targetCategory);

sb.append(targetId);
sb.append(relationCategory);
sb.append(relationValue1);

sb.append(relationValue2);
sb.append(orgCorporationId);
sb.append(state);

char[] charArr = sb.toString().toCharArray();
int hash = 0;
for(char c : charArr) {
hash = hash * 131 + c;
}
return hash;
}

public OrgRelationBean() {

}

public OrgRelationBean(Integer sourceCategory,Long sourceId,Integer targetCategory,
Long targetId,Integer relationCategory,Long orgCorporationId) {
setNewId();
this.sourceCategory = sourceCategory;
this.sourceId = sourceId;
this.targetCategory = targetCategory;
this.targetId = targetId;
this.relationCategory = relationCategory;
this.orgCorporationId = orgCorporationId;
this.state = 0;
}

public Integer getSourceCategory() {
return this.sourceCategory;
}

public void setSourceCategory(Integer v) {
this.sourceCategory = v;
}

public Long getSourceId() {
return this.sourceId;
}

public void setSourceId(Long v) {
this.sourceId = v;
}

public Integer getTargetCategory() {
return this.targetCategory;
}

public void setTargetCategory(Integer v) {
this.targetCategory = v;
}

public Long getTargetId() {
return this.targetId;
}

public void setTargetId(Long v) {
this.targetId = v;
}

public Integer getRelationCategory() {
return this.relationCategory;
}

public void setRelationCategory(Integer v) {
this.relationCategory = v;
}

public Long getRelationValue1() {
return this.relationValue1;
}

public void setRelationValue1(Long v) {
this.relationValue1 = v;
}

public String getRelationValue2() {
return this.relationValue2;
}

public void setRelationValue2(String v) {
this.relationValue2 = v;
}

public Long getOrgCorporationId() {
return this.orgCorporationId;
}

public void setOrgCorporationId(Long v) {
this.orgCorporationId = v;
}

public Integer getState() {
return state;
}

public void setState(Integer state) {
this.state = state;
}

}
原文地址:https://www.cnblogs.com/czq520/p/11647694.html