1、重写equals方法时需要重写hashCode方法,主要是针对Map、Set等集合类型的使用;
a: Map、Set等集合类型存放的对象必须是唯一的;
b: 集合类判断两个对象是否相等,是先判断equals是否相等,如果equals返回TRUE,还要再判断HashCode返回值是否ture,只有两者都返回ture,才认为该两个对象是相等的。
2、由于Object的hashCode返回的是对象的hash值,所以即使equals返回TRUE,集合也可能判定两个对象不等,所以必须重写hashCode方法,以保证当equals返回TRUE时,hashCode也返回Ture,这样才能使得集合中存放的对象唯一。
2、我们都知道Java语言是完全面向对象的,在java中,所有的对象都是继承于Object类。Ojbect类中有两个方法equals、hashCode,这两个方法都是用来比较两个对象是否相等的。
在未重写equals方法我们是继承了object的equals方法,那里的 equals是比较两个对象的内存地址,显然我们new了2个对象内存地址肯定不一样
- 对于值对象,==比较的是两个对象的值
- 对于引用对象,比较的是两个对象的地址
默认的equals方法同==,一般来说我们的对象都是引用对象,要重写equals方法。
现在有一个学生对象,有属性学号跟姓名,现在我新建了一个学生对象,又从数据里查出一个学生对象,这两个对象的学号跟姓名都一样,那这两个对象是不是相等呢?一般情况下,除非你有特殊需求要处理,这两个对象是相等的,可如果用==去比较,返回的结果是错误的。
这时候我们就必须重写equlas方法了。如果学号是主键,在equals方法里,我们认为只要学号相同,就可以返回true。
hashCode方法也是可以用来比较两个对象是否相等的。但是我们很少使用,应该说是很少直接使用。hashCode方法返回的是一个int值,可以看做是一个对象的唯一编码,如果两个对象的hashCode值相同,我们应该认为这两个对象是同一个对象。
一般如果使用java中的Map对象进行存储时,他会自动调用hashCode方法来比较两个对象是否相等。
所以如果我们对equals方法进行了重写,建议一定要对hashCode方法重写,以保证相同的对象返回相同的hash值,不同的对象返回不同的hash值。
--------------以上引用,下面引入自己在excel中去重复值的一个场景:
package com.touna.checkaccount.bean;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* excel中查询出来的数据(重写equals和hashcode去重复)
*
* @author tl
*/
public class LoanCount {
/** 主键id */
private int id;
/** 序号 */
private Integer sequence;
/** 放款日期 */
private Date confirmDate;
/** 合同号 */
private String contractNo;
/** 借款人 */
private String custName;
/** 分公司号 */
private String siteNo;
/** 分公司 */
private String siteName;
/** 是否直销 */
private int isDirect;
/** 合作机构 */
private String cooperation;
/** 贷款类型 */
private String loanType;
/** 产品类型名字 */
private String productTypeName;
/** 产品类型 */
private String productType;
/** 还款方式 */
private String paymentTypeName;
/** 贷款总期数 */
private int totalPhases;
/** 客户主任 */
private String customerDirector;
/** 客户经理 */
private String customerManager;
/** 计息本金 */
private double auditamt;
/** 付款类型 */
private String payType;
/** 付款金额 */
private double payAmt;
/** 履约保证金 */
private double lybzjFees;
/** 考察费 */
private double kcFeeS;
/** gps费 */
private double gpsFees;
/** 抵押登记费 */
private double dydjFees;
/** 停车费 */
private double tcFees;
/** 盗抢险 */
private double dqxFees;
/** 刑侦费 */
private double xzFees;
/** 评估费 */
private double pgFees;
/** 律师见证费 */
private double lsjzFees;
/** 加急费 */
private double jjFees;
/** 风险金 */
private double kdFees;
/** 抵押登记 */
private double gzFees;
/** 手续费 */
private double sxFees;
/** 征信费 */
private double zxsxFees;
/** 快递费 */
private double shouldglf;
/** 其它 */
private double qtFees;
/** 报销状态 */
private int reimbursementStatus;
/** 审批状态 */
private int approvalStatus;
/** field 判断导入的属于哪个字段 */
private String field;
/** 处理时间 */
private Date handleDate;
/** 批次名 */
private String batchName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public Date getConfirmDate() {
return confirmDate;
}
public void setConfirmDate(Date confirmDate) {
this.confirmDate = confirmDate;
}
public String getContractNo() {
return contractNo;
}
public void setContractNo(String contractNo) {
this.contractNo = contractNo;
}
public String getSiteName() {
return siteName;
}
public void setSiteName(String siteName) {
this.siteName = siteName;
}
public int getIsDirect() {
return isDirect;
}
public void setIsDirect(int isDirect) {
this.isDirect = isDirect;
}
public String getCooperation() {
return cooperation;
}
public void setCooperation(String cooperation) {
this.cooperation = cooperation;
}
public String getLoanType() {
return loanType;
}
public void setLoanType(String loanType) {
this.loanType = loanType;
}
public String getProductTypeName() {
return productTypeName;
}
public void setProductTypeName(String productTypeName) {
this.productTypeName = productTypeName;
}
public String getProductType() {
return productType;
}
public void setProductType(String productType) {
this.productType = productType;
}
public String getPaymentTypeName() {
return paymentTypeName;
}
public void setPaymentTypeName(String paymentTypeName) {
this.paymentTypeName = paymentTypeName;
}
public int getTotalPhases() {
return totalPhases;
}
public void setTotalPhases(int totalPhases) {
this.totalPhases = totalPhases;
}
public String getCustomerDirector() {
return customerDirector;
}
public void setCustomerDirector(String customerDirector) {
this.customerDirector = customerDirector;
}
public String getCustomerManager() {
return customerManager;
}
public void setCustomerManager(String customerManager) {
this.customerManager = customerManager;
}
public double getAuditamt() {
return auditamt;
}
public void setAuditamt(double auditamt) {
this.auditamt = auditamt;
}
public String getPayType() {
return payType;
}
public void setPayType(String payType) {
this.payType = payType;
}
public double getPayAmt() {
return payAmt;
}
public void setPayAmt(double payAmt) {
this.payAmt = payAmt;
}
public double getLybzjFees() {
return lybzjFees;
}
public void setLybzjFees(double lybzjFees) {
this.lybzjFees = lybzjFees;
}
public double getKcFeeS() {
return kcFeeS;
}
public void setKcFeeS(double kcFeeS) {
this.kcFeeS = kcFeeS;
}
public double getGpsFees() {
return gpsFees;
}
public void setGpsFees(double gpsFees) {
this.gpsFees = gpsFees;
}
public double getDydjFees() {
return dydjFees;
}
public void setDydjFees(double dydjFees) {
this.dydjFees = dydjFees;
}
public double getTcFees() {
return tcFees;
}
public void setTcFees(double tcFees) {
this.tcFees = tcFees;
}
public double getDqxFees() {
return dqxFees;
}
public void setDqxFees(double dqxFees) {
this.dqxFees = dqxFees;
}
public double getXzFees() {
return xzFees;
}
public void setXzFees(double xzFees) {
this.xzFees = xzFees;
}
public double getPgFees() {
return pgFees;
}
public void setPgFees(double pgFees) {
this.pgFees = pgFees;
}
public double getLsjzFees() {
return lsjzFees;
}
public void setLsjzFees(double lsjzFees) {
this.lsjzFees = lsjzFees;
}
public double getJjFees() {
return jjFees;
}
public void setJjFees(double jjFees) {
this.jjFees = jjFees;
}
public double getKdFees() {
return kdFees;
}
public void setKdFees(double kdFees) {
this.kdFees = kdFees;
}
public double getGzFees() {
return gzFees;
}
public void setGzFees(double gzFees) {
this.gzFees = gzFees;
}
public double getSxFees() {
return sxFees;
}
public void setSxFees(double sxFees) {
this.sxFees = sxFees;
}
public double getZxsxFees() {
return zxsxFees;
}
public void setZxsxFees(double zxsxFees) {
this.zxsxFees = zxsxFees;
}
public double getShouldglf() {
return shouldglf;
}
public void setShouldglf(double shouldglf) {
this.shouldglf = shouldglf;
}
public double getQtFees() {
return qtFees;
}
public void setQtFees(double qtFees) {
this.qtFees = qtFees;
}
public int getReimbursementStatus() {
return reimbursementStatus;
}
public void setReimbursementStatus(int reimbursementStatus) {
this.reimbursementStatus = reimbursementStatus;
}
public int getApprovalStatus() {
return approvalStatus;
}
public String getSiteNo() {
return siteNo;
}
public void setSiteNo(String siteNo) {
this.siteNo = siteNo;
}
public void setApprovalStatus(int approvalStatus) {
this.approvalStatus = approvalStatus;
}
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
public Integer getSequence() {
return sequence;
}
public void setSequence(Integer sequence) {
this.sequence = sequence;
}
public Date getHandleDate() {
return handleDate;
}
public void setHandleDate(Date handleDate) {
this.handleDate = handleDate;
}
public String getBatchName() {
return batchName;
}
public void setBatchName(String batchName) {
this.batchName = batchName;
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (this == obj)
return true;
if (!(obj instanceof LoanCount))
return false;
LoanCount lr = (LoanCount) obj;
// 如果合同号相同且对应金额都不为空(>0),则认为重复
if (lr.getContractNo() != null && lr.getContractNo().equals(contractNo) && kcFeeS > 0 && lr.getKcFeeS() > 0) {
return true;
}
if (lr.getContractNo() != null && lr.getContractNo().equals(contractNo) && gpsFees > 0 && lr.getGpsFees() > 0) {
return true;
}
if (lr.getContractNo() != null && lr.getContractNo().equals(contractNo) && dydjFees > 0 && lr.getDydjFees() > 0) {
return true;
}
if (lr.getContractNo() != null && lr.getContractNo().equals(contractNo) && dqxFees > 0 && lr.getDqxFees() > 0) {
return true;
}
if (lr.getContractNo() != null && lr.getContractNo().equals(contractNo) && xzFees > 0 && lr.getXzFees() > 0) {
return true;
}
if (lr.getContractNo() != null && lr.getContractNo().equals(contractNo) && pgFees > 0 && lr.getPgFees() > 0) {
return true;
}
if (lr.getContractNo() != null && lr.getContractNo().equals(contractNo) && lsjzFees > 0 && lr.getLsjzFees() > 0) {
return true;
}
if (lr.getContractNo() != null && lr.getContractNo().equals(contractNo) && jjFees > 0 && lr.getJjFees() > 0) {
return true;
}
if (lr.getContractNo() != null && lr.getContractNo().equals(contractNo) && kdFees > 0 && lr.getKdFees() > 0) {
return true;
}
if (lr.getContractNo() != null && lr.getContractNo().equals(contractNo) && gzFees > 0 && lr.getGzFees() > 0) {
return true;
}
if (lr.getContractNo() != null && lr.getContractNo().equals(contractNo) && sxFees > 0 && lr.getSxFees() > 0) {
return true;
}
if (lr.getContractNo() != null && lr.getContractNo().equals(contractNo) && zxsxFees > 0 && lr.getZxsxFees() > 0) {
return true;
}
if (lr.getContractNo() != null && lr.getContractNo().equals(contractNo) && shouldglf > 0 && lr.getShouldglf() > 0) {
return true;
}
if (lr.getContractNo() != null && lr.getContractNo().equals(contractNo) && qtFees > 0 && lr.getQtFees() > 0) {
return true;
}
return false;
}
@Override
public int hashCode() {
return contractNo.hashCode() + 123 * 100;
}
public static void main(String[] args) {
LoanCount l1 = new LoanCount();
l1.setContractNo("FSRXJK-20150130407");
l1.setKcFeeS(90.0);
LoanCount l2 = new LoanCount();
l2.setContractNo("FSRXJK-20150130407");
l2.setKcFeeS(1100.0);
l2.setDqxFees(100);
List<LoanCount> loanCounts = new ArrayList<LoanCount>();
loanCounts.add(l1);
loanCounts.add(l2);
System.out.println(getSameValues(loanCounts).size());
}
/**
* 获取list中相同元素
* @param loanCounts
* @return
*/
private static List<LoanCount> getSameValues(List<LoanCount> loanCounts){
if(loanCounts == null || loanCounts.size() == 0) {
return null;
} else {
List<LoanCount> sames = new ArrayList<LoanCount>();
List<LoanCount> unsames = new ArrayList<LoanCount>();
for(LoanCount l : loanCounts) {
if(unsames.contains(l)) {
sames.add(l);
} else {
unsames.add(l);
}
}
return sames;
}
}
}