java中的hashcode怎么自己重写_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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值