java

在java中有时候要判断两个对象是否相等,不相等的情况下才做添加处理,我们一般是Set来保存model对象,因为Set是不能添加重复的对象的,但是Set是怎么判断对象重复的呢?
是通过重写hashCode()和equals()方法
比如:现在我有个业务model对象CaptureData
public class CaptureData implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;

/**
* data_id.
*/
private Long dataId;

/**
* product_code.
*/
private String productCode;

/**
* corp_code.
*/
private String corpCode;

/**
* corp_user_id.
*/
private String corpUserId;

/**
* cluster_code.
*/
private String clusterCode;

/**
* entity_id.
*/
private String entityId;

/**
* data.
*/
private String data;

/**
* start_time.
*/
private Date startTime;

/**
* end_time.
*/
private Date endTime;

/**
* file_name.
*/
private String fileName;

/**
* last_process_time.
*/
private Date lastProcessTime;

/**
* gmt_create.
*/
private Date gmtCreate;

private Boolean isSuccess ;

public Boolean getIsSuccess() {
return isSuccess;
}

public void setIsSuccess(Boolean isSuccess) {
this.isSuccess = isSuccess;
}


public Long getDataId() {
return dataId;
}

public void setDataId(Long dataId) {
this.dataId = dataId;
}

public String getProductCode() {
return this.productCode;
}

public void setProductCode(String productCode) {
this.productCode = productCode;
}

public String getCorpCode() {
return this.corpCode;
}

public void setCorpCode(String corpCode) {
this.corpCode = corpCode;
}

public String getCorpUserId() {
return this.corpUserId;
}

public void setCorpUserId(String corpUserId) {
this.corpUserId = corpUserId;
}

public String getClusterCode() {
return this.clusterCode;
}

public void setClusterCode(String clusterCode) {
this.clusterCode = clusterCode;
}

public String getEntityId() {
return this.entityId;
}

public void setEntityId(String entityId) {
this.entityId = entityId;
}

public String getData() {
return this.data;
}

public void setData(String data) {
this.data = data;
}

public Date getStartTime() {
return this.startTime;
}

public void setStartTime(Date startTime) {
this.startTime = startTime;
}

public Date getEndTime() {
return this.endTime;
}

public void setEndTime(Date endTime) {
this.endTime = endTime;
}

public String getFileName() {
return this.fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

public Date getLastProcessTime() {
return this.lastProcessTime;
}

public void setLastProcessTime(Date lastProcessTime) {
this.lastProcessTime = lastProcessTime;
}

@Override
public int hashCode() {
int hash = 7;
hash = 53 * hash + (this.corpCode != null ? this.corpCode.hashCode() : 0);
hash = 53 * hash + (this.corpUserId != null ? this.corpUserId.hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object obj) {
CaptureData other=obj instanceof CaptureData?(CaptureData)obj:null;
try{
return this.corpCode.equals(other.getCorpCode()) &&
this.getCorpUserId().equals(other.getCorpUserId());
}catch(Exception e){
return false;
}
}

public Date getGmtCreate() {
return gmtCreate;
}

public void setGmtCreate(Date gmtCreate) {
this.gmtCreate = gmtCreate;
}

}

由于我在这个对象重写了hashCode()和equals()方法 根据corpCode,corpUserId 相等的情况下 就判断是相同的对象(这里可以根据具体的业务逻辑来处理)
再把这个对象添加到Set集合中,所以添加的Set集合的重的对象都是不重复(根据你的业务条件)。

基础:
Child继承Parent,子类的变量和静态方法分别隐藏父类的变量和静态方法,子类的实例方法覆盖父类的实例方法。隐藏只是把父类的东东藏起来,但是其还是实质存在的;而覆盖就是把父类的东东完全抹掉以替换成子类的,是不可恢复的。在child被强制转换成Parent类型后,被隐藏的东西又被恢复了,而被覆盖的方法却是一去不复返了(子类的方法在这个实例里面永久替代了原先父类的方法)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值