比如专家的类定义为:
public class Expert{
private Integer id;
.....其他属性省略
public Integer getId(){
return id;
}
public void setId(Integer id){
this.id=id;
}
//覆写hashCode()方法
@Override
public int hashCode(){
int hash=0;
hash+=(id!=null?id.hashCode:0);
return hash;
}
//覆写equals()方法
public boolean equals(Object object){
if(!(object instanceof Expert)){
return false;
}
Expert other=(Expert)object;
if((this.id==null&&other.id!=null)||(this.id!=null&&!this.id.equals(other.id))){
return false
}
return true;
}
}
测试方法:
private Set<Integer> expertCart = new HashSet<Integer>();
public void test(Integer expertId){
expertCart.add(expertId);
}